RecyclerView

Recyclerview基本使用<5>-----侧滑

2017-07-08  本文已影响159人  天空在微笑

SwipeDelMenuLayout

滑动删除.png

必须引入上面的包,下面的例子也是上面库里的demo。想要快速实现上图的,引入包后直接copy下面的代码,想要详细了解的直接看上面的库。
  文件有FullDelDemoActivity、FullDelDemoAdapter、item_cst_swipe.xml

  1. FullDelDemoActivity
package mcxtzhang.swipedelmenu.FullDemo;
import android.app.Activity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Toast;
import com.mcxtzhang.swipemenulib.SwipeMenuLayout;
import java.util.ArrayList;
import java.util.List;
import mcxtzhang.listswipemenudemo.R;
import mcxtzhang.swipedelmenu.SwipeBean;
public class FullDelDemoActivity extends Activity {
    private static final String TAG = "zxt";
    private RecyclerView mRv;
    private FullDelDemoAdapter mAdapter;
    private LinearLayoutManager mLayoutManager;
    private List<SwipeBean> mDatas;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_full_del_demo);
        mRv = (RecyclerView) findViewById(R.id.rv);
        initDatas();
        mAdapter = new FullDelDemoAdapter(this, mDatas);
        mAdapter.setOnDelListener(new FullDelDemoAdapter.onSwipeListener() {
            @Override
            public void onDel(int pos) {
                if (pos >= 0 && pos < mDatas.size()) {
                    Toast.makeText(FullDelDemoActivity.this, "删除:" + pos, Toast.LENGTH_SHORT).show();
                    mDatas.remove(pos);
                    mAdapter.notifyItemRemoved(pos);//推荐用这个
                    //如果删除时,不使用mAdapter.notifyItemRemoved(pos),则删除没有动画效果,
                    //且如果想让侧滑菜单同时关闭,需要同时调用 ((CstSwipeDelMenu) holder.itemView).quickClose();
                    //mAdapter.notifyDataSetChanged();
                }
            }

            @Override
            public void onClick(int pos) {

            }

            @Override
            public void onTop(int pos) {
                if (pos > 0 && pos < mDatas.size()) {
                    SwipeBean swipeBean = mDatas.get(pos);
                    mDatas.remove(swipeBean);
                    mAdapter.notifyItemInserted(0);
                    mDatas.add(0, swipeBean);
                    mAdapter.notifyItemRemoved(pos + 1);
                    if (mLayoutManager.findFirstVisibleItemPosition() == 0) {
                        mRv.scrollToPosition(0);
                    }
                    //notifyItemRangeChanged(0,holder.getAdapterPosition()+1);
                }
            }
        });
        mRv.setAdapter(mAdapter);
//        mRv.setLayoutManager(mLayoutManager = new GridLayoutManager(this, 2));
        mRv.setLayoutManager(mLayoutManager = new LinearLayoutManager(this));

        //6 2016 10 21 add , 增加viewChache 的 get()方法,
        // 可以用在:当点击外部空白处时,关闭正在展开的侧滑菜单。我个人觉得意义不大,
        mRv.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (event.getAction() == MotionEvent.ACTION_UP) {
                    SwipeMenuLayout viewCache = SwipeMenuLayout.getViewCache();
                    if (null != viewCache) {
                        viewCache.smoothClose();
                    }
                }
                return false;
            }
        });
    }

    private void initDatas() {
        mDatas = new ArrayList<>();
        for (int i = 0; i < 20; i++) {
            mDatas.add(new SwipeBean("" + i));
        }
    }
}

  1. FullDelDemoAdapter
package mcxtzhang.swipedelmenu.FullDemo;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import java.util.List;
import mcxtzhang.listswipemenudemo.R;
import mcxtzhang.swipedelmenu.SwipeBean;
public class FullDelDemoAdapter extends RecyclerView.Adapter<FullDelDemoAdapter.FullDelDemoVH> {
    private Context mContext;
    private LayoutInflater mInfalter;
    private List<SwipeBean> mDatas;

    public FullDelDemoAdapter(Context context, List<SwipeBean> mDatas) {
        mContext = context;
        mInfalter = LayoutInflater.from(context);
        this.mDatas = mDatas;
    }

    public void setData(List<SwipeBean> data) {
        mDatas = data;
        notifyDataSetChanged();
    }
    @Override
    public FullDelDemoVH onCreateViewHolder(ViewGroup parent, int viewType) {
        return new FullDelDemoVH(mInfalter.inflate(R.layout.item_cst_swipe, parent, false));
    }

    @Override
    public void onBindViewHolder(final FullDelDemoVH holder, final int position) {
//        ((SwipeMenuLayout) holder.itemView).setIos(false).setLeftSwipe(position % 2 == 0 ? true : false);//这句话关掉IOS阻塞式交互效果 并依次打开左滑右滑
//        ((SwipeMenuLayout) holder.itemView).setIos(false).setLeftSwipe(position % 2 == 0 ? true : false);//这句话关掉IOS阻塞式交互效果 并依次打开左滑右滑

        holder.content.setText(mDatas.get(position).name + (position % 2 == 0 ? "我右白虎" : "我左青龙"));

        //验证长按
        holder.content.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                Toast.makeText(mContext, "longclig", Toast.LENGTH_SHORT).show();
                Log.d("TAG", "onLongClick() called with: v = [" + v + "]");
                return false;
            }
        });

//        holder.btnUnRead.setVisibility(position % 3 == 0 ? View.GONE : View.VISIBLE);

        holder.btnDelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (null != mOnSwipeListener) {
                    //如果删除时,不使用mAdapter.notifyItemRemoved(pos),则删除没有动画效果,
                    //且如果想让侧滑菜单同时关闭,需要同时调用 ((CstSwipeDelMenu) holder.itemView).quickClose();
                    //((CstSwipeDelMenu) holder.itemView).quickClose();
                    mOnSwipeListener.onDel(holder.getAdapterPosition());
                }
            }
        });
        //注意事项,设置item点击,不能对整个holder.itemView设置咯,只能对第一个子View,即原来的content设置,这算是局限性吧。
        (holder.content).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(mContext, "onClick:" + mDatas.get(holder.getAdapterPosition()).name, Toast.LENGTH_SHORT).show();
                Log.d("TAG", "onClick() called with: v = [" + v + "]");
                mOnSwipeListener.onClick(position);
            }
        });
        //置顶:
//        holder.btnTop.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                if (null!=mOnSwipeListener){
//                    mOnSwipeListener.onTop(holder.getAdapterPosition());
//                }
//
//            }
//        });
    }

    @Override
    public int getItemCount() {
        return null != mDatas ? mDatas.size() : 0;
    }

    /**
     * 和Activity通信的接口
     */
    public interface onSwipeListener {
        void onDel(int pos);
        void onClick(int pos);
        void onTop(int pos);
    }

    private onSwipeListener mOnSwipeListener;

    public onSwipeListener getOnDelListener() {
        return mOnSwipeListener;
    }

    public void setOnDelListener(onSwipeListener mOnDelListener) {
        this.mOnSwipeListener = mOnDelListener;
    }

    class FullDelDemoVH extends RecyclerView.ViewHolder {
        TextView content;
        Button btnDelete;
//        Button btnUnRead;
//        Button btnTop;

        public FullDelDemoVH(View itemView) {
            super(itemView);
            content = (TextView) itemView.findViewById(R.id.content);
            btnDelete = (Button) itemView.findViewById(R.id.btnDelete);
//            btnUnRead = (Button) itemView.findViewById(R.id.btnUnRead);
//            btnTop = (Button) itemView.findViewById(R.id.btnTop);
        }
    }
}


  1. item_cst_swipe.xml
<?xml version="1.0" encoding="utf-8"?>
<com.mcxtzhang.swipemenulib.SwipeMenuLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="100dp"
    android:clickable="true"
    android:paddingBottom="1dp"
    app:ios="false"
    app:leftSwipe="true"
    app:swipeEnable="true">

    <TextView
        android:id="@+id/content"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="?android:attr/selectableItemBackground"
        android:gravity="center"
        android:text="项目中我是任意复杂的原Item布局"/>

    <!--<!– 以下都是侧滑菜单的内容依序排列 –>-->
    <!--<Button-->
        <!--android:id="@+id/btnTop"-->
        <!--android:layout_width="60dp"-->
        <!--android:layout_height="match_parent"-->
        <!--android:background="#d9dee4"-->
        <!--android:text="置顶"-->
        <!--android:textColor="@android:color/white"/>-->

    <!--<Button-->
        <!--android:id="@+id/btnUnRead"-->
        <!--android:layout_width="120dp"-->
        <!--android:layout_height="match_parent"-->
        <!--android:background="#ecd50a"-->
        <!--android:clickable="true"-->
        <!--android:text="标记未读"-->
        <!--android:textColor="@android:color/white"/>-->

    <Button
        android:id="@+id/btnDelete"
        android:layout_width="60dp"
        android:layout_height="match_parent"
        android:background="@color/red_ff4a57"
        android:text="删除"
        android:textColor="@android:color/white"/>


    <!--    <RelativeLayout
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:background="@color/red_ff4a57"
            android:clickable="true">

            <TextView
                android:id="@+id/tv_delete"
                android:layout_width="100dp"
                android:layout_height="wrap_content"
                android:layout_centerVertical="true"
                android:drawablePadding="5dp"
                android:drawableTop="@drawable/point_icon_delete"
                android:gravity="center"
                android:text="删除"
                android:textColor="@android:color/white"/>
        </RelativeLayout>-->

</com.mcxtzhang.swipemenulib.SwipeMenuLayout>

实现原理介绍

占坑

上一篇 下一篇

猜你喜欢

热点阅读