单击不处理Fragment中的RecyclerView项目

时间:2016-11-15 13:52:50

标签: android android-fragments onclick android-recyclerview fragment

我的应用程序使用以下结构:

Activity > ViewPager > FragmentStatePagerAdapter > Fragment > RecyclerView

我过去使用RecyclerViews相当舒服,但由于某种原因,当放入片段时,我无法让我的RecyclerView项目注册点击事件。我的ViewHolder如下:

// ViewHolder Class for ExhibitAdapter
    public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        public TextView itemTitleTextView;
        public TextView itemDescriptionTextView;
        public NetworkImageView itemCoverPhoto;

        public ViewHolder(View itemView) {
            super(itemView);
            itemCoverPhoto = (NetworkImageView) itemView.findViewById(R.id.exhibitThumbnailNIV);
            itemTitleTextView = (TextView) itemView.findViewById(R.id.exhibitNameTV);
            itemDescriptionTextView = (TextView) itemView.findViewById(R.id.exhibitDescriptionTV);
            itemView.setOnClickListener(this);
        }


        @Override
        public void onClick(View view) {
            try {
                Log.d(TAG, "Exhibit clicked");
                Exhibit currentExhibit = exhibits.get(getAdapterPosition());
                intent.putExtra(ExhibitContentActivity.EXHIBIT_ID , currentExhibit.getId());
                intent.putExtra(ExhibitContentActivity.EXHIBIT_COVER_URL , currentExhibit.getCoverPhotoUrl());

                ActivityOptionsCompat options = ActivityOptionsCompat.
                        makeSceneTransitionAnimation((Activity) adapterContext, itemView.findViewById(R.id.exhibitThumbnailNIV), "profile");

                Log.i(TAG, "Attempting to launch detail activity with exhibit id: " + currentExhibit.getId());

                adapterContext.startActivity(intent, options.toBundle());

            }
            catch(IndexOutOfBoundsException e) {
                Log.e(TAG, "* Error: Adapter position exceeded the no. of items.");
            }
        }

    }

任何帮助将不胜感激。我尝试按照https://guides.codepath.com/android/Creating-and-Using-Fragments#fragment-listener中列出的步骤创建一个Fragment Interaction侦听器,但我不确定如何在RecyclerView的Adapter中实现它。提前谢谢!

2 个答案:

答案 0 :(得分:3)

您应该在RecyclerView适配器的onBindViewHolder方法中设置onClickListener,而不是在ViewHolder中。问题是ViewHolders不是实际视图(因此您无法单击其中一个),它们是绑定到视图的持有者,允许您将这些视图重用于多个列表项,以减少应用程序的内存占用。 / p>

RecyclerView.Adapter实施

public MyAdapter extends RecyclerView.Adapter<YourViewHolderClass> {
    .
    .
    .
    @Override
    public void onBindViewHolder(final YourViewHolderClass holder, int position) {
        holder.itemView.setOnClickListener (new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // Do something!
            }
        });
        // Do you other view binding operations
    }
    .
    .
    .
}

请确保在您的视图持有者中本地存储itemView的副本,以便您可以将OnClickListener绑定到列表项的容器。

RecyclerView.ViewHolder实施

public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
    public TextView itemTitleTextView;
    public TextView itemDescriptionTextView;
    public NetworkImageView itemCoverPhoto;
    public View itemView;

    public ViewHolder(View itemView) {
        super(itemView);
        this.itemView = itemView;
        itemCoverPhoto = (NetworkImageView) itemView.findViewById(R.id.exhibitThumbnailNIV);
        itemTitleTextView = (TextView) itemView.findViewById(R.id.exhibitNameTV);
        itemDescriptionTextView = (TextView) itemView.findViewById(R.id.exhibitDescriptionTV);
    }

}

我希望这有帮助!

- 汤姆

答案 1 :(得分:0)

你的itemView内部是否有任何可聚焦的视图?如果是这样,尝试将android:focusable="false"添加到视图的xml,它在ListView中工作,但我不确定它是否在RecyclerView中。