我有一个显示RecyclerView的片段。我目前正在这样处理我的holder类中的onclick事件:

public class CategoryHolder extends RecyclerView.ViewHolder implements View.OnClickListener  {

    public ImageView categoryImageView;
    public TextView categoryNameTextView;
    public TextView categoryAmountTextView;
    ArrayList<Category> categoriesArrayList = new ArrayList<>();
    Context context;


    public CategoryHolder(View itemView, Context context, ArrayList<Category> categories) {
        super(itemView);
        this.categoriesArrayList = categories;
        this.context = context;

        itemView.setOnClickListener(this);
        this.categoryImageView = (ImageView) itemView.findViewById(R.id.categoryImageView);
        this.categoryNameTextView = (TextView) itemView.findViewById(R.id.categoryNameTextView);
        this.categoryAmountTextView = (TextView) itemView.findViewById(R.id.categoryAmountTextView);
    }


    @Override
    public void onClick(View v) {

        int position = getAdapterPosition();
        Category category = this.categoriesArrayList.get(position);
        Toast.makeText(context, ""+category.getCategoryName() + position,


    }
}

如您所见,我已经在holder类中实现了OnClickListener,然后在Holder中实现了setTheOnItemClickListner。

在我的适配器中,我将持有者类与数据一起传递给arraylist,如下所示:
 Context context;
    ArrayList<Category> categories;


    public CategoriesAdapter(Context context, ArrayList<Category> categories) {
        this.context = context;
        this.categories = categories;
    }

    @Override
    public CategoryHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.categorylist_singlecell, parent, false);
        CategoryHolder holder = new CategoryHolder(v, context, categories);
        return holder;
    }

创建新的持有人时,我正在传递数据。

这可以正常工作,我可以从ArrayListRecyclerView中获取位置和任何数据。

我想做的是在初始化RecyclerView和适配器的主片段中使用它。
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    // Inflate the layout for this fragment
    View view = inflater.inflate(R.layout.fragment_nested_youtube_video_selection, container, false);

    //////////////////////////// CATEGORIES RECYCLER /////////////////////////////////////

    // Initialise the categories Recylerview
    LinearLayoutManager categoriesLayoutManger = new LinearLayoutManager(getActivity());
    categoriesLayoutManger.setOrientation(LinearLayoutManager.HORIZONTAL);
    categoriesRecyclerView = (RecyclerView) view.findViewById(R.id.youtubeAlarmCategoryRecyclerView);
    categoriesRecyclerView.setLayoutManager(categoriesLayoutManger);

    // Get the data for the categories RecyclerView
    categoryArraylist = CategoryCollection.getCategoryArrayList();

    // Initialse the categories RecyclerView Adapter
    categoriesAdapter = new CategoriesAdapter(getActivity(), categoryArraylist);

    // Bind the categories Adapter to the categories RecyclerView
    categoriesRecyclerView.setAdapter(categoriesAdapter);

我在想我需要一个接口,但是我不确定该怎么做,或者那是否是最好的方法。

最佳答案

以下应该工作:

  • 创建接口
  • 在您的片段中创建此接口的实例,并将其传递给适配器
  • 让适配器将此接口传递给视图持有者
  • 让视图持有者定义一个onClickListener,该事件将事件传递给
  • 接口

    这是一个例子:

    界面
    public interface ItemClickListener {
    
        void onItemClicked(ViewHolder vh, Object item, int pos);
    }
    
    public interface GenericItemClickListener<T, VH extends ViewHolder> {
    
        void onItemClicked(VH vh, T item, int pos);
    }
    

    ViewHolder
    public class CategoryHolder extends RecyclerView.ViewHolder {
    
        public CategoryHolder(View itemView, Context context) {
            super(itemView);
            this.context = context;
    
            this.categoryImageView = (ImageView) itemView.findViewById(R.id.categoryImageView);
            this.categoryNameTextView = (TextView) itemView.findViewById(R.id.categoryNameTextView);
            this.categoryAmountTextView = (TextView) itemView.findViewById(R.id.categoryAmountTextView);
        }
    }
    

    适配器
    Context context;
    ArrayList<Category> categories;
    ItemClickListener itemClickListener;
    
    public CategoriesAdapter(Context context, ArrayList<Category> categories, ItemClickListener itemClickListener) {
        this.context = context;
        this.categories = categories;
        this.itemClickListener = itemClickListener;
    }
    
    // DON'T PASS YOUR DATA HERE, just create a ViewHolder
    @Override
    public CategoryHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.categorylist_singlecell, parent, false);
        CategoryHolder holder = new CategoryHolder(v, context);
        return holder;
    }
    
    //HERE you bind one item of your list to the view holder
    @Override
    public void onBindViewHolder(final CategoryHolder vh, final int i) {
        final Category category = categories.get(i);
    
        // set click listener
        vh.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                itemClickListener.onItemClicked(vh, category, i);
            }
        });
    
       // update images and texts...
    }
    

    编辑

    更新了代码以向您显示,您不应将项目数组传递给每个视图持有者...创建并重复使用视图持有者,仅更新onBindViewHolder中的视图持有者并将视图持有者实例绑定到那里的正确数据。 ..

    顺便说一句,我改为创建一个通用接口,它更加美丽……我的示例只是一个简单的解决方案……

    Edt 2-如何创建接口实例
    ItemClickListener listener = new ItemClickListener(){
        @Override
        public void onItemClicked(RecyclerView.ViewHolder vh, Object item, int pos){
            Toast.makeText(getActivity(), "Item clicked: " + pos, Toast.LENGTH_SHORT).show();
        }
    };
    

    07-25 22:18