处理recyclerview单击片段而不是持有者类

问题描述 投票:1回答:2

我有一个显示RecyclerView的片段。我目前在我的持有人类中处理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, 


    }
}

如您所见,我已在持有者类中实现了OnClickListener,然后在Holder中设置了TheOnItemClickListner。

在我的适配器中,我将持有者类传递给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;
    }

我在创建新持有者时传递数据。

这很好用,我可以用ArrayList用于RecyclerView的位置和任何数据。

我想要做的是在我初始化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);

我在想我需要一个界面,但我不知道该怎么做,或者这甚至是最好的方法。

java android android-fragments android-recyclerview onclicklistener
2个回答
5
投票

以下应该工作:

  • 创建一个接口
  • 在片段中创建此接口的实例,并将其传递给适配器
  • 让适配器将此接口传递给视图持有者
  • 让视图持有者定义一个将事件传递给接口的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();
    }
};

0
投票

抽象YOURAdapter类然后添加方法

void onLikeClicked(Object object, int position); void onItemClicked();

bindView中称之为就像

holder.btnLike.setOnclickListner(new OnClickListner){
onLikeClicked(list.get(position), position);
}

在Activity Class中,它将自动实现适配器的所有方法

YOURAdapter adapter=new YOURAdapter(){
    void onLikeClicked(Object object, int position){

    }
    void onItemClicked(){

    }
}
© www.soinside.com 2019 - 2024. All rights reserved.