Android 列表侧滑删除

2018-07-06  本文已影响0人  GrayZhang

SwipeLayout

import android.content.Context;

import android.util.AttributeSet;

import android.util.Log;

import android.view.MotionEvent;

import android.view.View;

import android.widget.HorizontalScrollView;

import android.widget.TextView;

import com.wondersgroup.smartcampus.teacher.R;

/**

* Created by ${zhanggaobo} on 2018/6/25.

* 侧滑删除

*/

public class SwipeLayoutextends HorizontalScrollView {

private TextViewmTextView_Delete;

private int mScrollWidth;

private IonSlidingButtonListenermIonSlidingButtonListener;

private BooleanisOpen =false;

private Booleanonce =false;

public SwipeLayout(Context context) {

this(context,null);

}

public SwipeLayout(Context context, AttributeSet attrs) {

this(context, attrs,0);

}

public SwipeLayout(Context context, AttributeSet attrs,int defStyleAttr) {

super(context, attrs, defStyleAttr);

this.setOverScrollMode(OVER_SCROLL_NEVER);

}

@Override

    protected void onMeasure(int widthMeasureSpec,int heightMeasureSpec) {

super.onMeasure(widthMeasureSpec, heightMeasureSpec);

if (!once) {

mTextView_Delete = (TextView) findViewById(R.id.tv_delete);

once =true;

}

}

@Override

    protected void onLayout(boolean changed,int l,int t,int r,int b) {

super.onLayout(changed, l, t, r, b);

if (changed) {

this.scrollTo(0,0);

//获取水平滚动条可以滑动的范围,即右侧按钮的宽度

            mScrollWidth =mTextView_Delete.getWidth();

Log.i("asd","mScrollWidth:" +mScrollWidth);

}

}

@Override

    public boolean onTouchEvent(MotionEvent ev) {

int action = ev.getAction();

switch (action) {

case MotionEvent.ACTION_DOWN:

case MotionEvent.ACTION_MOVE:

mIonSlidingButtonListener.onDownOrMove(this);

break;

case MotionEvent.ACTION_UP:

case MotionEvent.ACTION_CANCEL:

changeScrollx();

return true;

default:

break;

}

return super.onTouchEvent(ev);

}

@Override

    protected void onScrollChanged(int l,int t,int oldl,int oldt) {

super.onScrollChanged(l, t, oldl, oldt);

mTextView_Delete.setTranslationX(l -mScrollWidth);

}

/**

* 按滚动条被拖动距离判断关闭或打开菜单

*/

    public void changeScrollx() {

if (getScrollX() >= (mScrollWidth /2)) {

this.smoothScrollTo(mScrollWidth,0);

isOpen =true;

mIonSlidingButtonListener.onMenuIsOpen(this);

}else {

this.smoothScrollTo(0,0);

isOpen =false;

}

}

/**

* 打开菜单

*/

    public void openMenu() {

if (isOpen) {

return;

}

this.smoothScrollTo(mScrollWidth,0);

isOpen =true;

mIonSlidingButtonListener.onMenuIsOpen(this);

}

/**

* 关闭菜单

*/

    public void closeMenu() {

if (!isOpen) {

return;

}

this.smoothScrollTo(0,0);

isOpen =false;

}

public void setSlidingButtonListener(IonSlidingButtonListener listener) {

mIonSlidingButtonListener = listener;

}

public interface IonSlidingButtonListener {

void onMenuIsOpen(View view);

void onDownOrMove(SwipeLayout swipeLayout);

}

}

layout

android:id="@+id/swipe_layout"

    android:layout_width="match_parent"

    android:layout_height="60dp"

    android:layout_marginBottom="1dp"

    android:background="@color/white">

        android:layout_width="match_parent"

        android:layout_height="match_parent">

            android:id="@+id/tv_delete"

            android:layout_width="100dp"

            android:layout_height="match_parent"

            android:layout_toRightOf="@+id/layout_content"

            android:background="@color/red_car"

            android:gravity="center"

            android:text="删 除"

            android:textColor="#DDFFFFFF" />

            android:id="@+id/layout_content"

            android:layout_width="match_parent"

            android:layout_height="match_parent"

            android:background="@color/white"

            android:clickable="true">

                android:id="@+id/imgae_check"

                android:layout_width="16dp"

                android:layout_height="16dp"

                android:layout_alignParentRight="true"

                android:layout_centerVertical="true"

                android:layout_marginRight="@dimen/margin_15"

                android:src="@mipmap/icon_circle_big"

                android:visibility="gone" />

                android:id="@+id/image_file"

                android:layout_width="wrap_content"

                android:layout_height="wrap_content"

                android:layout_alignParentLeft="true"

                android:layout_centerVertical="true"

                android:layout_marginLeft="@dimen/margin_15"

                android:layout_marginRight="@dimen/margin_15"

                android:src="@mipmap/icon_file_folder" />

                android:layout_width="match_parent"

                android:layout_height="match_parent"

                android:layout_toRightOf="@+id/image_file"

                android:gravity="center_vertical"

                android:orientation="vertical">

                    android:id="@+id/text_file_name"

                    android:layout_width="wrap_content"

                    android:layout_height="wrap_content"

                    android:layout_marginBottom="@dimen/margin_5"

                    android:lineSpacingExtra="6sp"

                    android:text="教学笔记-2018"

                    android:textColor="#333333"

                    android:textSize="15sp" />

                    android:id="@+id/text_file_time"

                    android:layout_width="wrap_content"

                    android:layout_height="wrap_content"

                    android:text="2018-06-20  12:00"

                    android:textColor="#999999"

                    android:textSize="12.1sp" />

package com.wondersgroup.smartcampus.teacher.adapter.file;

import android.content.Context;

import android.support.annotation.NonNull;

import android.support.v7.widget.RecyclerView;

import android.view.LayoutInflater;

import android.view.View;

import android.view.ViewGroup;

import android.widget.ImageView;

import android.widget.RelativeLayout;

import android.widget.TextView;

import com.johnson.commonlibs.common_utils.utils.DateUtils;

import com.johnson.commonlibs.common_utils.utils.DensityUtil;

import com.johnson.commonlibs.common_utils.utils.StrUtils;

import com.wondersgroup.smartcampus.teacher.R;

import com.wondersgroup.smartcampus.teacher.model.filesmanager.FilesResult;

import com.wondersgroup.smartcampus.teacher.view.SwipeLayout;

import java.util.ArrayList;

import java.util.List;

import butterknife.Bind;

import butterknife.ButterKnife;

/**

* 列表适配器

*/

public class MyFilesAdapterextends RecyclerView.Adapterimplements SwipeLayout.IonSlidingButtonListener {

private Listdata;

private Contextcontext;

private SwipeLayoutmMenu =null;

private IonSlidingViewClickListenermIDeleteClickListener;

private Listchooses =new ArrayList<>();

private boolean isSelect =false;

public void setSelect(boolean select) {

isSelect = select;

chooses.clear();

notifyDataSetChanged();

}

public void setData(List data) {

this.data = data;

notifyDataSetChanged();

}

public void clear() {

chooses.clear();

notifyDataSetChanged();

}

public List getData() {

return data;

}

public List getChooses() {

return chooses;

}

public MyFilesAdapter(List data, Context context) {

this.data = data;

this.context = context;

}

public void setmIDeleteClickListener(IonSlidingViewClickListener mIDeleteClickListener) {

this.mIDeleteClickListener = mIDeleteClickListener;

}

@NonNull

@Override

    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent,int viewType) {

return new ViewHolder(LayoutInflater.from(context).inflate(R.layout.item_files_view, parent,false));

}

@Override

    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder,final int position) {

final FilesResult.BodyBean.DocListBean dataBean =data.get(position);

final ViewHolder viewHolder = (ViewHolder) holder;

viewHolder.swipeLayout.closeMenu();

if (!StrUtils.equals(dataBean.getDocType(),"0")) {

viewHolder.tvDelete.setVisibility(View.VISIBLE);

if (StrUtils.equals(dataBean.getDocType(),"1")) {

viewHolder.imageFile.setImageResource(R.mipmap.icon_file_doc);

}else if (StrUtils.equals(dataBean.getDocType(),"2")) {

viewHolder.imageFile.setImageResource(R.mipmap.icon_file_pdf);

}else if (StrUtils.equals(dataBean.getDocType(),"3")) {

viewHolder.imageFile.setImageResource(R.mipmap.icon_file_jpg);

}else if (StrUtils.equals(dataBean.getDocType(),"4")) {

viewHolder.imageFile.setImageResource(R.mipmap.icon_file_video);

}

if (isSelect) {

viewHolder.imgaeCheck.setVisibility(View.VISIBLE);

if (chooses.contains(dataBean.getId())) {

viewHolder.imgaeCheck.setImageResource(R.mipmap.icon_check);

}else {

viewHolder.imgaeCheck.setImageResource(R.mipmap.icon_circle_big);

}

}else {

viewHolder.imgaeCheck.setVisibility(View.GONE);

}

}else {

viewHolder.tvDelete.setVisibility(View.GONE);

viewHolder.imageFile.setImageResource(R.mipmap.icon_file_folder);

viewHolder.imgaeCheck.setVisibility(View.GONE);

}

viewHolder.textFileName.setText(dataBean.getName());

viewHolder.textFileTime.setText(DateUtils.time2StringMM(dataBean.getCreateTime()));

viewHolder.layoutContent.getLayoutParams().width = DensityUtil.getScreenWidth(context);

viewHolder.layoutContent.setOnClickListener(new View.OnClickListener() {

@Override

            public void onClick(View v) {

//判断是否有删除菜单打开

                if (menuIsOpen()) {

closeMenu();//关闭菜单

                }else {

if (!isSelect) {

int n =viewHolder.getLayoutPosition();

mIDeleteClickListener.onItemClick(v, n);

}else {

if (!StrUtils.equals(dataBean.getDocType(),"0")) {

if (chooses.contains(dataBean.getId())) {

chooses.remove(dataBean.getId());

viewHolder.imgaeCheck.setImageResource(R.mipmap.icon_circle_big);

}else {

chooses.add(dataBean.getId());

viewHolder.imgaeCheck.setImageResource(R.mipmap.icon_check);

}

}else {

int n =viewHolder.getLayoutPosition();

mIDeleteClickListener.onItemClick(v, n);

}

}

}

}

});

viewHolder.layoutContent.setOnLongClickListener(new View.OnLongClickListener() {

@Override

            public boolean onLongClick(View v) {

int n =viewHolder.getLayoutPosition();

mIDeleteClickListener.onLongClick(v, n);

return true;

}

});

viewHolder.tvDelete.setOnClickListener(new View.OnClickListener() {

@Override

            public void onClick(View v) {

int n =viewHolder.getLayoutPosition();

mIDeleteClickListener.onDeleteBtnCilck(v, n);

}

});

}

@Override

    public int getItemCount() {

return (data !=null &&data.size() >0) ?data.size() :0;

}

/**

* 关闭菜单

*/

    public void closeMenu() {

mMenu.closeMenu();

mMenu =null;

}

/**

* 判断是否有菜单打开

*/

    public Boolean menuIsOpen() {

if (mMenu !=null) {

return true;

}

return false;

}

/**

* 删除菜单打开信息接收

*/

    @Override

    public void onMenuIsOpen(View view) {

mMenu = (SwipeLayout) view;

}

/**

* 滑动或者点击了Item监听

*

    * @param slidingButtonView

    */

    @Override

    public void onDownOrMove(SwipeLayout slidingButtonView) {

if (menuIsOpen()) {

if (mMenu != slidingButtonView) {

closeMenu();

}

}

}

public interface IonSlidingViewClickListener {

void onItemClick(View view,int position);

void onDeleteBtnCilck(View view,int position);

void onLongClick(View view,int position);

}

class ViewHolderextends RecyclerView.ViewHolder {

@Bind(R.id.swipe_layout)

SwipeLayoutswipeLayout;

@Bind(R.id.tv_delete)

TextViewtvDelete;

@Bind(R.id.imgae_check)

ImageViewimgaeCheck;

@Bind(R.id.image_file)

ImageViewimageFile;

@Bind(R.id.text_file_name)

TextViewtextFileName;

@Bind(R.id.text_file_time)

TextViewtextFileTime;

@Bind(R.id.layout_content)

RelativeLayoutlayoutContent;

public ViewHolder(View itemView) {

super(itemView);

ButterKnife.bind(this, itemView);

((SwipeLayout) itemView).setSlidingButtonListener(MyFilesAdapter.this);

}

}

}

上一篇下一篇

猜你喜欢

热点阅读