您好,登錄后才能下訂單哦!
這篇文章給大家介紹使用RecyclerView怎么實現(xiàn)一個側(cè)滑拖拽功能,內(nèi)容非常詳細,感興趣的小伙伴們可以參考借鑒,希望對大家能有所幫助。
ItemDragListener
package slideslipdrag; import android.support.v7.widget.RecyclerView; public interface ItemDragListener { /** * 拖拽 * * @param viewHolder viewHolder */ void onStartDrag(RecyclerView.ViewHolder viewHolder); }
ItemTouchHelperAdapter
package slideslipdrag; public interface ItemTouchHelperAdapter { /** * 移動 * * @param fromPosition 起始位 * @param toPosition 終止位 */ void onItemMove(int fromPosition, int toPosition); /** * 側(cè)滑結(jié)束 * * @param position 位置 */ void onItemSlideSlipEnd(int position); }
ItemTouchHelperViewHolder
package slideslipdrag; import android.content.Context; /** * @decs: ItemTouchHelperViewHolder * @date: 2018/7/22 13:24 * @version: v 1.0 */ public interface ItemTouchHelperViewHolder { /** * 側(cè)滑開始 * * @param context 上下文 */ void onItemSlideSlipStart(Context context); /** * 側(cè)滑停止 * * @param context 上下文 */ void onItemSlideSlipStop(Context context); }
SimpleItemTouchHelperCallback
package slideslipdrag; import android.content.Context; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Canvas; import android.graphics.Paint; import android.support.v4.content.ContextCompat; import android.support.v7.widget.RecyclerView; import android.support.v7.widget.helper.ItemTouchHelper; import android.view.View; import com.self.zsp.dfs.R; import util.DensityUtils; /** * @decs: SimpleItemTouchHelperCallback * @date: 2018/7/22 13:34 * @version: v 1.0 */ public class SimpleItemTouchHelperCallback extends ItemTouchHelper.Callback { private final ItemTouchHelperAdapter mAdapter; private Context context; private Paint paint = new Paint(); public SimpleItemTouchHelperCallback(ItemTouchHelperAdapter adapter, Context context) { mAdapter = adapter; this.context = context; } @Override public boolean isLongPressDragEnabled() { return false; } @Override public boolean isItemViewSwipeEnabled() { return true; } @Override public int getMovementFlags(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) { // Enable drag up and down and right swipe in right direction final int dragFlags = ItemTouchHelper.UP | ItemTouchHelper.DOWN; final int swipeFlags = ItemTouchHelper.END; // final int swipeFlags = ItemTouchHelper.END | ItemTouchHelper.START; Enable swipe in both direction return makeMovementFlags(dragFlags, swipeFlags); } @Override public long getAnimationDuration(RecyclerView recyclerView, int animationType, float animateDx, float animateDy) { // return animationType == ItemTouchHelper.ANIMATION_TYPE_DRAG ? DEFAULT_DRAG_ANIMATION_DURATION : DEFAULT_SWIPE_ANIMATION_DURATION; return animationType == ItemTouchHelper.ANIMATION_TYPE_DRAG ? DEFAULT_DRAG_ANIMATION_DURATION : 350; } @Override public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder source, RecyclerView.ViewHolder target) { if (source.getItemViewType() != target.getItemViewType()) { return false; } // Notify the adapter of the move mAdapter.onItemMove(source.getAdapterPosition(), target.getAdapterPosition()); return true; } @Override public void onChildDrawOver(Canvas c, RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, float dX, float dY, int actionState, boolean isCurrentlyActive) { super.onChildDrawOver(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive); } @Override public void onSwiped(final RecyclerView.ViewHolder viewHolder, int i) { // Notify the adapter of the dismissal mAdapter.onItemSlideSlipEnd(viewHolder.getAdapterPosition()); } @Override public int getBoundingBoxMargin() { return super.getBoundingBoxMargin(); } @Override public void onChildDraw(final Canvas c, RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, float dX, float dY, int actionState, boolean isCurrentlyActive) { super.onChildDraw(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive); // Fade out the view as it is swiped out of the parent's bounds if (actionState == ItemTouchHelper.ACTION_STATE_SWIPE) { View itemView = viewHolder.itemView; Bitmap icon; if (dX > 0) { icon = BitmapFactory.decodeResource(context.getResources(), R.drawable.delete); // Set color for right swipe paint.setColor(ContextCompat.getColor(context, R.color.red)); // Draw Rect with varying right side, equal to displacement dX c.drawRect((float) itemView.getLeft() + DensityUtils.dpToPxFour(0), (float) itemView.getTop(), dX + DensityUtils.dpToPxFour(0), (float) itemView.getBottom(), paint); // Set the image icon for right swipe c.drawBitmap(icon, (float) itemView.getLeft() + DensityUtils.dpToPxFour(16), (float) itemView.getTop() + ((float) itemView.getBottom() - (float) itemView.getTop() - icon.getHeight()) / 2, paint); icon.recycle(); } } } @Override public void onSelectedChanged(RecyclerView.ViewHolder viewHolder, int actionState) { if (actionState != ItemTouchHelper.ACTION_STATE_IDLE) { // Let the view holder know that this item is being moved or dragged ItemTouchHelperViewHolder itemViewHolder = (ItemTouchHelperViewHolder) viewHolder; itemViewHolder.onItemSlideSlipStart(context); } super.onSelectedChanged(viewHolder, actionState); /*final boolean swiping = actionState == ItemTouchHelper.ACTION_STATE_SWIPE; swipeRefreshLayout.setEnabled(!swiping);*/ } @Override public void clearView(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) { super.clearView(recyclerView, viewHolder); // Tell the view holder it's time to restore the idle state ItemTouchHelperViewHolder itemViewHolder = (ItemTouchHelperViewHolder) viewHolder; itemViewHolder.onItemSlideSlipStop(context); } @Override public float getMoveThreshold(RecyclerView.ViewHolder viewHolder) { /*return super.getMoveThreshold(viewHolder);*/ return 0.1f; /*return super.getMoveThreshold(0.5f);*/ } @Override public float getSwipeThreshold(RecyclerView.ViewHolder viewHolder) { /*if (viewHolder instanceof RecyclerView.ViewHolder) { return 1f; } return super.getSwipeThreshold(viewHolder);*/ return 0.9f; } }
使用
布局
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/llBasicConfigChoose" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@drawable/white_gray_button_en_r_selector" android:descendantFocusability="blocksDescendants" android:orientation="vertical"> <RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:paddingBottom="@dimen/d18" android:paddingLeft="@dimen/d12" android:paddingRight="@dimen/d12" android:paddingTop="@dimen/d18"> <TextView android:id="@+id/tvBasicConfigChoose" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" android:textColor="@color/fontInput" android:textSize="@dimen/s14" tools:text="第一項目部" /> <ImageView android:id="@+id/ivBasicConfigChoose" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentEnd="true" android:layout_alignParentRight="true" android:layout_centerVertical="true" android:contentDescription="@string/toDo" android:src="@drawable/dehaze" android:visibility="invisible" /> <CheckBox android:id="@+id/cbBasicConfigChoose" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentEnd="true" android:layout_alignParentRight="true" android:layout_centerVertical="true" android:button="@drawable/checkbox_image_selector" /> </RelativeLayout> <View android:layout_width="match_parent" android:layout_height="@dimen/px1" android:layout_marginLeft="@dimen/d12" android:layout_marginStart="@dimen/d12" android:background="@color/gray" /> </LinearLayout>
適配器
package adapter.parameterconfiguration; import android.annotation.SuppressLint; import android.content.Context; import android.graphics.PorterDuff; import android.support.annotation.NonNull; import android.support.v4.content.ContextCompat; import android.support.v7.widget.RecyclerView; import android.view.LayoutInflater; import android.view.MotionEvent; import android.view.View; import android.view.ViewGroup; import android.widget.CheckBox; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.TextView; import com.self.zsp.dfs.R; import java.util.ArrayList; import java.util.Collections; import java.util.List; import entity.sync.Ground; import butterknife.BindView; import butterknife.ButterKnife; import slideslipdrag.ItemDragListener; import slideslipdrag.ItemTouchHelperAdapter; import slideslipdrag.ItemTouchHelperViewHolder; import util.ToastUtils; /** * Created on 2018/6/27. * * @desc 參數(shù)配置選擇(地層) */ public class ParaConfigChooseGroundAdapter extends RecyclerView.Adapter<ParaConfigChooseGroundAdapter.ViewHolder> implements View.OnClickListener, View.OnLongClickListener, ItemTouchHelperAdapter { private Context context; private List<Ground> groundList; private int viewStateReceive; private OnRecyclerViewItemClickListener mOnItemClickListener; private OnRecyclerViewItemLongClickListener mOnItemLongClickListener; private final ItemDragListener dragStartListener; private int selectPosition = -1; /** * constructor * * @param context 上下文 * @param viewState 視圖狀(0選擇1無選擇) * @param dragStartListener 拖拽監(jiān)聽 */ public ParaConfigChooseGroundAdapter(Context context, int viewState, ItemDragListener dragStartListener) { this.context = context; this.dragStartListener = dragStartListener; groundList = new ArrayList<>(); viewStateReceive = viewState; } private void setSelectPosition(int selectPosition) { this.selectPosition = selectPosition; } public void setParaConfigChooseGroundData(List<Ground> list) { this.groundList = list; } @Override public int getItemCount() { if (groundList != null && groundList.size() > 0) { return groundList.size(); } return 0; } @Override public void onClick(View v) { if (mOnItemClickListener != null) { // Method getTag which get data. mOnItemClickListener.onItemClick(v, (Ground) v.getTag()); } } @Override public boolean onLongClick(View v) { if (mOnItemLongClickListener != null) { // Method getTag which get data. mOnItemLongClickListener.onItemLongClick(v, (Ground) v.getTag()); } return true; } public void setOnItemClickListener(OnRecyclerViewItemClickListener listener) { this.mOnItemClickListener = listener; } public void setOnItemLongClickListener(OnRecyclerViewItemLongClickListener listener) { this.mOnItemLongClickListener = listener; } @NonNull @Override public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { // inflate(R.layout.list_item_record,parent,false)不這么寫則cardview不適寬 View view = LayoutInflater.from(context).inflate(R.layout.basic_config_choose_item, parent, false); // 為所創(chuàng)View注冊點擊事件 view.setOnClickListener(this); view.setOnLongClickListener(this); return new ViewHolder(view); } @SuppressLint("ClickableViewAccessibility") @Override public void onBindViewHolder(@NonNull final ViewHolder holder, int position) { final Ground ground = groundList.get(position); // 數(shù)據(jù)存itemView之Tag以便點獲 holder.itemView.setTag(ground); // 值 holder.tvBasicConfigChoose.setText(ground.getDcmc()); switch (viewStateReceive) { case 0: // 選否 if (selectPosition == position) { holder.cbBasicConfigChoose.setChecked(true); } else { holder.cbBasicConfigChoose.setChecked(false); } // CheckBox holder.cbBasicConfigChoose.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (selectPosition == holder.getAdapterPosition()) { // 選則取消 setSelectPosition(-1); mOnItemClickListener.onItemChoose(ground, false); } else { // 未選則選 setSelectPosition(holder.getAdapterPosition()); mOnItemClickListener.onItemChoose(ground, true); } notifyDataSetChanged(); } }); // ConvertView holder.itemView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (selectPosition == holder.getAdapterPosition()) { // 選則取消 setSelectPosition(-1); mOnItemClickListener.onItemChoose(ground, false); } else { // 未選則選 setSelectPosition(holder.getAdapterPosition()); mOnItemClickListener.onItemChoose(ground, true); } notifyDataSetChanged(); } }); break; case 1: holder.cbBasicConfigChoose.setVisibility(View.INVISIBLE); holder.ivBasicConfigChoose.setVisibility(View.VISIBLE); break; default: break; } holder.ivBasicConfigChoose.setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { switch (event.getAction()) { case MotionEvent.ACTION_DOWN: dragStartListener.onStartDrag(holder); break; default: break; } return false; } }); } @Override public void onItemMove(int fromPosition, int toPosition) { if (fromPosition < toPosition) { for (int i = fromPosition; i < toPosition; i++) { Collections.swap(groundList, i, i + 1); } } else { for (int i = fromPosition; i > toPosition; i--) { Collections.swap(groundList, i, i - 1); } } notifyItemMoved(fromPosition, toPosition); } @Override public void onItemSlideSlipEnd(int position) { Ground ground = groundList.get(position); switch (ground.getLocalAdd()) { case 1: notifyItemRemoved(position); groundList.remove(position); notifyItemRangeChanged(0, getItemCount()); ground.delete(); break; default: notifyItemRangeChanged(0, getItemCount()); ToastUtils.shortShow(context.getString(R.string.localMaintainedDataDeleteHint)); break; } } public interface OnRecyclerViewItemClickListener { /** * 短點 * * @param view 視圖 * @param ground 數(shù)據(jù) */ void onItemClick(View view, Ground ground); /** * 短點(選) * * @param ground 數(shù)據(jù) * @param choose 選否 */ void onItemChoose(Ground ground, boolean choose); } public interface OnRecyclerViewItemLongClickListener { /** * 長點 * * @param view 視圖 * @param ground 數(shù)據(jù) */ void onItemLongClick(View view, Ground ground); } /** * 自定ViewHolder(持每Item頁全元素) */ class ViewHolder extends RecyclerView.ViewHolder implements ItemTouchHelperViewHolder { @BindView(R.id.llBasicConfigChoose) LinearLayout llBasicConfigChoose; @BindView(R.id.tvBasicConfigChoose) TextView tvBasicConfigChoose; @BindView(R.id.ivBasicConfigChoose) ImageView ivBasicConfigChoose; @BindView(R.id.cbBasicConfigChoose) CheckBox cbBasicConfigChoose; ViewHolder(final View view) { super(view); ButterKnife.bind(this, view); } @Override public void onItemSlideSlipStart(Context context) { llBasicConfigChoose.setBackgroundColor(ContextCompat.getColor(context, R.color.blue)); tvBasicConfigChoose.setTextColor(ContextCompat.getColor(context, R.color.background)); ivBasicConfigChoose.setColorFilter(ContextCompat.getColor(context, R.color.background), PorterDuff.Mode.SRC_IN); } @Override public void onItemSlideSlipStop(Context context) { llBasicConfigChoose.setBackgroundColor(ContextCompat.getColor(context, R.color.background)); tvBasicConfigChoose.setTextColor(ContextCompat.getColor(context, R.color.fontInput)); ivBasicConfigChoose.setColorFilter(ContextCompat.getColor(context, R.color.blue), PorterDuff.Mode.SRC_IN); } } }
主代碼
package adapter.parameterconfiguration; import android.annotation.SuppressLint; import android.content.Context; import android.graphics.PorterDuff; import android.support.annotation.NonNull; import android.support.v4.content.ContextCompat; import android.support.v7.widget.RecyclerView; import android.view.LayoutInflater; import android.view.MotionEvent; import android.view.View; import android.view.ViewGroup; import android.widget.CheckBox; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.TextView; import com.self.zsp.dfs.R; import java.util.ArrayList; import java.util.Collections; import java.util.List; import entity.sync.Ground; import butterknife.BindView; import butterknife.ButterKnife; import slideslipdrag.ItemDragListener; import slideslipdrag.ItemTouchHelperAdapter; import slideslipdrag.ItemTouchHelperViewHolder; import util.ToastUtils; public class ParaConfigChooseGroundAdapter extends RecyclerView.Adapter<ParaConfigChooseGroundAdapter.ViewHolder> implements View.OnClickListener, View.OnLongClickListener, ItemTouchHelperAdapter { private Context context; private List<Ground> groundList; private int viewStateReceive; private OnRecyclerViewItemClickListener mOnItemClickListener; private OnRecyclerViewItemLongClickListener mOnItemLongClickListener; private final ItemDragListener dragStartListener; private int selectPosition = -1; /** * constructor * * @param context 上下文 * @param viewState 視圖狀(0選擇1無選擇) * @param dragStartListener 拖拽監(jiān)聽 */ public ParaConfigChooseGroundAdapter(Context context, int viewState, ItemDragListener dragStartListener) { this.context = context; this.dragStartListener = dragStartListener; groundList = new ArrayList<>(); viewStateReceive = viewState; } private void setSelectPosition(int selectPosition) { this.selectPosition = selectPosition; } public void setParaConfigChooseGroundData(List<Ground> list) { this.groundList = list; } @Override public int getItemCount() { if (groundList != null && groundList.size() > 0) { return groundList.size(); } return 0; } @Override public void onClick(View v) { if (mOnItemClickListener != null) { // Method getTag which get data. mOnItemClickListener.onItemClick(v, (Ground) v.getTag()); } } @Override public boolean onLongClick(View v) { if (mOnItemLongClickListener != null) { // Method getTag which get data. mOnItemLongClickListener.onItemLongClick(v, (Ground) v.getTag()); } return true; } public void setOnItemClickListener(OnRecyclerViewItemClickListener listener) { this.mOnItemClickListener = listener; } public void setOnItemLongClickListener(OnRecyclerViewItemLongClickListener listener) { this.mOnItemLongClickListener = listener; } @NonNull @Override public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { // inflate(R.layout.list_item_record,parent,false)不這么寫則cardview不適寬 View view = LayoutInflater.from(context).inflate(R.layout.basic_config_choose_item, parent, false); // 為所創(chuàng)View注冊點擊事件 view.setOnClickListener(this); view.setOnLongClickListener(this); return new ViewHolder(view); } @SuppressLint("ClickableViewAccessibility") @Override public void onBindViewHolder(@NonNull final ViewHolder holder, int position) { final Ground ground = groundList.get(position); // 數(shù)據(jù)存itemView之Tag以便點獲 holder.itemView.setTag(ground); // 值 holder.tvBasicConfigChoose.setText(ground.getDcmc()); switch (viewStateReceive) { case 0: // 選否 if (selectPosition == position) { holder.cbBasicConfigChoose.setChecked(true); } else { holder.cbBasicConfigChoose.setChecked(false); } // CheckBox holder.cbBasicConfigChoose.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (selectPosition == holder.getAdapterPosition()) { // 選則取消 setSelectPosition(-1); mOnItemClickListener.onItemChoose(ground, false); } else { // 未選則選 setSelectPosition(holder.getAdapterPosition()); mOnItemClickListener.onItemChoose(ground, true); } notifyDataSetChanged(); } }); // ConvertView holder.itemView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (selectPosition == holder.getAdapterPosition()) { // 選則取消 setSelectPosition(-1); mOnItemClickListener.onItemChoose(ground, false); } else { // 未選則選 setSelectPosition(holder.getAdapterPosition()); mOnItemClickListener.onItemChoose(ground, true); } notifyDataSetChanged(); } }); break; case 1: holder.cbBasicConfigChoose.setVisibility(View.INVISIBLE); holder.ivBasicConfigChoose.setVisibility(View.VISIBLE); break; default: break; } holder.ivBasicConfigChoose.setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { switch (event.getAction()) { case MotionEvent.ACTION_DOWN: dragStartListener.onStartDrag(holder); break; default: break; } return false; } }); } @Override public void onItemMove(int fromPosition, int toPosition) { if (fromPosition < toPosition) { for (int i = fromPosition; i < toPosition; i++) { Collections.swap(groundList, i, i + 1); } } else { for (int i = fromPosition; i > toPosition; i--) { Collections.swap(groundList, i, i - 1); } } notifyItemMoved(fromPosition, toPosition); } @Override public void onItemSlideSlipEnd(int position) { Ground ground = groundList.get(position); switch (ground.getLocalAdd()) { case 1: notifyItemRemoved(position); groundList.remove(position); notifyItemRangeChanged(0, getItemCount()); ground.delete(); break; default: notifyItemRangeChanged(0, getItemCount()); ToastUtils.shortShow(context.getString(R.string.localMaintainedDataDeleteHint)); break; } } public interface OnRecyclerViewItemClickListener { /** * 短點 * * @param view 視圖 * @param ground 數(shù)據(jù) */ void onItemClick(View view, Ground ground); /** * 短點(選) * * @param ground 數(shù)據(jù) * @param choose 選否 */ void onItemChoose(Ground ground, boolean choose); } public interface OnRecyclerViewItemLongClickListener { /** * 長點 * * @param view 視圖 * @param ground 數(shù)據(jù) */ void onItemLongClick(View view, Ground ground); } /** * 自定ViewHolder(持每Item頁全元素) */ class ViewHolder extends RecyclerView.ViewHolder implements ItemTouchHelperViewHolder { @BindView(R.id.llBasicConfigChoose) LinearLayout llBasicConfigChoose; @BindView(R.id.tvBasicConfigChoose) TextView tvBasicConfigChoose; @BindView(R.id.ivBasicConfigChoose) ImageView ivBasicConfigChoose; @BindView(R.id.cbBasicConfigChoose) CheckBox cbBasicConfigChoose; ViewHolder(final View view) { super(view); ButterKnife.bind(this, view); } @Override public void onItemSlideSlipStart(Context context) { llBasicConfigChoose.setBackgroundColor(ContextCompat.getColor(context, R.color.blue)); tvBasicConfigChoose.setTextColor(ContextCompat.getColor(context, R.color.background)); ivBasicConfigChoose.setColorFilter(ContextCompat.getColor(context, R.color.background), PorterDuff.Mode.SRC_IN); } @Override public void onItemSlideSlipStop(Context context) { llBasicConfigChoose.setBackgroundColor(ContextCompat.getColor(context, R.color.background)); tvBasicConfigChoose.setTextColor(ContextCompat.getColor(context, R.color.fontInput)); ivBasicConfigChoose.setColorFilter(ContextCompat.getColor(context, R.color.blue), PorterDuff.Mode.SRC_IN); } } }
關(guān)于使用RecyclerView怎么實現(xiàn)一個側(cè)滑拖拽功能就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。
免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。