亚洲激情专区-91九色丨porny丨老师-久久久久久久女国产乱让韩-国产精品午夜小视频观看

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

Android中RecycleView如何實現指定范圍的拖動效果

發布時間:2020-07-16 17:26:20 來源:億速云 閱讀:184 作者:小豬 欄目:開發技術

小編這次要給大家分享的是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)
        }
      }
    })

看完這篇關于Android中RecycleView如何實現指定范圍的拖動效果的文章,如果覺得文章內容寫得不錯的話,可以把它分享出去給更多人看到。

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

诸城市| 太保市| 加查县| 内江市| 镶黄旗| 宁陵县| 广宗县| 五原县| 邢台县| 长顺县| 荔波县| 康平县| 定结县| 岱山县| 麻阳| 威远县| 元氏县| 都昌县| 汉沽区| 绩溪县| 平邑县| 当阳市| 广汉市| 商洛市| 晋江市| 盐城市| 宁海县| 水城县| 雷波县| 弥勒县| 瓮安县| 新化县| 元谋县| 比如县| 积石山| 阳江市| 壤塘县| 西青区| 江孜县| 临西县| 隆尧县|