Android實(shí)現(xiàn)滑動(dòng)選擇圖片

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

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容