实现功能:
1.recyclerview点击和长按事件
2.recyclerview滑动删除和移动位置
效果图如下:
public class RecyclerViewAdapter extends RecyclerView.Adapterimplements ItemTouchHelper { private List mStrings = new ArrayList<>(); private OnItemClickListener mOnItemClickListener;//点击事件 private OnItemLongClickListener mOnItemLongClickListener;//长按事件 public RecyclerViewAdapter(List strings) { mStrings = strings; } public void setOnItemLongClickListener(OnItemLongClickListener onItemLongClickListener) { mOnItemLongClickListener = onItemLongClickListener; } public void setOnItemClickListener(OnItemClickListener onItemClickListener) { this.mOnItemClickListener = onItemClickListener; } @Override public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_recycle_view, parent, false); return new ViewHolder(view, mOnItemClickListener, mOnItemLongClickListener); } @Override public void onBindViewHolder(ViewHolder holder, int position) { holder.mTextView.setText("" + mStrings.get(position)); } @Override public int getItemCount() { return mStrings.size(); } //删除item数据 public void deleItem(int postion) { mStrings.remove(postion); this.notifyItemRemoved(postion); } //新增item public void addItem(int postion) { mStrings.add(postion, "ss"); this.notifyItemInserted(postion); } //移动item @Override public void onItemMove(int startPostion, int endPosttion) { Collections.swap(mStrings, startPostion, endPosttion); this.notifyItemMoved(startPostion, endPosttion); } //滑动删除item数据 @Override public void onItemDele(int postion) { deleItem(postion); } }
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener, View.OnLongClickListener { TextView mTextView; private OnItemClickListener mOnItemClickListener; private OnItemLongClickListener mOnItemLongClickListener; public ViewHolder(View itemView, OnItemClickListener onItemClickListener, OnItemLongClickListener onItemLongClickListener) { super(itemView); mTextView = (TextView) itemView.findViewById(R.id.tv); this.mOnItemClickListener = onItemClickListener; this.mOnItemLongClickListener = onItemLongClickListener; itemView.setOnClickListener(this); itemView.setOnLongClickListener(this); } @Override public void onClick(View view) { if (mOnItemClickListener != null) { mOnItemClickListener.onItemClickListener(view, getLayoutPosition()); } } @Override public boolean onLongClick(View view) { if (mOnItemClickListener != null) { mOnItemLongClickListener.onItemLongClickListener(view, getLayoutPosition()); } return true; } }
public interface OnItemClickListener { void onItemClickListener(View view, int postion); }
public interface OnItemLongClickListener { void onItemLongClickListener(View view, int postion); }
public interface ItemTouchHelper { //移动 void onItemMove(int startPostion,int endPosttion); //删除 void onItemDele(int postion); }
public class ItemTouchHelperCallback extends ItemTouchHelper.Callback { private RecyclerViewAdapter mAdapter; public ItemTouchHelperCallback(RecyclerViewAdapter adapter) { mAdapter = adapter; } //返回滑动的方向 @Override public int getMovementFlags(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) { int moveFlags = ItemTouchHelper.UP | ItemTouchHelper.DOWN;//上下移动 int swipeFlags = ItemTouchHelper.LEFT;//从右向左滑动 return makeMovementFlags(moveFlags, swipeFlags); } //拖动时交换位置 @Override public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) { mAdapter.onItemMove(viewHolder.getAdapterPosition(), target.getAdapterPosition()); return true; } //滑动大约一半时,调用该方法删除item @Override public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) { mAdapter.onItemDele(viewHolder.getAdapterPosition()); } @Override public boolean isLongPressDragEnabled() { return true; } @Override public boolean isItemViewSwipeEnabled() { return true; } //滑动时改变item颜色 @Override public void onChildDraw(Canvas c, RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, float dX, float dY, int actionState, boolean isCurrentlyActive) { super.onChildDraw(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive); if (actionState == ItemTouchHelper.ACTION_STATE_SWIPE) { final float alpha = 1 - Math.abs(dX) / (float) viewHolder.itemView.getWidth(); viewHolder.itemView.setAlpha(alpha); viewHolder.itemView.setTranslationX(dX); } } }
public class MainActivity extends AppCompatActivity { private RecyclerView mRecyclerView; private RecyclerViewAdapter mAdapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initView(); } private void initView() { mRecyclerView = (RecyclerView) findViewById(R.id.recycle_view); mAdapter = new RecyclerViewAdapter(getString()); mRecyclerView.setAdapter(mAdapter); mRecyclerView.setItemAnimator(new DefaultItemAnimator()); mRecyclerView.setLayoutManager(new LinearLayoutManager(this)); initListener(); initItemTouchHelper(); } private void initItemTouchHelper() { //实例化Callback ItemTouchHelper.Callback callback = new ItemTouchHelperCallback(mAdapter); ItemTouchHelper touchHelper = new ItemTouchHelper(callback); touchHelper.attachToRecyclerView(mRecyclerView); } private void initListener() { mAdapter.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClickListener(View view, int postion) { Toast.makeText(MainActivity.this, "我被点击啦" + postion, Toast.LENGTH_SHORT).show(); } }); mAdapter.setOnItemLongClickListener(new OnItemLongClickListener() { @Override public void onItemLongClickListener(View view, int postion) { // Toast.makeText(MainActivity.this, "我被点击啦" + postion + " " + "长按", Toast.LENGTH_SHORT).show(); // mAdapter.deleItem(postion);//长按删除 // mAdapter.addItem(postion);//长按添加 } }); } private ListgetString() { List mStrings = new ArrayList<>(); for (int i = 0; i < 20; i++) { mStrings.add("哈哈哈哈哈哈" + i); } return mStrings; } }
xml version="1.0" encoding="utf-8"?>xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.example.admin.recyclerviewdemo.MainActivity"> <android.support.v7.widget.RecyclerView android:id="@+id/recycle_view" android:layout_width="match_parent" android:layout_height="wrap_content"> android.support.v7.widget.RecyclerView>
xml version="1.0" encoding="utf-8"?>xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="10dp" android:background="@mipmap/ic_launcher"/> android:id="@+id/tv" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="25dp"/> android:layout_width="match_parent" android:layout_height="1px" android:layout_marginLeft="10dp" android:layout_marginRight="10dp" android:background="@color/colorPrimary"/>