其他分享
首页 > 其他分享> > android-如何在RecyclerView上获得点击(不是孩子)

android-如何在RecyclerView上获得点击(不是孩子)

作者:互联网

有什么方法可以在RecyclerView上设置onClickListener吗?

我有一个RecyclerView,其中有一些子项,并在父RecyclerView上设置了OnClickListener.但是,当我单击该视图时,不会触发onClick.请参见下面的示例代码-我们希望点击父级而不是子级.在这种情况下,我们不关心项目的点击.

我尝试对孩子进行setFocusable(false),setClickable(false)和setOnClickListener(null)无效.无论如何,我都不认为孩子会窃取父母的点击,因为当我单击没有孩子的区域时,这些点击也不会注册.

package com.formagrid.hellotest;

import android.app.Activity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.util.Arrays;
import java.util.List;

public class HelloActivity extends Activity {

    private RecyclerView mRecyclerView;
    private RecyclerAdapter mAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_hello);

        mRecyclerView = (RecyclerView) findViewById(R.id.recycler_view);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        mAdapter = new RecyclerAdapter(Arrays.asList("hi", "this", "is", "some", "text"));
        mRecyclerView.setAdapter(mAdapter);
        mRecyclerView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Log.d("patricia", view.toString());
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    @Override
    protected void onPause() {
        super.onPause();
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.Holder> {

        public class Holder extends RecyclerView.ViewHolder {

            protected TextView textView;

            public Holder(TextView itemView) {
                super(itemView);
                this.textView = itemView;
            }

        }

        private List<String> contents;

        public RecyclerAdapter(List<String> contents) {
            this.contents = contents;
        }

        @Override
        public Holder onCreateViewHolder(ViewGroup parent, int viewType) {
            return new Holder(new TextView(parent.getContext()));
        }

        @Override
        public void onBindViewHolder(Holder holder, int position) {
            holder.textView.setText(contents.get(position));
        }

        @Override
        public int getItemCount() {
            return contents.size();
        }

    }

}

解决方法:

Is there any way to set an onClickListener on a RecyclerView?

不能.也就是说,您可以设置OnClickListener,但是RecyclerView永远不会调用它. RecyclerView拦截所有触摸事件,但从不调用performClick(),这是View调用其侦听器的方式.

但是,您可以使用OnTouchListener和GestureDetector模拟OnClickListener.对于GestureDetector的侦听器,我们可以使用SimpleOnGestureListener,仅实现onSingleTapUp()方法.

class ClickListener extends GestureDetector.SimpleOnGestureListener {
    @Override
    public boolean onSingleTapUp(MotionEvent e) {
        Toast.makeText(HelloActivity.this, "Clicked", 0).show();
        return true;
    }
};

然后,我们只需要从OnTouchListener传入GestureDetector和MotionEvent,然后检查返回值来决定是否使用该事件,以免干扰滚动,拖动等.

final GestureDetector detector = new GestureDetector(HelloActivity.this, new ClickListener());

mRecyclerView.setOnTouchListener(new OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            if(detector.onTouchEvent(event)) {
                return true;
            }
            return false;
        }
    }
);

请注意,上述解决方案仅适用于“简单”的RecyclerView,就像问题中描述和给出的那样.如果您正在使用涉及更多的项目处理(例如拖放或滑动),则需要在触摸事件链的更远处处理手势检测.

为此,我们可以将RecyclerView子类化,并在dispatchTouchEvent()方法中执行检测.如果检测到单击,我们只需调用performClick(),它将触发RecyclerView的OnClickListener.

public class ClickableRecyclerView extends RecyclerView {

    private final GestureDetectorCompat detector;

    public ClickableRecyclerView(Context context) {
        this(context, null);
    }

    public ClickableRecyclerView(Context context, AttributeSet attrs) {
        super(context, attrs);
        detector = new GestureDetectorCompat(context, new ClickListener());
    }

    private class ClickListener extends GestureDetector.SimpleOnGestureListener {
        @Override
        public boolean onSingleTapUp(MotionEvent e) {
            performClick();
            return true;
        }
    };

    @Override
    public boolean dispatchTouchEvent(MotionEvent e) {
        detector.onTouchEvent(e);
        return super.dispatchTouchEvent(e);
    }
}

只需使用此子类代替常规的RecyclerView,然后像往常一样在其上设置一个OnClickListener.根据您如何实例化此方法,可能需要其他构造函数.

标签:android,android-recyclerview,android-layout,onclicklistener,recyclerview-layout
来源: https://codeday.me/bug/20191010/1887347.html