【发布时间】:2017-11-05 23:32:19
【问题描述】:
我正在使用一个recyclerview,它的viewholder条目有一个imageview。两者都有一个 onclick 监听器。如果子图像视图响应了 onclick,我不希望 recylerview onClick 响应它。如果单击的位置不在 imageviewer 上,我希望 recyclerview 响应。我目前的研究表明,通常这种情况可以通过使用相同的函数来响应两个 onClick 事件来处理,但问题是 recyclerview onclick 和它的子 imageview onClick 是不同的。我看不出如何在同一个函数中处理它们。现在我有一个黑客解决方法来使用一个全局变量来指示 imageview 已经响应了这个点击,并在 recyclerview 监听器上设置了 200ms 的延迟来在响应之前检查全局变量。有没有更合适的方法?
我刚刚注意到,如果我为 recycler 视图的每种类型的子视图添加一个“onClick”并删除 recyclerview 上的侦听器,那可以工作。无论如何,现在我只有两个子视图。虽然我会添加更多内容,但它仍然可以管理。如果没有更好的方法,我可能会这样做。
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:focusable="true"
android:paddingLeft="16dp"
android:paddingRight="4dp"
android:paddingTop="10dp"
android:paddingBottom="10dp"
android:clickable="true"
android:background="?android:attr/selectableItemBackground"
android:orientation="vertical">
<TextView
android:id="@+id/lstItemName"
android:textColor="@color/title"
android:textSize="16dp"
android:textStyle="bold"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toLeftOf="@+id/lstItemReportby"
android:layout_gravity="left"
android:layout_width="0dp"
android:layout_height="wrap_content"
/>
<ImageView
android:id="@+id/lstItemMap"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginRight="2dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="true"/>
</android.support.constraint.ConstraintLayout>
public class TrackedItemListAdapter extends RecyclerView.Adapter<TrackedItemListAdapter.MyViewHolder>{
public class MyViewHolder extends RecyclerView.ViewHolder {
public TextView m_itemName, m_itemDes, m_itemReportedBy, m_itemHiddenText;
public ImageView m_itemMapDrop;
public MyViewHolder(View view) {
super(view);
m_itemName = (TextView) view.findViewById(R.id.lstItemName);
m_itemMapDrop = (ImageView) view.findViewById(R.id.lstItemMap);
}
}
@Override
public void onBindViewHolder(TrackedItemListAdapter.MyViewHolder holder, int position) {
holder.m_itemName.setText(obj.getName());
holder.m_itemMapDrop.setImageResource(R.drawable.ic_img_nomap);
holder.m_itemMapDrop.setOnClickListener(new View.OnClickListener() {
//@Override
public void onClick(View v) {
startMap();
}
});
}
}
m_trackedItemListAdapter = new TrackedItemListAdapter();
m_recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
m_recyclerView.setHasFixedSize(true);
RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getApplicationContext());
m_recyclerView.setLayoutManager(mLayoutManager);
m_recyclerView.addItemDecoration(new DividerItemDecoration(this, LinearLayoutManager.VERTICAL));
m_recyclerView.setItemAnimator(new DefaultItemAnimator());
m_recyclerView.setAdapter(m_trackedItemListAdapter);
m_recyclerView.addOnItemTouchListener(new RecyclerTouchListener(getApplicationContext(), m_recyclerView, new RecyclerTouchListener.ClickListener() {
@Override
public void onClick(View view, int position) {
//a hack to turn off response if the imageview for location already responded
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
popupItemContent();
}
}, 200);
}
}
【问题讨论】:
-
我真的不明白 2 个完全不同的视图最终会如何共享同一个 onClickListener,除非您以这种方式手动分配它。你介意分享你的一些代码吗?特别是 ViewHolder、Adapter 和 Layout。
标签: android android-recyclerview onclicklistener