Android?Recyclerview實(shí)現(xiàn)左滑刪除功能
本文實(shí)例為大家分享了Android Recyclerview實(shí)現(xiàn)左滑刪除的具體代碼,供大家參考,具體內(nèi)容如下
1.先創(chuàng)建一個(gè)工具類
SlideRecyclerView
public class SlideRecyclerView extends RecyclerView { ? ? ? private static final String TAG = "SlideRecyclerView"; ? ? private static final int INVALID_POSITION = -1; // 觸摸到的點(diǎn)不在子View范圍內(nèi) ? ? private static final int INVALID_CHILD_WIDTH = -1; ?// 子ItemView不含兩個(gè)子View ? ? private static final int SNAP_VELOCITY = 600; ? // 最小滑動(dòng)速度 ? ? ? private VelocityTracker mVelocityTracker; ? // 速度追蹤器 ? ? private int mTouchSlop; // 認(rèn)為是滑動(dòng)的最小距離(一般由系統(tǒng)提供) ? ? private Rect mTouchFrame; ? // 子View所在的矩形范圍 ? ? private Scroller mScroller; ? ? private float mLastX; ? // 滑動(dòng)過程中記錄上次觸碰點(diǎn)X ? ? private float mFirstX, mFirstY; // 首次觸碰范圍 ? ? private boolean mIsSlide; ? // 是否滑動(dòng)子View ? ? private ViewGroup mFlingView; ? // 觸碰的子View ? ? private int mPosition; ?// 觸碰的view的位置 ? ? private int mMenuViewWidth; ? ?// 菜單按鈕寬度 ? ? ? public SlideRecyclerView(Context context) { ? ? ? ? this(context, null); ? ? } ? ? ? public SlideRecyclerView(Context context, @Nullable AttributeSet attrs) { ? ? ? ? this(context, attrs, 0); ? ? } ? ? ? public SlideRecyclerView(Context context, @Nullable AttributeSet attrs, int defStyle) { ? ? ? ? super(context, attrs, defStyle); ? ? ? ? mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop(); ? ? ? ? mScroller = new Scroller(context); ? ? } ? ? ? @Override ? ? public boolean onInterceptTouchEvent(MotionEvent e) { ? ? ? ? int x = (int) e.getX(); ? ? ? ? int y = (int) e.getY(); ? ? ? ? obtainVelocity(e); ? ? ? ? switch (e.getAction()) { ? ? ? ? ? ? case MotionEvent.ACTION_DOWN: ? ? ? ? ? ? ? ? if (!mScroller.isFinished()) { ?// 如果動(dòng)畫還沒停止,則立即終止動(dòng)畫 ? ? ? ? ? ? ? ? ? ? mScroller.abortAnimation(); ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? mFirstX = mLastX = x; ? ? ? ? ? ? ? ? mFirstY = y; ? ? ? ? ? ? ? ? mPosition = pointToPosition(x, y); ?// 獲取觸碰點(diǎn)所在的position ? ? ? ? ? ? ? ? if (mPosition != INVALID_POSITION) { ? ? ? ? ? ? ? ? ? ? View view = mFlingView; ? ? ? ? ? ? ? ? ? ? // 獲取觸碰點(diǎn)所在的view ? ? ? ? ? ? ? ? ? ? mFlingView = (ViewGroup) getChildAt(mPosition - ((LinearLayoutManager) getLayoutManager()).findFirstVisibleItemPosition()); ? ? ? ? ? ? ? ? ? ? // 這里判斷一下如果之前觸碰的view已經(jīng)打開,而當(dāng)前碰到的view不是那個(gè)view則立即關(guān)閉之前的view,此處并不需要擔(dān)動(dòng)畫沒完成沖突,因?yàn)橹耙呀?jīng)abortAnimation ? ? ? ? ? ? ? ? ? ? if (view != null && mFlingView != view && view.getScrollX() != 0) { ? ? ? ? ? ? ? ? ? ? ? ? view.scrollTo(0, 0); ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? ? // 這里進(jìn)行了強(qiáng)制的要求,RecyclerView的子ViewGroup必須要有2個(gè)子view,這樣菜單按鈕才會(huì)有值, ? ? ? ? ? ? ? ? ? ? // 需要注意的是:如果不定制RecyclerView的子View,則要求子View必須要有固定的width。 ? ? ? ? ? ? ? ? ? ? // 比如使用LinearLayout作為根布局,而content部分width已經(jīng)是match_parent,此時(shí)如果菜單view用的是wrap_content,menu的寬度就會(huì)為0。 ? ? ? ? ? ? ? ? ? ? if (mFlingView.getChildCount() == 2) { ? ? ? ? ? ? ? ? ? ? ? ? mMenuViewWidth = mFlingView.getChildAt(1).getWidth(); ? ? ? ? ? ? ? ? ? ? } else { ? ? ? ? ? ? ? ? ? ? ? ? mMenuViewWidth = INVALID_CHILD_WIDTH; ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? break; ? ? ? ? ? ? case MotionEvent.ACTION_MOVE: ? ? ? ? ? ? ? ? mVelocityTracker.computeCurrentVelocity(1000); ? ? ? ? ? ? ? ? // 此處有倆判斷,滿足其一則認(rèn)為是側(cè)滑: ? ? ? ? ? ? ? ? // 1.如果x方向速度大于y方向速度,且大于最小速度限制; ? ? ? ? ? ? ? ? // 2.如果x方向的側(cè)滑距離大于y方向滑動(dòng)距離,且x方向達(dá)到最小滑動(dòng)距離; ? ? ? ? ? ? ? ? float xVelocity = mVelocityTracker.getXVelocity(); ? ? ? ? ? ? ? ? float yVelocity = mVelocityTracker.getYVelocity(); ? ? ? ? ? ? ? ? if (Math.abs(xVelocity) > SNAP_VELOCITY && Math.abs(xVelocity) > Math.abs(yVelocity) ? ? ? ? ? ? ? ? ? ? ? ? || Math.abs(x - mFirstX) >= mTouchSlop ? ? ? ? ? ? ? ? ? ? ? ? && Math.abs(x - mFirstX) > Math.abs(y - mFirstY)) { ? ? ? ? ? ? ? ? ? ? mIsSlide = true; ? ? ? ? ? ? ? ? ? ? return true; ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? break; ? ? ? ? ? ? case MotionEvent.ACTION_UP: ? ? ? ? ? ? ? ? releaseVelocity(); ? ? ? ? ? ? ? ? break; ? ? ? ? } ? ? ? ? return super.onInterceptTouchEvent(e); ? ? } ? ? ? @Override ? ? public boolean onTouchEvent(MotionEvent e) { ? ? ? ? if (mIsSlide && mPosition != INVALID_POSITION) { ? ? ? ? ? ? float x = e.getX(); ? ? ? ? ? ? obtainVelocity(e); ? ? ? ? ? ? switch (e.getAction()) { ? ? ? ? ? ? ? ? case MotionEvent.ACTION_DOWN: ? // 因?yàn)闆]有攔截,所以不會(huì)被調(diào)用到 ? ? ? ? ? ? ? ? ? ? break; ? ? ? ? ? ? ? ? case MotionEvent.ACTION_MOVE: ? ? ? ? ? ? ? ? ? ? // 隨手指滑動(dòng) ? ? ? ? ? ? ? ? ? ? if (mMenuViewWidth != INVALID_CHILD_WIDTH) { ? ? ? ? ? ? ? ? ? ? ? ? float dx = mLastX - x; ? ? ? ? ? ? ? ? ? ? ? ? if (mFlingView.getScrollX() + dx <= mMenuViewWidth ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? && mFlingView.getScrollX() + dx > 0) { ? ? ? ? ? ? ? ? ? ? ? ? ? ? mFlingView.scrollBy((int) dx, 0); ? ? ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? ? ? ? mLastX = x; ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? ? break; ? ? ? ? ? ? ? ? case MotionEvent.ACTION_UP: ? ? ? ? ? ? ? ? ? ? if (mMenuViewWidth != INVALID_CHILD_WIDTH) { ? ? ? ? ? ? ? ? ? ? ? ? int scrollX = mFlingView.getScrollX(); ? ? ? ? ? ? ? ? ? ? ? ? mVelocityTracker.computeCurrentVelocity(1000); ? ? ? ? ? ? ? ? ? ? ? ? // 此處有兩個(gè)原因決定是否打開菜單: ? ? ? ? ? ? ? ? ? ? ? ? // 1.菜單被拉出寬度大于菜單寬度一半; ? ? ? ? ? ? ? ? ? ? ? ? // 2.橫向滑動(dòng)速度大于最小滑動(dòng)速度; ? ? ? ? ? ? ? ? ? ? ? ? // 注意:之所以要小于負(fù)值,是因?yàn)橄蜃蠡瑒t速度為負(fù)值 ? ? ? ? ? ? ? ? ? ? ? ? if (mVelocityTracker.getXVelocity() < -SNAP_VELOCITY) { ? ?// 向左側(cè)滑達(dá)到側(cè)滑最低速度,則打開 ? ? ? ? ? ? ? ? ? ? ? ? ? ? mScroller.startScroll(scrollX, 0, mMenuViewWidth - scrollX, 0, Math.abs(mMenuViewWidth - scrollX)); ? ? ? ? ? ? ? ? ? ? ? ? } else if (mVelocityTracker.getXVelocity() >= SNAP_VELOCITY) { ?// 向右側(cè)滑達(dá)到側(cè)滑最低速度,則關(guān)閉 ? ? ? ? ? ? ? ? ? ? ? ? ? ? mScroller.startScroll(scrollX, 0, -scrollX, 0, Math.abs(scrollX)); ? ? ? ? ? ? ? ? ? ? ? ? } else if (scrollX >= mMenuViewWidth / 2) { // 如果超過刪除按鈕一半,則打開 ? ? ? ? ? ? ? ? ? ? ? ? ? ? mScroller.startScroll(scrollX, 0, mMenuViewWidth - scrollX, 0, Math.abs(mMenuViewWidth - scrollX)); ? ? ? ? ? ? ? ? ? ? ? ? } else { ? ?// 其他情況則關(guān)閉 ? ? ? ? ? ? ? ? ? ? ? ? ? ? mScroller.startScroll(scrollX, 0, -scrollX, 0, Math.abs(scrollX)); ? ? ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? ? ? ? invalidate(); ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? ? mMenuViewWidth = INVALID_CHILD_WIDTH; ? ? ? ? ? ? ? ? ? ? mIsSlide = false; ? ? ? ? ? ? ? ? ? ? mPosition = INVALID_POSITION; ? ? ? ? ? ? ? ? ? ? releaseVelocity(); ?// 這里之所以會(huì)調(diào)用,是因?yàn)槿绻懊鏀r截了,就不會(huì)執(zhí)行ACTION_UP,需要在這里釋放追蹤 ? ? ? ? ? ? ? ? ? ? break; ? ? ? ? ? ? } ? ? ? ? ? ? return true; ? ? ? ? } else { ? ? ? ? ? ? // 此處防止RecyclerView正?;瑒?dòng)時(shí),還有菜單未關(guān)閉 ? ? ? ? ? ? closeMenu(); ? ? ? ? ? ? // Velocity,這里的釋放是防止RecyclerView正常攔截了,但是在onTouchEvent中卻沒有被釋放; ? ? ? ? ? ? // 有三種情況:1.onInterceptTouchEvent并未攔截,在onInterceptTouchEvent方法中,DOWN和UP一對(duì)獲取和釋放; ? ? ? ? ? ? // 2.onInterceptTouchEvent攔截,DOWN獲取,但事件不是被側(cè)滑處理,需要在這里進(jìn)行釋放; ? ? ? ? ? ? // 3.onInterceptTouchEvent攔截,DOWN獲取,事件被側(cè)滑處理,則在onTouchEvent的UP中釋放。 ? ? ? ? ? ? releaseVelocity(); ? ? ? ? } ? ? ? ? return super.onTouchEvent(e); ? ? } ? ? ? private void releaseVelocity() { ? ? ? ? if (mVelocityTracker != null) { ? ? ? ? ? ? mVelocityTracker.clear(); ? ? ? ? ? ? mVelocityTracker.recycle(); ? ? ? ? ? ? mVelocityTracker = null; ? ? ? ? } ? ? } ? ? ? private void obtainVelocity(MotionEvent event) { ? ? ? ? if (mVelocityTracker == null) { ? ? ? ? ? ? mVelocityTracker = VelocityTracker.obtain(); ? ? ? ? } ? ? ? ? mVelocityTracker.addMovement(event); ? ? } ? ? ? public int pointToPosition(int x, int y) { ? ? ? ? if (null == getLayoutManager()) return INVALID_POSITION; ? ? ? ? int firstPosition = ((LinearLayoutManager) getLayoutManager()).findFirstVisibleItemPosition(); ? ? ? ? Rect frame = mTouchFrame; ? ? ? ? if (frame == null) { ? ? ? ? ? ? mTouchFrame = new Rect(); ? ? ? ? ? ? frame = mTouchFrame; ? ? ? ? } ? ? ? ? ? final int count = getChildCount(); ? ? ? ? for (int i = count - 1; i >= 0; i--) { ? ? ? ? ? ? final View child = getChildAt(i); ? ? ? ? ? ? if (child.getVisibility() == View.VISIBLE) { ? ? ? ? ? ? ? ? child.getHitRect(frame); ? ? ? ? ? ? ? ? if (frame.contains(x, y)) { ? ? ? ? ? ? ? ? ? ? return firstPosition + i; ? ? ? ? ? ? ? ? } ? ? ? ? ? ? } ? ? ? ? } ? ? ? ? return INVALID_POSITION; ? ? } ? ? ? @Override ? ? public void computeScroll() { ? ? ? ? if (mScroller.computeScrollOffset()) { ? ? ? ? ? ? mFlingView.scrollTo(mScroller.getCurrX(), mScroller.getCurrY()); ? ? ? ? ? ? invalidate(); ? ? ? ? } ? ? } ? ? ? /** ? ? ?* 將顯示子菜單的子view關(guān)閉 ? ? ?* 這里本身是要自己來實(shí)現(xiàn)的,但是由于不定制item,因此不好監(jiān)聽器點(diǎn)擊事件,因此需要調(diào)用者手動(dòng)的關(guān)閉 ? ? ?*/ ? ? public void closeMenu() { ? ? ? ? if (mFlingView != null && mFlingView.getScrollX() != 0) { ? ? ? ? ? ? mFlingView.scrollTo(0, 0); ? ? ? ? } ? ? } }
2.主布局
<com.xxx.xxx.SlideRecyclerView ? ? android:id="@+id/shoppingtrolley_rv" ? ? android:layout_width="match_parent" ? ? android:layout_height="wrap_content" />
3.子布局
adapter_shoppingtrollery
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" ? ? android:layout_width="match_parent" ? ? android:layout_height="wrap_content" ? ? android:orientation="horizontal" ? ? android:background="#FFF8F8F8" ? ? android:clickable="true"> ? ? ? <RelativeLayout ? ? ? ? android:layout_width="match_parent" ? ? ? ? android:layout_height="@dimen/dp_133" ? ? ? ? android:layout_marginTop="@dimen/dp_12" ? ? ? ? android:background="@color/white"> ? ? ? ? <CheckBox ? ? ? ? ? ? android:id="@+id/shoppingtrollery_ischeck" ? ? ? ? ? ? android:layout_width="@dimen/dp_20" ? ? ? ? ? ? android:layout_height="@dimen/dp_20" ? ? ? ? ? ? android:button="@null" ? ? ? ? ? ? android:background="@drawable/shoppingtrollery_selector" ? ? ? ? ? ? android:layout_marginStart="@dimen/dp_14" ? ? ? ? ? ? android:layout_centerVertical="true"/> ? ? ? ? <ImageView ? ? ? ? ? ? android:id="@+id/shoppingtrollery_iv" ? ? ? ? ? ? android:layout_width="@dimen/dp_100" ? ? ? ? ? ? android:layout_height="@dimen/dp_100" ? ? ? ? ? ? android:src="@mipmap/logo" ? ? ? ? ? ? android:layout_toEndOf="@+id/shoppingtrollery_ischeck" ? ? ? ? ? ? android:layout_centerVertical="true" ? ? ? ? ? ? android:layout_marginStart="@dimen/dp_11"/> ? ? ? ? <TextView ? ? ? ? ? ? android:id="@+id/shoppingtrollery_name" ? ? ? ? ? ? android:layout_width="wrap_content" ? ? ? ? ? ? android:layout_height="wrap_content" ? ? ? ? ? ? android:text="超級(jí)團(tuán)品polo衫" ? ? ? ? ? ? android:textSize="@dimen/sp_16" ? ? ? ? ? ? android:textColor="@color/_333333" ? ? ? ? ? ? android:layout_toEndOf="@+id/shoppingtrollery_iv" ? ? ? ? ? ? android:layout_marginTop="@dimen/dp_22" ? ? ? ? ? ? android:layout_marginStart="@dimen/dp_14" ? ? ? ? ? ? android:textStyle="bold"/> ? ? ? ? <TextView ? ? ? ? ? ? android:id="@+id/shoppingtrollery_type" ? ? ? ? ? ? android:layout_width="wrap_content" ? ? ? ? ? ? android:layout_height="wrap_content" ? ? ? ? ? ? android:text="2瓶裝套裝" ? ? ? ? ? ? android:textSize="@dimen/sp_12" ? ? ? ? ? ? android:textColor="@color/_333333" ? ? ? ? ? ? android:layout_toEndOf="@+id/shoppingtrollery_iv" ? ? ? ? ? ? android:layout_below="@+id/shoppingtrollery_name" ? ? ? ? ? ? android:layout_marginTop="@dimen/dp_12" ? ? ? ? ? ? android:layout_marginStart="@dimen/dp_14"/> ? ? ? ? <LinearLayout ? ? ? ? ? ? android:layout_width="wrap_content" ? ? ? ? ? ? android:layout_height="wrap_content" ? ? ? ? ? ? android:orientation="horizontal" ? ? ? ? ? ? android:layout_below="@+id/shoppingtrollery_type" ? ? ? ? ? ? android:layout_marginTop="@dimen/dp_6" ? ? ? ? ? ? android:layout_toEndOf="@+id/shoppingtrollery_iv" ? ? ? ? ? ? android:layout_marginStart="@dimen/dp_11"> ? ? ? ? ? ? <TextView ? ? ? ? ? ? ? ? android:layout_width="wrap_content" ? ? ? ? ? ? ? ? android:layout_height="wrap_content" ? ? ? ? ? ? ? ? android:text="¥" ? ? ? ? ? ? ? ? android:textSize="@dimen/sp_12" ? ? ? ? ? ? ? ? android:textColor="@color/black" ? ? ? ? ? ? ? ? android:textStyle="bold"/> ? ? ? ? ? ? <TextView ? ? ? ? ? ? ? ? android:id="@+id/shoppingtrollery_price" ? ? ? ? ? ? ? ? android:layout_width="wrap_content" ? ? ? ? ? ? ? ? android:layout_height="wrap_content" ? ? ? ? ? ? ? ? android:text="400.00" ? ? ? ? ? ? ? ? android:textSize="@dimen/sp_18" ? ? ? ? ? ? ? ? android:textColor="@color/black" ? ? ? ? ? ? ? ? android:textStyle="bold"/> ? ? ? ? </LinearLayout> ? ? ? ? <LinearLayout ? ? ? ? ? ? android:layout_width="@dimen/dp_72" ? ? ? ? ? ? android:layout_height="@dimen/dp_24" ? ? ? ? ? ? android:orientation="horizontal" ? ? ? ? ? ? android:layout_alignParentEnd="true" ? ? ? ? ? ? android:layout_marginEnd="@dimen/dp_12" ? ? ? ? ? ? android:layout_alignParentBottom="true" ? ? ? ? ? ? android:layout_marginBottom="@dimen/dp_17" ? ? ? ? ? ? android:background="@drawable/shoppingtrollery_adddelete_bg"> ? ? ? ? ? ? <TextView ? ? ? ? ? ? ? ? android:id="@+id/shoppingtrollery_subtract" ? ? ? ? ? ? ? ? android:layout_width="@dimen/dp_0" ? ? ? ? ? ? ? ? android:layout_weight="1" ? ? ? ? ? ? ? ? android:layout_height="match_parent" ? ? ? ? ? ? ? ? android:text="-" ? ? ? ? ? ? ? ? android:textColor="@color/_333333" ? ? ? ? ? ? ? ? android:textSize="@dimen/sp_15" ? ? ? ? ? ? ? ? android:gravity="center" ? ? ? ? ? ? ? ? android:textStyle="bold"/> ? ? ? ? ? ? <TextView ? ? ? ? ? ? ? ? android:id="@+id/shoppingtrollery_count" ? ? ? ? ? ? ? ? android:layout_width="@dimen/dp_0" ? ? ? ? ? ? ? ? android:layout_weight="1" ? ? ? ? ? ? ? ? android:layout_height="match_parent" ? ? ? ? ? ? ? ? android:text="1" ? ? ? ? ? ? ? ? android:textColor="@color/_333333" ? ? ? ? ? ? ? ? android:textSize="@dimen/sp_15" ? ? ? ? ? ? ? ? android:gravity="center" ? ? ? ? ? ? ? ? android:textStyle="bold"/> ? ? ? ? ? ? <TextView ? ? ? ? ? ? ? ? android:id="@+id/shoppingtrollery_add" ? ? ? ? ? ? ? ? android:layout_width="@dimen/dp_0" ? ? ? ? ? ? ? ? android:layout_weight="1" ? ? ? ? ? ? ? ? android:layout_height="match_parent" ? ? ? ? ? ? ? ? android:text="+" ? ? ? ? ? ? ? ? android:textColor="@color/_333333" ? ? ? ? ? ? ? ? android:textSize="@dimen/sp_15" ? ? ? ? ? ? ? ? android:gravity="center" ? ? ? ? ? ? ? ? android:textStyle="bold"/> ? ? ? ? </LinearLayout> ? ? </RelativeLayout> ? ? <LinearLayout ? ? ? ? android:layout_width="@dimen/dp_84" ? ? ? ? android:layout_height="match_parent" ? ? ? ? android:orientation="horizontal"> ? ? ? ? <TextView ? ? ? ? ? ? android:id="@+id/shoppingtrollery_delete" ? ? ? ? ? ? android:layout_width="match_parent" ? ? ? ? ? ? android:layout_height="match_parent" ? ? ? ? ? ? android:background="#FFE6212A" ? ? ? ? ? ? android:gravity="center" ? ? ? ? ? ? android:text="刪除" ? ? ? ? ? ? android:textColor="@color/white" ? ? ? ? ? ? android:textSize="@dimen/sp_16" /> ? ? </LinearLayout> </LinearLayout>
4.適配器
ShoppingtrolleyAdapter
public class ShoppingtrolleyAdapter extends RecyclerView.Adapter<ShoppingtrolleyAdapter.Myvh> { ? ? ? Context context; ? ? List<String> list; ? ? private OnItemClickListener mOnItemClickListener; ? ? ? public ShoppingtrolleyAdapter(Context context) { ? ? ? ? this.context = context; ? ? ? ? list = new ArrayList<>(); ? ? } ? ? ? public void setList(List<String> list) { ? ? ? ? this.list = list; ? ? ? ? notifyDataSetChanged(); ? ? } ? ? ? public void setOnItemClickListener(OnItemClickListener onItemClickListener) { ? ? ? ? mOnItemClickListener = onItemClickListener; ? ? } ? ? ? public interface OnItemClickListener { ? ? ? ? void onItemClick(View view, int position); ? ? } ? ? ? @NonNull ? ? @NotNull ? ? @Override ? ? public ShoppingtrolleyAdapter.Myvh onCreateViewHolder(@NonNull @NotNull ViewGroup parent, int viewType) { ? ? ? ? View view = LayoutInflater.from(context).inflate(R.layout.adapter_shoppingtrollery,parent,false); ? ? ? ? return new ShoppingtrolleyAdapter.Myvh(view); ? ? } ? ? ? @SuppressLint("SetTextI18n") ? ? @Override ? ? public void onBindViewHolder(@NonNull @NotNull ShoppingtrolleyAdapter.Myvh holder, int position) { ? ? ? ? ? //左滑刪除事件 ? ? ? ? holder.shoppingtrollery_delete.setOnClickListener(new View.OnClickListener() { ? ? ? ? ? ? @Override ? ? ? ? ? ? public void onClick(View v) { ? ? ? ? ? ? ? ? if (mOnItemClickListener != null) { ? ? ? ? ? ? ? ? ? ? mOnItemClickListener.onItemClick(v, position); ? ? ? ? ? ? ? ? } ? ? ? ? ? ? } ? ? ? ? }); ? ? } ? ? ? @Override ? ? public int getItemCount() { ? ? ? ? return list.size(); ? ? } ? ? ? public class Myvh extends RecyclerView.ViewHolder { ? ? ? ? ? private final CheckBox shoppingtrollery_ischeck; ? ? ? ? private final ImageView shoppingtrollery_iv; ? ? ? ? private final TextView shoppingtrollery_name; ? ? ? ? private final TextView shoppingtrollery_type; ? ? ? ? private final TextView shoppingtrollery_price; ? ? ? ? private final TextView shoppingtrollery_subtract; ? ? ? ? private final TextView shoppingtrollery_count; ? ? ? ? private final TextView shoppingtrollery_add; ? ? ? ? private final TextView shoppingtrollery_delete; ? ? ? ? ? public Myvh(@NonNull @NotNull View itemView) { ? ? ? ? ? ? super(itemView); ? ? ? ? ? ? ? shoppingtrollery_ischeck = itemView.findViewById(R.id.shoppingtrollery_ischeck); ? ? ? ? ? ? shoppingtrollery_iv = itemView.findViewById(R.id.shoppingtrollery_iv); ? ? ? ? ? ? shoppingtrollery_name = itemView.findViewById(R.id.shoppingtrollery_name); ? ? ? ? ? ? shoppingtrollery_type = itemView.findViewById(R.id.shoppingtrollery_type); ? ? ? ? ? ? shoppingtrollery_price = itemView.findViewById(R.id.shoppingtrollery_price); ? ? ? ? ? ? shoppingtrollery_subtract = itemView.findViewById(R.id.shoppingtrollery_subtract); ? ? ? ? ? ? shoppingtrollery_count = itemView.findViewById(R.id.shoppingtrollery_count); ? ? ? ? ? ? shoppingtrollery_add = itemView.findViewById(R.id.shoppingtrollery_add); ? ? ? ? ? ? shoppingtrollery_delete = itemView.findViewById(R.id.shoppingtrollery_delete); ? ? ? ? } ? ? } }
5.主頁代碼
/** ? ? ?* 購物車列表 ? ? ?*/ ? ? private void setlistdata() { ? ? ? ? ? SlideRecyclerView shoppingtrolley_rv = findViewById(R.id.shoppingtrolley_rv); ? ? ? ? // 創(chuàng)建布局管理 ? ? ? ? LinearLayoutManager layoutManager = new LinearLayoutManager(this); ? ? ? ? layoutManager.setOrientation(LinearLayoutManager.VERTICAL); ? ? ? ? shoppingtrolley_rv.setLayoutManager(layoutManager); ? ? ? ? ? //模擬數(shù)據(jù) ? ? ? ? List<String> list = new ArrayList<>(); ? ? ? ? list.add("a"); ? ? ? ? list.add("a"); ? ? ? ? list.add("a"); ? ? ? ? list.add("a"); ? ? ? ? list.add("a"); ? ? ? ? ShoppingtrolleyAdapter shoppingtrolleyAdapter = new ShoppingtrolleyAdapter(this); ? ? ? ? shoppingtrolleyAdapter.setList(list); ? ? ? ? shoppingtrolley_rv.setAdapter(shoppingtrolleyAdapter); ? ? ? ? ? //左滑刪除 ? ? ? ? shoppingtrolleyAdapter.setOnItemClickListener(new ShoppingtrolleyAdapter.OnItemClickListener() { ? ? ? ? ? ? @Override ? ? ? ? ? ? public void onItemClick(View view, int position) { ? ? ? ? ? ? ? ? list.remove(position); ? ? ? ? ? ? ? ? shoppingtrolleyAdapter.notifyDataSetChanged(); ? ? ? ? ? ? ? ? //Toast.makeText(ShoppingtrolleyActivity.this, ""+position, Toast.LENGTH_SHORT).show(); ? ? ? ? ? ? } ? ? ? ? }); }
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
android電源信息查看(電量、溫度、電壓)實(shí)例代碼
這篇文章主要介紹了android電源信息查看方法,以實(shí)例形式較為詳細(xì)的分析了Android實(shí)現(xiàn)電源電量、電壓、溫度等信息查看的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-10-10Android實(shí)現(xiàn)動(dòng)態(tài)添加標(biāo)簽及其點(diǎn)擊事件
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)動(dòng)態(tài)添加標(biāo)簽及其點(diǎn)擊事件,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-12-12Android DynamicGrid實(shí)現(xiàn)拖曳交換位置功能
這篇文章主要為大家詳細(xì)介紹了Android DynamicGrid實(shí)現(xiàn)拖曳交換位置功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-06-06Android實(shí)現(xiàn)取消GridView中Item選中時(shí)默認(rèn)的背景色
這篇文章主要介紹了Android實(shí)現(xiàn)取消GridView中Item選中時(shí)默認(rèn)的背景色,涉及Android GridView中Item屬性設(shè)置的相關(guān)技巧,需要的朋友可以參考下2016-02-02Android?XML數(shù)據(jù)解析要點(diǎn)介紹
這篇文章主要為大家介紹了Android?XML數(shù)據(jù)解析要點(diǎn)介紹,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-04-04Android波紋擴(kuò)散效果之仿支付寶咻一咻功能實(shí)現(xiàn)波紋擴(kuò)散特效
這篇文章主要介紹了Android波紋擴(kuò)散效果之仿支付寶咻一咻功能實(shí)現(xiàn)波紋擴(kuò)散特效的相關(guān)資料,需要的朋友可以參考下2016-02-02