当使用适配器的SelectionTracker时,我如何在recylerview项目上有一个onClick事件?<



我为我的项目使用以下。xml,它在我的recyclerview中用于列出每个项目:

<?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>

现在我已经实现了selectiontracker,已经捕获了我在每个项目上的单点点击。如果用户单击此xml中的特定子条目,例如在id为vp的textview上,是否有可能使用不同的事件/方法?如果这也像往常一样选择项目,我可以接受,但我也想在用户单击项目的某个部分时显示一些弹出信息(因为没有足够的空间来显示它)。

我已经尝试过这样的onItemTouchListener:

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;
}

但是logcat中显示的字符串总是第一个LinearLayout容器,所以我不知道如何下降到vp textview,看看用户是否实际点击了它,而不是在项目的其他地方。

我澄清了这个问题,现在添加了我的跟踪器代码,所以阅读这篇文章的人不会把它与我试图获得两个onClick事件混淆。如上所述,跟踪器似乎捕获了所有的onClick事件供自己使用,所以设置更多的onClickListener对我没有任何好处。

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

您需要对代码进行一些更改,我自己几个月前就遇到了这个问题。这篇文章帮助了我。

您可以在ConstraintLayout的顶部添加View,并将此View的宽度和高度设置为match_parent,并添加两个点击侦听器一个到View,一个到vpTextView,因此您的Xml将看起来像这样:

<?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>

并将这两个点击监听器添加到视图holder中:

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

相关内容

最新更新