1、效果圖

select.gif
2、開始實(shí)現(xiàn),首先自定義一個(gè)RecyclerView,然后重寫dispatchTouchEvent()方法,在里面添加一個(gè)監(jiān)聽器,監(jiān)聽滑動(dòng)事件。代碼如下:
package com.quanten.view;
import android.content.Context;
import android.support.annotation.Nullable;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
import android.view.MotionEvent;
/**
* 文 件 名: PhotoRecyclerView
* 版 權(quán): Quanten Team. Copyright YYYY-YYYY, All rights reserved
* 描 述: <描述>
* 修 改 人: LLF
* 修改時(shí)間: 2018/9/6 0006
* 跟蹤單號(hào): <跟蹤單號(hào)>
* 修改單號(hào): <修改單號(hào)>
* 修改內(nèi)容: <修改內(nèi)容>
*/
public class PhotoRecyclerView extends RecyclerView {
private OnDispatchTouchListener mOnDispatchTouchListener;
public PhotoRecyclerView(Context context) {
this(context,null);
}
public PhotoRecyclerView(Context context, @Nullable AttributeSet attrs) {
this(context, attrs,0);
}
public PhotoRecyclerView(Context context, @Nullable AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@Override
public boolean dispatchTouchEvent(final MotionEvent event) {
if(mOnDispatchTouchListener != null)
{
mOnDispatchTouchListener.onDispatchTouch(event);
}
return super.dispatchTouchEvent(event);
}
public void setmOnDispatchTouchListener(OnDispatchTouchListener mOnDispatchTouchListener) {
this.mOnDispatchTouchListener = mOnDispatchTouchListener;
}
public interface OnDispatchTouchListener{
void onDispatchTouch(MotionEvent event);
}
}
3、在圖片選擇頁面監(jiān)聽用戶選擇圖片事件,然后更新圖片選擇狀態(tài)即可,主要代碼如下:
recyclerView.setmOnDispatchTouchListener(new PhotoRecyclerView.OnDispatchTouchListener()
{
@Override
public void onDispatchTouch(MotionEvent event)
{
switch (event.getAction())
{
case MotionEvent.ACTION_DOWN:
// 獲取按下時(shí)的位置,x,y
selectPics.clear();
break;
case MotionEvent.ACTION_MOVE:
View v = recyclerView.findChildViewUnder(event.getX(), event.getY());
if (v != null && !selectPics.contains(v.getTag()))
{
Log.d("PhotoSelectActivity", " getId=" + v.getTag());
Integer index=(Integer) v.getTag();
selectPics.add(index);
mAdapter.selectedPic(null, imageFolderBeanList.get(index), index);
}
break;
case MotionEvent.ACTION_UP:
selectPics.clear();
break;
}
}
});
4、代碼已上傳百度云,如有問題,歡迎評(píng)論!源碼鏈接:https://pan.baidu.com/s/1MOdtjOZfkBoiGC_-twk1Cw