Android Recyclerview Multiple onclick items

2020-07-27 06:01发布

问题:

I am trying to have a recyclerview where each item is a simple TextView + Checkbox

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/title"
    android:layout_width="0dp"
    android:layout_weight="5"
    android:layout_height="wrap_content"
    />
<CheckBox
    android:id="@+id/chk"
    android:layout_width="0dp"
    android:layout_weight="1"
    android:layout_height="wrap_content" />
</LinearLayout>

What I am trying to achieve is to (un)check the checkbox if it is clicked. But if the textview is clicked, then do the ripple animation on the entire item and start a fragment transition.

I couldn't find an example online and the closest that I found was RecyclerView onClick

I managed to get the onclick to function however, in the code

@Override public boolean onInterceptTouchEvent(RecyclerView view, MotionEvent e) {
View childView = view.findChildViewUnder(e.getX(), e.getY());
if (childView != null && mListener != null && mGestureDetector.onTouchEvent(e)) {
  mListener.onItemClick(childView, view.getChildPosition(childView));
  return true;
}
return false;

}

The problem I have is that the childView returned is the linearlayout and not the textview or checkbox. I am thus unable to determine whether the click was done on the textview or the checkbox.

Is there something that I am missing? Ideally if someone has a working example of how to implement this, that would be great.

Thanks.

回答1:

Here you go:

Your list item xml:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:background="?attr/selectableItemBackground"
android:layout_height="72dp">

<TextView
    android:id="@+id/title"
    android:layout_width="wrap_content"
    android:text="My Text"
    android:layout_gravity="left|center_vertical"
    android:paddingLeft="16dp"
    android:layout_height="wrap_content" />

<CheckBox
    android:id="@+id/checkbox"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginRight="16dp"
    android:layout_gravity="right|center_vertical" />

(I've used a FrameLayout as root but a LinearLayout should possibly work as well.)

And in the Adapter of your RecyclerView use something like this:

            @Override
            public YourViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
                View itemView = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.list_item, viewGroup, false);

                itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Log.w("Test", "Parent clicked");
                    }
                });
                return new YourViewHolder(itemView);

            }

            @Override
            public void onBindViewHolder(YourViewHolder viewHolder, int i) {

                viewHolder.checkBox.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Log.w("Test", "Checkbox clicked");
                    }
                });
            }