同一布局中的多个RecyclerViews:单击一个的侦听器将触发所有RecyclerView



我在同一布局中有两个回收器视图(比如A和B),它们都有一个适配器,两个循环器视图看起来像这样:


public class ChapterListAdapter extends RecyclerView.Adapter<ChapterListAdapter.ChapterListViewHolder>{
private final ArrayList<ChapterObj> mChapterListObj;
private final Context mContext;
public ChapterListAdapter(ArrayList<ChapterObj> chapterObj, Context c) {
    mChapterListObj = chapterObj;
    mContext = c;
}
@Override
public ChapterListViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.chapter_list_item, parent, false);
    ChapterListViewHolder vh = new ChapterListViewHolder(v);
    return vh;
}
@Override
public void onBindViewHolder(ChapterListViewHolder holder, int position) {
    holder.chapterNumber.setText(mChapterListObj.get(position).getChapterNumber());
}
@Override
public int getItemCount() {
    return mChapterListObj.size();
}
public class ChapterListViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener, View.OnTouchListener {
    private TextView chapterNumber;
    public ChapterListViewHolder(View itemView) {
        super(itemView);
        chapterNumber = (TextView) itemView.findViewById(R.id.chapter_number);
        itemView.setOnClickListener(this);
        itemView.setOnTouchListener(this);
    }
    @Override
    public void onClick(View v) {
        Log.d("hello", "hello");
        }
    }
}
}

A和B都有点击侦听器。独立地说,两者都很好。但是,当两者都在同一布局中时,每当我单击"A"的项目时,"B"的单击侦听器就会被触发。

如果你需要查看更多的代码,告诉我你想查看哪个文件,我也会添加他们的代码。


编辑:我将它们一起使用的xml布局文件如下所示:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".VerseActivity">
<android.support.v7.widget.RecyclerView
    android:id="@+id/chapter_list_menu"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_alignParentLeft="true"
    xmlns:android="http://schemas.android.com/apk/res/android"
    >
</android.support.v7.widget.RecyclerView>

<android.support.v7.widget.RecyclerView
    android:id="@+id/verse_list_menu"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:layout_alignParentTop="true"
    xmlns:android="http://schemas.android.com/apk/res/android"
    >
</android.support.v7.widget.RecyclerView>
</RelativeLayout>

您可以使用Sectioned recyclerator视图做同样的事情。它甚至可以更快地加载,并允许您显示任意数量的分段。(消除相同页面或视图中2-3个可循环视图的障碍)

如果你想创建自己的,你可以检查这个-https://gist.github.com/gabrielemariotti/4c189fb1124df4556058

或者你也可以找到很多这样的库。

最新更新