RecyclerView实现侧滑拖拽功能

本文实例为大家分享了RecyclerView实现侧滑拖拽功能的具体代码,供大家参考,具体内容如下

准备

ItemDragListener

package slideslipdrag;

import android.support.v7.widget.RecyclerView;

/**
 * Created on 2018/7/22.
 *
 * @desc ItemDragListener
 */
public interface ItemDragListener {
 /**
  * 拖拽
  *
  * @param viewHolder viewHolder
  */
 void onStartDrag(RecyclerView.ViewHolder viewHolder);
}

ItemTouchHelperAdapter

package slideslipdrag;

/**
 * @decs: ItemTouchHelperAdapter
 * @date: 2018/7/22 13:23
 * @version: v 1.0
 */
public interface ItemTouchHelperAdapter {
 /**
  * 移动
  *
  * @param fromPosition 起始位
  * @param toPosition 终止位
  */
 void onItemMove(int fromPosition, int toPosition);

 /**
  * 侧滑结束
  *
  * @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 {
  /**
   * 侧滑开始
   *
   * @param context 上下文
   */
  void onItemSlideSlipStart(Context context);

  /**
   * 侧滑停止
   *
   * @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 参数配置选择(地层)
 */
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 拖拽监听
   */
  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);
    // 为所创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);
    // 数据存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 数据
     */
    void onItemClick(View view, Ground ground);

    /**
     * 短点(选)
     *
     * @param ground 数据
     * @param choose 选否
     */
    void onItemChoose(Ground ground, boolean choose);
  }

  public interface OnRecyclerViewItemLongClickListener {
    /**
     * 长点
     *
     * @param view  视图
     * @param ground 数据
     */
    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;

/**
 * Created on 2018/6/27.
 *
 * @author 郑少鹏
 * @desc 参数配置选择(地层)
 */
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 拖拽监听
   */
  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);
    // 为所创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);
    // 数据存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 数据
     */
    void onItemClick(View view, Ground ground);

    /**
     * 短点(选)
     *
     * @param ground 数据
     * @param choose 选否
     */
    void onItemChoose(Ground ground, boolean choose);
  }

  public interface OnRecyclerViewItemLongClickListener {
    /**
     * 长点
     *
     * @param view  视图
     * @param ground 数据
     */
    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);
    }
  }
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。

(0)

相关推荐

  • android RecyclerView侧滑菜单,滑动删除,长按拖拽,下拉刷新上拉加载

    本文介绍的库中的侧滑效果借鉴自SwipeMenu,并对SipwMenu的源码做了修改与Bug修复,然后才开发出的SwipeRecyclerView. 需要说明的是,本库没有对RecyclerView做大的修改,只是ItemView的封装.看起来是对RecyclerView的修改,其实仅仅是为RecyclerView添加了使用的方法API而已. 本库已经更新了三个版本了,会一直维护下去,根据小伙伴的要求,以后也会添加一些其它功能. SwipeRecyclerView将完美解决这些问题: 以下功能全

  • Android recyclerview实现拖拽排序和侧滑删除

    Recyclerview现在基本已经替代Listview了,RecyclerView也越来越好用了  当我们有实现条目的拖拽排序和侧滑删除时  可以直接时候Recyclerview提供的API就可以直接实现了 先贴上主要代码 private void initveiw() { ArrayList<String> items = new ArrayList<>(Arrays.asList("itme1", "item2", "itme

  • android的RecyclerView实现拖拽排序和侧滑删除示例

    在平时开发应用的时候,经常会遇到列表排序.滑动删除的需求.如果列表效果采用的是 ListView 的话,需要经过自定义 View 才能实现效果:但是如果采用的是 RecyclerView 的话,系统 API 就已经为我们提供了相应的功能. 接下来,我们就来看一下怎么用系统 API 来实现排序和删除的效果. 创建 ItemTouchHelper 创建一个 ItemTouchHelper 对象,然后其调用 attachToRecyclerView 方法: RecyclerView recyclerV

  • RecyclerView进阶:使用ItemTouchHelper实现拖拽和侧滑删除效果

    前言 现在RecyclerView的应用越来越广泛了,不同的应用场景需要其作出不同的改变.有时候我们可能需要实现侧滑删除的功能,比如知乎首页的侧滑删除,又或者长按Item进行拖动与其他Item进行位置的交换,但RecyclerView没有提供现成的API供我们操作,所幸SDK提供了ItemTouchHelper这样一个工具类帮助我们快速实现以上功能.不多说别的,我们来介绍一下ItemTouchHelper. 什么是ItemTouchHelper This is a utility class t

  • RecyclerView实现侧滑拖拽功能

    本文实例为大家分享了RecyclerView实现侧滑拖拽功能的具体代码,供大家参考,具体内容如下 准备 ItemDragListener package slideslipdrag; import android.support.v7.widget.RecyclerView; /** * Created on 2018/7/22. * * @desc ItemDragListener */ public interface ItemDragListener { /** * 拖拽 * * @par

  • Android利用RecyclerView实现全选、置顶和拖拽功能示例

    前言 今天给大家分享是如何在RecyclerView实现全选,ItemTouchHelper实现侧滑删除,拖拽功能.比较基础.关于RecyclerView的强大,就不多说了.在Android L SDK发布的新API中最有意思的就是RecyclerView 和 CardView了, 按照官方的说法, RecyclerView 一个ListView 的一个更高级更灵活的一个版本, 可以自定义的东西太多了. 效果: RecyclerView实现全选,ItemTouchHelper实现侧滑删除,拖拽功

  • Android中RecyclerView实现滑动删除与拖拽功能

    前言 从Android 5.0开始,谷歌推出了新的控件RecyclerView,相对于早它之前的ListView,优点多多,功能强大,也给我们的开发着提供了极大的便利,今天自己学习一下RecyclerView轻松实现滑动删除及拖拽的效果. 如下图. 相信研究过RecyclerView的同学,应该很清楚该怎么实现这样的效果,若是用ListView,这样的效果实现起来可能就有点麻烦,但是在强大的RecyclerView面前这样的的效果只需很少的代码,因为谷歌给我们提供了强大的工具类ItemTouch

  • Android用RecyclerView实现图标拖拽排序以及增删管理

    1. 效果图 2. 基本的功能 在非编辑状态下可以直接点击图标进行跳转 在编辑状态可以拖拽.添加.删除操作 已被添加过的不能再次添加 3. 实现的思路 用两个RecyclerView实现,同时维护两个数据源,上部是常用应用,最多可以放8个:下部是全部应用. 1. 每个应用图标的状态用枚举Option表示 // 当前的操作状态 enum class Option { ADD, REMOVE, NONE } 2. 在处于编辑状态时创建ItemTouchHelper对象并attch到RecyclerV

  • JS基于面向对象实现的拖拽功能示例

    本文实例讲述了JS基于面向对象实现的拖拽功能.分享给大家供大家参考,具体如下: <html xmlns="http://www.w3.org/1999/xhtml"> <head> <style> #div1 {width:100px; height:100px; background:red; position:absolute;} #div2 {width:100px; height:100px; background:yellow; posit

  • java swing中实现拖拽功能示例

    java实现拖拽示例 Swing中实现拖拽功能,代码很简单,都有注释,自己看,运行效果如下图: 复制代码 代码如下: package com; import java.awt.*;import java.awt.datatransfer.DataFlavor;import java.awt.dnd.DnDConstants;import java.awt.dnd.DropTarget;import java.awt.dnd.DropTargetAdapter;import java.awt.dn

  • Swift下使用UICollectionView 实现长按拖拽功能

    导读 简单用Swift写了一个collectionview的拖拽点击排序效果; 拖拽排序是新闻类的App可以说是必有的交互设计,如今日头条,网易新闻等. GitHub地址:https://github.com/wangliujiayou/Swift-dragLabel 欢迎Star. 效果 主要代码 手势长按移动 1.给CollectionViewCell添加一个长按手势. private lazy var collectionView: UICollectionView = { let clv

  • 原生js实现弹出层登录拖拽功能

    在WEB开发过程中,总会遇到一些从未接触过的需求,总是想尽一切办法去研究,最终实现效果,在实现效果的那一刻成就感爆棚,有木有? 留言墙.弹出框等一些常见地方都有拖拽功能,方便用户体验嘛. 实现拖拽功能 ,三个事件 mousemove , mouseup ,mousedown, 偏移量(offsetLeft, offsetTop , offsetWidth ,offsetHeight),窗口坐标位置(clientX ,clientY ) 以及获取可视区域方法的兼容性处理. 之前做的比较多的留言墙效

  • 基于JavaScript实现右键菜单和拖拽功能

    下面先给大家介绍下js实现的右键菜单功能,具体详情如下所示: 这一章解决的问题 1.实现右键菜单功能代码. 2.阻止默认事件的实际应用. <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>右键菜单</title> <style type="text/css"> #menu {

  • vue2.0使用Sortable.js实现的拖拽功能示例

    简介 在使用vue1.x之前的版本的时候,页面中的拖拽功能,我在项目中是直接用的jquery ui中的sortable.js,只是在拖拽完成后,在update的回调函数中又重新排序了存放数据的数组.但是当把vue升级到2.0以上后发现拖拽功能失效了,于是使用了下面代码. 该案例主要是在用于vuejs2.0中实现的拖拽功能,用到的的js有Sortable.js,vuedraggable.js,当然还有vue.min.js,提供的案例使用的require.js加载. 实现效果 实现后的效果如图所示:

随机推荐