您好,登錄后才能下訂單哦!
廢話不多說,下面展示一下效果。
這是GridView主文件實現。
public class GridViewActivity extends AppCompatActivity { RecyclerView mRecyclerView; List<String> mStringList; RecyclerAdapter mRecyAdapter; @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_recyclerview); initView(); initRecy(); } private void initView() { getSupportActionBar().setDisplayHomeAsUpEnabled(true); mRecyclerView = (RecyclerView) findViewById(R.id.view_recycler); } private void initRecy() { if (mStringList == null) { mStringList = new ArrayList<>(); } mStringList.addAll(DataManager.getData(20 - mStringList.size())); mRecyAdapter = new RecyclerAdapter(R.layout.item_gridview, mStringList, true); mRecyclerView.setLayoutManager(new GridLayoutManager(this, 4)); mRecyclerView.addItemDecoration(new DividerGriItemDecoration(this)); mRecyclerView.setHasFixedSize(true); RecyItemTouchHelperCallback itemTouchHelperCallback = new RecyItemTouchHelperCallback(mRecyAdapter, false, true); final ItemTouchHelper itemTouchHelper = new ItemTouchHelper(itemTouchHelperCallback); itemTouchHelper.attachToRecyclerView(mRecyclerView); mRecyclerView.addOnItemTouchListener(new OnRecyclerItemClickListener(mRecyclerView) { @Override public void onItemClick(RecyclerView.ViewHolder viewHolder) { RecyclerAdapter.ViewHolder viewHolder1 = (RecyclerAdapter.ViewHolder) viewHolder; String tvString = viewHolder1.mTextView.getText().toString(); Toast.makeText(GridViewActivity.this, "碰了一下 " + tvString, Toast.LENGTH_SHORT).show(); } @Override public void onLongClick(RecyclerView.ViewHolder viewHolder) { RecyclerAdapter.ViewHolder viewHolder1 = (RecyclerAdapter.ViewHolder) viewHolder; String tvString = viewHolder1.mTextView.getText().toString(); Toast.makeText(GridViewActivity.this, "長按不放可以拖動!", Toast.LENGTH_SHORT).show(); if (viewHolder.getLayoutPosition() != 0) { itemTouchHelper.startDrag(viewHolder); } } }); mRecyclerView.setAdapter(mRecyAdapter); } @Override public boolean onOptionsItemSelected(MenuItem item) { if (item.getItemId() == android.R.id.home) { finish(); } return super.onOptionsItemSelected(item); } }
代碼不難理解,就是activity里面設置一大堆初始化的東西。RecyclerView 初始化,setLayoutManager、addItemDecoration、setHasFixedSize、itemTouchHelper、addOnItemTouchListener、setAdapter。拖拽效果關鍵代碼是itemTouchHelper.startDrag(viewHolder)。其他都圍繞著它來轉。
然后具體看看各個設置。
DataManager
public class DataManager { private static List<String> sStringList = Arrays.asList("語文", "數學", "英語", "政治", "歷史", "化學", "生物", "地理", "體育", "音樂"); public static final List<String> getData(int number) { List<String> stringList = new ArrayList<>(); for (int i = 0; i < number; i++) { stringList.add(sStringList.get(i % sStringList.size())); } return stringList; } }
DividerGriItemDecoration 這個繪畫了item的邊線
public class DividerGriItemDecoration extends RecyclerView.ItemDecoration { private static final int[] ATTRS = new int[]{android.R.attr.listDivider}; private Drawable mDivider; private int lineWidth = 1; public DividerGriItemDecoration(GridViewActivity gridViewActivity) { final TypedArray array = gridViewActivity.obtainStyledAttributes(ATTRS); mDivider = array.getDrawable(0); array.recycle(); } public DividerGriItemDecoration(int color) { mDivider = new ColorDrawable(color); } @Override public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) { drawHorizontal(c, parent); drawVertical(c, parent); } private void drawHorizontal(Canvas c, RecyclerView parent) { int childCount = parent.getChildCount(); for (int i = 0; i < childCount; i++) { final View child = parent.getChildAt(i); final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child.getLayoutParams(); final int left = child.getLeft() - params.leftMargin; final int right = child.getRight() + params.rightMargin + lineWidth; final int top = child.getBottom() + params.bottomMargin; final int bottom = top + lineWidth; mDivider.setBounds(left, top, right, bottom); mDivider.draw(c); } } private void drawVertical(Canvas c, RecyclerView parent) { final int childCount = parent.getChildCount(); for (int i = 0; i < childCount; i++) { final View child = parent.getChildAt(i); final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child.getLayoutParams(); final int top = child.getTop() - params.topMargin; final int bottom = child.getBottom() + params.bottomMargin; final int left = child.getRight() + params.rightMargin; final int right = left + lineWidth; mDivider.setBounds(left, top, right, bottom); mDivider.draw(c); } } @Override public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) { outRect.set(0, 0, lineWidth, lineWidth); } }
RecyclerAdapter 適配器,和ui建立連接
public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.ViewHolder> { private int item_layout; private List<String> mDataList; private List<Integer> mInts; private boolean isFirstSpecial; public RecyclerAdapter(int item_gridview, List<String> dataList, boolean isFisrtSpecial) { this(item_gridview, dataList); this.isFirstSpecial = isFisrtSpecial; } public RecyclerAdapter(int item_gridview, List<String> dataList) { this.item_layout = item_gridview; this.mDataList = dataList; mInts = Arrays.asList(R.mipmap.ic_launcher, R.mipmap.ic_launcher, R.mipmap.ic_launcher, R.mipmap.ic_launcher, R.mipmap.ic_launcher, R.mipmap.ic_launcher); } @Override public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View view = LayoutInflater.from(parent.getContext()).inflate(item_layout, parent, false); return new ViewHolder(view); } @Override public void onBindViewHolder(ViewHolder holder, int position) { String str = mDataList.get(position); if (isFirstSpecial && position == 0) { holder.itemView.setBackgroundColor(Color.LTGRAY); holder.mTextView.setText("不許動"); holder.mImageView.setImageResource(R.mipmap.ic_launcher); } else { holder.itemView.setBackgroundColor(Color.WHITE); holder.mTextView.setText(str); holder.mImageView.setImageResource(mInts.get(position % mInts.size())); } } @Override public int getItemCount() { return mDataList == null ? 0 : mDataList.size(); } public List<String> getDataList() { return mDataList; } class ViewHolder extends RecyclerView.ViewHolder { TextView mTextView; ImageView mImageView; public ViewHolder(View itemView) { super(itemView); mTextView = itemView.findViewById(R.id.tv_item); mImageView = itemView.findViewById(R.id.img_item); } } }
OnRecyclerItemClickListener,點擊事件 。我們借用手勢工具類GestureDetectorCompat 來操作
public abstract class OnRecyclerItemClickListener implements RecyclerView.OnItemTouchListener{ private GestureDetectorCompat mGestureDetectorCompat; private RecyclerView mRecyclerView; public OnRecyclerItemClickListener(RecyclerView mRecyclerView) { this.mRecyclerView = mRecyclerView; mGestureDetectorCompat = new GestureDetectorCompat(mRecyclerView.getContext(), new ItemTouchHelperGestureListener()); } @Override public boolean onInterceptTouchEvent(RecyclerView rv, MotionEvent e) { mGestureDetectorCompat.onTouchEvent(e); return false; } @Override public void onTouchEvent(RecyclerView rv, MotionEvent e) { mGestureDetectorCompat.onTouchEvent(e); } @Override public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) { } //回調事件 public abstract void onItemClick(RecyclerView.ViewHolder viewHolder); public abstract void onLongClick(RecyclerView.ViewHolder viewHolder); private class ItemTouchHelperGestureListener extends GestureDetector.SimpleOnGestureListener { @Override public boolean onSingleTapUp(MotionEvent e) { View childViewUnder = mRecyclerView.findChildViewUnder(e.getX(), e.getY()); if (childViewUnder != null) { RecyclerView.ViewHolder childViewHolder = mRecyclerView.getChildViewHolder(childViewUnder); onItemClick(childViewHolder); } return true; } @Override public void onLongPress(MotionEvent e) { View childViewUnder = mRecyclerView.findChildViewUnder(e.getX(), e.getY()); if (childViewUnder != null) { RecyclerView.ViewHolder childViewHolder = mRecyclerView.getChildViewHolder(childViewUnder); onLongClick(childViewHolder); } } } }
RecyItemTouchHelperCallback
public class RecyItemTouchHelperCallback extends ItemTouchHelper.Callback { RecyclerView.Adapter mAdapter; boolean isSwipeEnable; boolean isFirstDragUnable; public RecyItemTouchHelperCallback(RecyclerView.Adapter mAdapter) { this.mAdapter = mAdapter; isSwipeEnable = true; isFirstDragUnable = false; } public RecyItemTouchHelperCallback(RecyclerView.Adapter mAdapter, boolean isSwipeEnable, boolean isFirstDragUnable) { this.mAdapter = mAdapter; this.isSwipeEnable = isSwipeEnable; this.isFirstDragUnable = isFirstDragUnable; } // 獲取Touch的響應方向 @Override public int getMovementFlags(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) { if (recyclerView.getLayoutManager() instanceof GridLayoutManager) {//網格布局時候 int dragFlags = ItemTouchHelper.UP | ItemTouchHelper.DOWN | ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT; int swipeFlags = 0; return makeMovementFlags(dragFlags, swipeFlags); } else {//list布局時候 int dragFlags = ItemTouchHelper.UP | ItemTouchHelper.DOWN; int swipeFlags = ItemTouchHelper.START | ItemTouchHelper.END; return makeMovementFlags(dragFlags, swipeFlags); } } @Override public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) { int fromPosition = viewHolder.getAdapterPosition(); int toPosition = target.getAdapterPosition(); if (isFirstDragUnable && toPosition == 0) { return false; } //重新更新排序 if (fromPosition < toPosition) { for (int i = fromPosition; i < toPosition; i++) { Collections.swap(((RecyclerAdapter) mAdapter).getDataList(), i, i + 1); } } else { for (int i = fromPosition; i > toPosition; i--) { Collections.swap(((RecyclerAdapter) mAdapter).getDataList(), i, i - 1); } } //刷新 mAdapter.notifyItemMoved(fromPosition, toPosition); return true; } /** * 側滑刪除后會回調的方法 */ @Override public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) { int adapterPosition = viewHolder.getAdapterPosition(); mAdapter.notifyItemRemoved(adapterPosition); ((RecyclerAdapter)mAdapter).getDataList().remove(adapterPosition); } @Override public void onSelectedChanged(RecyclerView.ViewHolder viewHolder, int actionState) { if (actionState != ItemTouchHelper.ACTION_STATE_IDLE) { viewHolder.itemView.setBackgroundColor(Color.LTGRAY); } super.onSelectedChanged(viewHolder, actionState); } @Override public void clearView(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) { super.clearView(recyclerView, viewHolder); viewHolder.itemView.setBackgroundColor(Color.WHITE); } @Override public boolean isLongPressDragEnabled() { return !isFirstDragUnable; } @Override public boolean isItemViewSwipeEnabled() { return isSwipeEnable; } }
下面是list的相關設置
同樣,listview也是一頓初始化設置。
public class ListViewActivity extends AppCompatActivity { RecyclerView mRecyclerView; List<String> mStringList; RecyclerAdapter mRecyAdapter; @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_recyclerview); initView(); initRecy(); } private void initRecy() { if (mStringList == null) { mStringList = new ArrayList<>(); } mStringList.addAll(DataManager.getData(15 - mStringList.size())); mRecyAdapter = new RecyclerAdapter(R.layout.item_listview, mStringList); mRecyclerView.setLayoutManager(new LinearLayoutManager(this)); mRecyclerView.addItemDecoration(new DividerListItemDecoration(this, LinearLayoutManager.VERTICAL)); mRecyclerView.setHasFixedSize(true); RecyItemTouchHelperCallback itemTouchHelperCallback = new RecyItemTouchHelperCallback(mRecyAdapter); final ItemTouchHelper itemTouchHelper = new ItemTouchHelper(itemTouchHelperCallback); itemTouchHelper.attachToRecyclerView(mRecyclerView); mRecyclerView.addOnItemTouchListener(new OnRecyclerItemClickListener(mRecyclerView) { @Override public void onItemClick(RecyclerView.ViewHolder viewHolder) { RecyclerAdapter.ViewHolder viewHolder1 = (RecyclerAdapter.ViewHolder) viewHolder; String tvString = viewHolder1.mTextView.getText().toString(); Toast.makeText(ListViewActivity.this, "碰了一下 " + tvString, Toast.LENGTH_SHORT).show(); } @Override public void onLongClick(RecyclerView.ViewHolder viewHolder) { Toast.makeText(ListViewActivity.this, "長按不放可以拖動!", Toast.LENGTH_SHORT).show(); } }); mRecyclerView.setAdapter(mRecyAdapter); } private void initView() { getSupportActionBar().setDisplayHomeAsUpEnabled(true); mRecyclerView = (RecyclerView) findViewById(R.id.view_recycler); } @Override public boolean onOptionsItemSelected(MenuItem item) { if (item.getItemId() == android.R.id.home) { finish(); } return super.onOptionsItemSelected(item); } }
DividerListItemDecoration
public class DividerListItemDecoration extends RecyclerView.ItemDecoration { private static final int[] ATTRS = new int[] {android.R.attr.listDivider}; public static final int HORIZONTAL_LIST = LinearLayoutManager.HORIZONTAL; public static final int VERTICAL_LIST = LinearLayoutManager.VERTICAL; private Drawable mDivider; private int mOrientation; public DividerListItemDecoration() { super(); } public DividerListItemDecoration(Context context, int orientation) { final TypedArray array = context.obtainStyledAttributes(ATTRS); mDivider = array.getDrawable(0); array.recycle(); this.mOrientation = orientation; } public DividerListItemDecoration(Context context, int orientation, int drawableId) { mDivider = ContextCompat.getDrawable(context, drawableId); setOrientation(orientation); } @Override public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) { super.onDraw(c, parent, state); } @Override public void onDrawOver(Canvas c, RecyclerView parent, RecyclerView.State state) { super.onDrawOver(c, parent, state); } @Override public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) { if (mOrientation == VERTICAL_LIST) { outRect.set(0, 0, 0, mDivider.getIntrinsicHeight()); } else { outRect.set(0, 0, mDivider.getIntrinsicWidth(), 0); } super.getItemOffsets(outRect, view, parent, state); } public void setOrientation(int orientation) { if (orientation != HORIZONTAL_LIST && orientation != VERTICAL_LIST) { throw new IllegalArgumentException("invalid orientaion"); } mOrientation = orientation; } }
源碼下載:http://download.csdn.net/download/loongago/9972876
總結
以上所述是小編給大家介紹的Android中RecyclerView拖拽、側刪功能的實現代碼,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對億速云網站的支持!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。