Home > Enterprise >  How can I have an onClick event on a recylerview item when using a SelectionTracker with the adapter
How can I have an onClick event on a recylerview item when using a SelectionTracker with the adapter

Time:06-23

I am using the following .xml for my item which is used in my recyclerview to list each item:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="horizontal">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:id="@ id/card"
        android:layout_width="match_parent"
        android:layout_height="113dp"
        android:layout_marginTop="2dp"
        android:layout_marginBottom="2dp"
        android:background="@drawable/item_background">

        <TextView
            android:id="@ id/familybonus"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="16dp"
            android:text="TextView"
            app:layout_constraintBottom_toBottomOf="@ id/price"
            app:layout_constraintStart_toEndOf="@ id/price"
            app:layout_constraintTop_toTopOf="@ id/price" />

        <TextView
            android:id="@ id/price"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="16dp"
            android:layout_marginBottom="3dp"
            android:background="@drawable/price_background"
            android:foregroundGravity="center"
            android:gravity="center"
            android:textAppearance="?attr/textAppearanceListItem"
            android:textColor="@android:color/white"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@ id/name" />

        <TextView
            android:id="@ id/name"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="@dimen/text_margin"
            android:layout_marginTop="23dp"
            android:layout_marginBottom="29dp"
            android:foregroundGravity="center"
            android:gravity="center"
            android:textAppearance="?attr/textAppearanceListItem"
            app:layout_constraintBottom_toTopOf="@ id/price"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.5"
            app:layout_constraintStart_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintVertical_bias="0.0" />

        <TextView
            android:id="@ id/vp"
            android:layout_width="40dp"
            android:layout_height="40dp"
            android:layout_marginEnd="16dp"
            android:layout_weight="1"
            android:background="@drawable/ic_shield"
            android:gravity="center"
            android:text="VP"
            android:textStyle="bold"
            app:layout_constraintBottom_toBottomOf="@ id/price"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintTop_toTopOf="@ id/price"
            app:layout_constraintVertical_bias="1.0" />

    </androidx.constraintlayout.widget.ConstraintLayout>

</LinearLayout>

Now I have selectiontracker implemented already catching my singleclick on each item. Is it possible that I can have a different event/method used if the user clicks on a specific child entry in this xml, for instance on the textview with the id vp. I could live with if this also selects the item as usual, but I also want to display some popup info when the user clicks on a certain part of the item (as there is not enough room to just show it in the first place).

I already tried with an onItemTouchListener like this:

        mRecyclerView.addOnItemTouchListener(new RecyclerView.OnItemTouchListener() {
            @Override
            public boolean onInterceptTouchEvent(@NonNull RecyclerView rv, @NonNull MotionEvent e) {
                View child = rv.findChildViewUnder(e.getX(), e.getY());
                Log.v("LONG", "" child.toString());
                return false;
            }

but the string shown in logcat is always the first LinearLayout Container, so I would have no clue how to descent down to the vp textview to see if the user actually clicked on it and not somewhere else on the item.

I clarified the question and adding my tracker code now, so people reading this do not confuse it with me trying to get two onClick events. As stated already above, the tracker seems to catch all onClick events for its own use, so setting any more onClickListener won't do me any good.

        tracker = new SelectionTracker.Builder<>(
                "my-selection-id",
                mRecyclerView,
                myItemKeyProvider,
                new MyItemDetailsLookup(mRecyclerView),
                    StorageStrategy.createStringStorage())
                    .withSelectionPredicate(new MySelectionPredicate<>(this, mCivicViewModel))
                    .build();
        mAdapter.setSelectionTracker(tracker);

CodePudding user response:

You need to make some changes in your code, I myself had this problem a few months ago. This article helped me.

CodePudding user response:

You can add a View on the top of the ConstraintLayout and set the width and the height of this View to match_parent and add two click listeners one to that View and one to vp TextView, so your Xml will look like this:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="horizontal">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:id="@ id/card"
        android:layout_width="match_parent"
        android:layout_height="113dp"
        android:layout_marginTop="2dp"
        android:layout_marginBottom="2dp"
        android:background="@drawable/item_background">

         <TextView
            android:id="@ id/view"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />

        <TextView
            android:id="@ id/familybonus"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="16dp"
            android:text="TextView"
            app:layout_constraintBottom_toBottomOf="@ id/price"
            app:layout_constraintStart_toEndOf="@ id/price"
            app:layout_constraintTop_toTopOf="@ id/price" />

        <TextView
            android:id="@ id/price"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="16dp"
            android:layout_marginBottom="3dp"
            android:background="@drawable/price_background"
            android:foregroundGravity="center"
            android:gravity="center"
            android:textAppearance="?attr/textAppearanceListItem"
            android:textColor="@android:color/white"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@ id/name" />

        <TextView
            android:id="@ id/name"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="@dimen/text_margin"
            android:layout_marginTop="23dp"
            android:layout_marginBottom="29dp"
            android:foregroundGravity="center"
            android:gravity="center"
            android:textAppearance="?attr/textAppearanceListItem"
            app:layout_constraintBottom_toTopOf="@ id/price"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.5"
            app:layout_constraintStart_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintVertical_bias="0.0" />

        <TextView
            android:id="@ id/vp"
            android:layout_width="40dp"
            android:layout_height="40dp"
            android:layout_marginEnd="16dp"
            android:layout_weight="1"
            android:background="@drawable/ic_shield"
            android:gravity="center"
            android:text="VP"
            android:textStyle="bold"
            app:layout_constraintBottom_toBottomOf="@ id/price"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintTop_toTopOf="@ id/price"
            app:layout_constraintVertical_bias="1.0" />

    </androidx.constraintlayout.widget.ConstraintLayout>

</LinearLayout>

And add those two click listeners to your view holder:

view.setOnClickListener { 
    // code for the click outside the TextView vp
}

tp.setOnClickListener {
    // code for the click on the TextView vp
}
  • Related