Android之RecycleView实现指定范围的拖动效果
1 问题
在RecycleView里面实现指定位置的拖动效果,(这里是实现线性布局的,不是网格布局的)
@Override
public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) {
Log.i(TAG, "onMove viewHolder.getAdapterPosition() is:" + viewHolder.getAdapterPosition());
int itemViewType = viewHolder.getAdapterPosition();
Log.i(TAG, "onMove---------------------------------itemViewType is:" + itemViewType);
// if (itemViewType == 0 || itemViewType == 1 || itemViewType == 16) {
// Log.i(TAG, "onMove---------------------------------itemViewType is:" + itemViewType);
// return false;
// }
int fromPosition = viewHolder.getAdapterPosition();//得到拖动ViewHolder的position
int toPosition = target.getAdapterPosition();//得到目标ViewHolder的position
Log.i(TAG, "onMove---------------------------------fromPosition is:" + fromPosition);
Log.i(TAG, "onMove---------------------------------toPosition is:" + toPosition);
//这里的position如果不是指定范围内的话,直接返回false,就拖动不到其它位置了
if (DragUtil.INSTANCE.getProhibitDragIndexs().contains(toPosition)) {
return false;
}
itemTouchAdapter.onMove(fromPosition,toPosition);
return true;
}
2 关键代码如下
MyItemTouchCallback.java
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.util.Log;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.ItemTouchHelper;
import androidx.recyclerview.widget.RecyclerView;
import com.appsinnova.android.keepdrop.manager.FileScanManager;
/**
* Created by Administrator on 2016/4/12.
*/
public class MyItemTouchCallback extends ItemTouchHelper.Callback {
public static final String TAG = MyItemTouchCallback.class.getSimpleName();
private ItemTouchAdapter itemTouchAdapter;
public MyItemTouchCallback(ItemTouchAdapter itemTouchAdapter){
this.itemTouchAdapter = itemTouchAdapter;
}
@Override
public boolean isLongPressDragEnabled() {
return false;
}
@Override
public boolean isItemViewSwipeEnabled() {
return true;
}
@Override
public int getMovementFlags(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
Log.i(TAG, "getMovementFlags viewHolder.getAdapterPosition() is:" + viewHolder.getAdapterPosition());
int itemViewType = viewHolder.getAdapterPosition();
Log.i(TAG, "---------------------------------itemViewType is:" + itemViewType);
// if (itemViewType == 0 || itemViewType == 1 || itemViewType == 16) {
// Log.i(TAG, "-getMovementFlags--------------------------------itemViewType is:" + itemViewType);
// return makeMovementFlags(0, 0);
// }
if (recyclerView.getLayoutManager() instanceof GridLayoutManager) {
final int dragFlags = ItemTouchHelper.UP | ItemTouchHelper.DOWN | ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT;
final int swipeFlags = 0;
return makeMovementFlags(dragFlags, swipeFlags);
} else {
final int dragFlags = ItemTouchHelper.UP | ItemTouchHelper.DOWN;
//final int swipeFlags = ItemTouchHelper.START | ItemTouchHelper.END;
final int swipeFlags = 0;
Log.i(TAG, "makeMovementFlags start");
return makeMovementFlags(dragFlags, swipeFlags);
}
}
@Override
public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) {
Log.i(TAG, "onMove viewHolder.getAdapterPosition() is:" + viewHolder.getAdapterPosition());
int itemViewType = viewHolder.getAdapterPosition();
Log.i(TAG, "onMove---------------------------------itemViewType is:" + itemViewType);
// if (itemViewType == 0 || itemViewType == 1 || itemViewType == 16) {
// Log.i(TAG, "onMove---------------------------------itemViewType is:" + itemViewType);
// return false;
// }
int fromPosition = viewHolder.getAdapterPosition();//得到拖动ViewHolder的position
int toPosition = target.getAdapterPosition();//得到目标ViewHolder的position
Log.i(TAG, "onMove---------------------------------fromPosition is:" + fromPosition);
Log.i(TAG, "onMove---------------------------------toPosition is:" + toPosition);
//这里的position如果不是指定范围内的话,直接返回false,就拖动不到其它位置了
if (DragUtil.INSTANCE.getProhibitDragIndexs().contains(toPosition)) {
return false;
}
itemTouchAdapter.onMove(fromPosition,toPosition);
return true;
}
@Override
public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {
int position = viewHolder.getAdapterPosition();
itemTouchAdapter.onSwiped(position);
}
@Override
public void onChildDraw(Canvas c, RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, float dX, float dY, int actionState, boolean isCurrentlyActive) {
if (actionState == ItemTouchHelper.ACTION_STATE_SWIPE) {
//滑动时改变Item的透明度
final float alpha = 1 - Math.abs(dX) / (float) viewHolder.itemView.getWidth();
viewHolder.itemView.setAlpha(alpha);
viewHolder.itemView.setTranslationX(dX);
} else {
super.onChildDraw(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive);
}
}
@Override
public void onSelectedChanged(RecyclerView.ViewHolder viewHolder, int actionState) {
if (actionState != ItemTouchHelper.ACTION_STATE_IDLE) {
if (background == null && bkcolor == -1) {
Drawable drawable = viewHolder.itemView.getBackground();
if (drawable == null) {
bkcolor = 0;
} else {
background = drawable;
}
}
viewHolder.itemView.setBackgroundColor(Color.LTGRAY);
}
super.onSelectedChanged(viewHolder, actionState);
}
@Override
public void clearView(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
super.clearView(recyclerView, viewHolder);
viewHolder.itemView.setAlpha(1.0f);
if (background != null) viewHolder.itemView.setBackgroundDrawable(background);
if (bkcolor != -1) viewHolder.itemView.setBackgroundColor(bkcolor);
//viewHolder.itemView.setBackgroundColor(0);
if (onDragListener!=null){
onDragListener.onFinishDrag();
}
}
private Drawable background = null;
private int bkcolor = -1;
private OnDragListener onDragListener;
public MyItemTouchCallback setOnDragListener(OnDragListener onDragListener) {
this.onDragListener = onDragListener;
return this;
}
public interface OnDragListener{
void onFinishDrag();
}
public interface ItemTouchAdapter {
void onMove(int fromPosition, int toPosition);
void onSwiped(int position);
}
}
OnRecyclerItemClickListener.java
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import androidx.core.view.GestureDetectorCompat;
import androidx.recyclerview.widget.RecyclerView;
/**
* Created by Administrator on 2016/4/14.
*/
public class OnRecyclerItemClickListener implements RecyclerView.OnItemTouchListener {
private GestureDetectorCompat mGestureDetector;
private RecyclerView recyclerView;
public OnRecyclerItemClickListener(RecyclerView recyclerView){
this.recyclerView = recyclerView;
mGestureDetector = new GestureDetectorCompat(recyclerView.getContext(),new ItemTouchHelperGestureListener());
}
@Override
public boolean onInterceptTouchEvent(RecyclerView rv, MotionEvent e) {
mGestureDetector.onTouchEvent(e);
return false;
}
@Override
public void onTouchEvent(RecyclerView rv, MotionEvent e) {
mGestureDetector.onTouchEvent(e);
}
@Override
public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) {
}
private class ItemTouchHelperGestureListener extends GestureDetector.SimpleOnGestureListener {
@Override
public boolean onSingleTapUp(MotionEvent e) {
View child = recyclerView.findChildViewUnder(e.getX(), e.getY());
if (child!=null) {
RecyclerView.ViewHolder vh = recyclerView.getChildViewHolder(child);
onItemClick(vh);
}
return true;
}
@Override
public void onLongPress(MotionEvent e) {
View child = recyclerView.findChildViewUnder(e.getX(), e.getY());
if (child!=null) {
RecyclerView.ViewHolder vh = recyclerView.getChildViewHolder(child);
onLongClick(vh);
}
}
}
public void onLongClick(RecyclerView.ViewHolder vh){}
public void onItemClick(RecyclerView.ViewHolder vh){}
}
适配器里面实现接口
MyItemTouchCallback.ItemTouchAdapter
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import org.greenrobot.eventbus.EventBus;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import static com.appsinnova.android.keepdrop.recommend.entity.ChannelHead.channelType;
public class ChannelManagerAdapter extends BaseMultiItemQuickAdapter<MultiItemEntity, BaseViewHolder> implements MyItemTouchCallback.ItemTouchAdapter {
public static final String TAG = ChannelManagerAdapter.class.getSimpleName();
public static final int MY_VEDIO_TYPE = 0;
public static final int MORE_VEDIO_TYPE = 1;
public static final int CAN_EDITABLE = 1;
public static final int CAN_NOT_EDITABLE = 0;
public List<MultiItemEntity> results = null;
public ChannelManagerAdapter(List<MultiItemEntity> data) {
super(data);
addItemType(ChannelHead.channelHeadType, R.layout.item_channel_head);
addItemType(channelType, R.layout.item_channel);
this.results = data;
}
@Override
protected void convert(BaseViewHolder helper, MultiItemEntity item) {
switch (helper.getItemViewType()) {
}
}
@Override
public void onMove(int fromPosition, int toPosition) {
if (fromPosition==results.size()-1 || toPosition==results.size()-1){
return;
}
if (fromPosition < toPosition) {
for (int i = fromPosition; i < toPosition; i++) {
Collections.swap(results, i, i + 1);
}
} else {
for (int i = fromPosition; i > toPosition; i--) {
Collections.swap(results, i, i - 1);
}
}
//这个数据是移动后的数据,会更新原始数据
int size = results.size();
List<RecommendVedioModel.RecommendVedioItem> items = new ArrayList<RecommendVedioModel.RecommendVedioItem>();
RecommendVedioModel recommendVedioModel = SPHelper.getInstance().getObject(SpConstants.RECOMMEND_VEDIO_CATETGORY, RecommendVedioModel.class);
for (int i = 0; i < size; ++i) {
if (results.get(i).getItemType() == channelType) {
RecommendVedioModel.RecommendVedioItem item = (RecommendVedioModel.RecommendVedioItem)results.get(i);
items.add(item);
}
}
if (recommendVedioModel != null) {
recommendVedioModel.items = items;
} else {
Log.i(TAG, "recommendVedioModel is null");
}
SPHelper.getInstance().putObject(SpConstants.RECOMMEND_VEDIO_CATETGORY, recommendVedioModel);
notifyItemMoved(fromPosition, toPosition);
}
@Override
public void onSwiped(int position) {
}
}
然后在页面显示的地方
channelManagerAdapter = ChannelManagerAdapter(allDatas)
val layoutManager = LinearLayoutManager(this)
layoutManager.orientation = LinearLayoutManager.VERTICAL
channelRecycleview.setLayoutManager(layoutManager)
channelRecycleview.setAdapter(channelManagerAdapter)
val itemTouchHelper = ItemTouchHelper(MyItemTouchCallback(channelManagerAdapter))
itemTouchHelper.attachToRecyclerView(channelRecycleview)
channelRecycleview.addOnItemTouchListener(object : OnRecyclerItemClickListener(channelRecycleview) {
override fun onLongClick(vh: RecyclerView.ViewHolder) {
LogUtil.i(TAG, "onLongClick vh.layoutPosition is:" + vh.layoutPosition)
var index = vh.layoutPosition
if (!prohibitDragIndexs.contains(index)) {
itemTouchHelper.startDrag(vh)
}
}
})