RecyclerView实现侧滑删除、置顶、滑动
1、首先在build.gradle里添加
compile 'com.github.mcxtzhang:SwipeDelMenuLayout:V1.2.1'
2、设置recyclerView的item布局
<?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="#d9dee4" android:text="删除" android:textColor="@android:color/white" /> </com.mcxtzhang.swipemenulib.SwipeMenuLayout>
3、Activity
public class RecyclerSlideActivity extends AppCompatActivity { private RecyclerView recyclerView; private RecyclerSlideAdapter adapter; private List<String> mDatas; private LinearLayoutManager mLayoutManager; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_recycler_slide); initData(); recyclerView = (RecyclerView) findViewById(R.id.recycler); adapter = new RecyclerSlideAdapter(this, mDatas); adapter.setOnDelListener(new RecyclerSlideAdapter.onSlideListener() { @Override public void onDel(int position) { Toast.makeText(RecyclerSlideActivity.this, "删除:" + position, Toast.LENGTH_SHORT).show(); mDatas.remove(position); adapter.notifyItemChanged(position); } @Override public void onTop(int position) { String s = mDatas.get(position); mDatas.remove(s); adapter.notifyItemInserted(0); mDatas.add(0, s); adapter.notifyItemRemoved(position + 1); if (mLayoutManager.findFirstVisibleItemPosition() == 0) { recyclerView.scrollToPosition(0); } } }); recyclerView.setAdapter(adapter); recyclerView.setLayoutManager(mLayoutManager = new LinearLayoutManager(this)); recyclerView.setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View view, MotionEvent motionEvent) { if (motionEvent.getAction() == MotionEvent.ACTION_UP) { SwipeMenuLayout viewCache = SwipeMenuLayout.getViewCache(); if (null != viewCache) { viewCache.smoothClose(); } } return false; } }); } private void initData() { mDatas = new ArrayList<>(); for (int i = 0; i < 40; i++) { mDatas.add("item" + i); } } }
4、adapter
public class RecyclerSlideAdapter extends RecyclerView.Adapter<RecyclerSlideAdapter.Slide> { private Context context; private LayoutInflater mInflater; private List<String> mDatas; public RecyclerSlideAdapter(Context context, List<String> mDatas) { this.context = context; this.mDatas = mDatas; mInflater = LayoutInflater.from(context); } @Override public Slide onCreateViewHolder(ViewGroup parent, int viewType) { return new Slide(mInflater.inflate(R.layout.item_recycler, parent, false)); } @Override public void onBindViewHolder(final Slide holder, final int position) { //这句话关掉IOS阻塞式交互效果,并依此打开左滑右滑 // ((SwipeMenuLayout) holder.itemView).setIos(false).setLeftSwipe(position % 2 == 0 ? true : false); // holder.content.setText(mDatas.get(position)); //验证长按 holder.content.setOnLongClickListener(new View.OnLongClickListener() { @Override public boolean onLongClick(View view) { Toast.makeText(context, "longclig", Toast.LENGTH_SHORT).show(); return false; } }); holder.btnUnRead.setVisibility(position % 3 == 0 ? View.GONE : View.VISIBLE); holder.btnDelete.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { /** * 如果删除时,不适用adapter.notifyItemRemoved(position),则删除没有动画效果 * 如果想让侧滑菜单同时关闭,需要同时调用(CstSwipeDelMenu)holder.itemView).quickClose(); */ mOnSlideListener.onDel(holder.getAdapterPosition()); } }); //注意事项,设置item点击,不能对真个holder.itemView设置,只能对第一个TextView设置 holder.content.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Toast.makeText(context, "onClick:" + position, Toast.LENGTH_SHORT).show(); } }); //置顶 holder.btnTop.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { if (null != mOnSlideListener) { mOnSlideListener.onTop(holder.getAdapterPosition()); } } }); } @Override public int getItemCount() { return null != mDatas ? mDatas.size() : 0; } /** * 和Activity通信的接口 */ public interface onSlideListener { void onDel(int position); void onTop(int position); } private onSlideListener mOnSlideListener; public onSlideListener getmOnSlideListener() { return mOnSlideListener; } public void setOnDelListener(onSlideListener mOnDelListener) { this.mOnSlideListener = mOnDelListener; } public class Slide extends RecyclerView.ViewHolder { TextView content; Button btnDelete; Button btnUnRead; Button btnTop; public Slide(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); } } }
RecyclerView滑动Scroll
package com.bumptech.glide.integration.recyclerview; import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.RecyclerView; import android.widget.AbsListView; import android.widget.ListView; /** * Converts {@link android.support.v7.widget.RecyclerView.OnScrollListener} events to * {@link AbsListView} scroll events. * * <p>Requires that the the recycler view be using a {@link LinearLayoutManager} subclass. */ public final class RecyclerToListViewScrollListener extends RecyclerView.OnScrollListener { public static final int UNKNOWN_SCROLL_STATE = Integer.MIN_VALUE; private final AbsListView.OnScrollListener scrollListener; private int lastFirstVisible = -1; private int lastVisibleCount = -1; private int lastItemCount = -1; public RecyclerToListViewScrollListener(AbsListView.OnScrollListener scrollListener) { this.scrollListener = scrollListener; } @Override public void onScrollStateChanged(RecyclerView recyclerView, int newState) { int listViewState; switch (newState) { case RecyclerView.SCROLL_STATE_DRAGGING: listViewState = ListView.OnScrollListener.SCROLL_STATE_TOUCH_SCROLL; break; case RecyclerView.SCROLL_STATE_IDLE: listViewState = ListView.OnScrollListener.SCROLL_STATE_IDLE; break; case RecyclerView.SCROLL_STATE_SETTLING: listViewState = ListView.OnScrollListener.SCROLL_STATE_FLING; break; default: listViewState = UNKNOWN_SCROLL_STATE; } scrollListener.onScrollStateChanged(null /*view*/, listViewState); } @Override public void onScrolled(RecyclerView recyclerView, int dx, int dy) { LinearLayoutManager layoutManager = (LinearLayoutManager) recyclerView.getLayoutManager(); int firstVisible = layoutManager.findFirstVisibleItemPosition(); int visibleCount = Math.abs(firstVisible - layoutManager.findLastVisibleItemPosition()); int itemCount = recyclerView.getAdapter().getItemCount(); if (firstVisible != lastFirstVisible || visibleCount != lastVisibleCount || itemCount != lastItemCount) { scrollListener.onScroll(null, firstVisible, visibleCount, itemCount); lastFirstVisible = firstVisible; lastVisibleCount = visibleCount; lastItemCount = itemCount; } } }