首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >从另一个回收器适配器类的onClick()调用方法以执行回收器视图的函数

从另一个回收器适配器类的onClick()调用方法以执行回收器视图的函数
EN

Stack Overflow用户
提问于 2019-06-05 06:07:00
回答 1查看 63关注 0票数 0

我有一个片段HomeFragment,它有两个回收器视图,两个回收器视图都有独立的适配器: 1.为了显示类别(这些是从api中检索的),我在http中使用了fetchCat() 2.为了使用带有类别id的http调用来获取这些类别的提要,我在HomeFragment片段中使用fetchFeed(categoryid);我被困在如何从category adapters访问HomeFragment中的垂直回收器视图方法。我需要知道哪个类别被点击了,我必须调用一个驻留在HomeFragment中的方法。

public class CateogoryAdapter extends RecyclerView.Adapter<CateogoryAdapter.CateogoryViewHolder>{

List<CateogoryList> cateogoryLists;
Context context;



public CateogoryAdapter(List<CateogoryList> cateogoryLists, Context context) {
    this.cateogoryLists = cateogoryLists;
    this.context = context;

}


@NonNull
@Override
public CateogoryViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
    View view = LayoutInflater.from(context).inflate(R.layout.horizontal_list, viewGroup, false);

    CateogoryViewHolder cateogoryViewHolder = new CateogoryViewHolder(view);

    return cateogoryViewHolder;
}

@Override
public void onBindViewHolder(@NonNull final CateogoryViewHolder cateogoryViewHolder, int i) {

    cateogoryViewHolder.cateogrylist.setText(cateogoryLists.get(i).getCategory());

    Glide.with(context)
            .load(cateogoryLists.get(i).getImage())
            .into(cateogoryViewHolder.images);


    cateogoryViewHolder.images.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v){
            ((Home)fragment).ruleName(position);

        }
    });


}


@Override
public int getItemCount() {
    return cateogoryLists.size();
}

public class CateogoryViewHolder extends RecyclerView.ViewHolder{
    TextView cateogrylist;
    CircleImageView images;

    public CateogoryViewHolder(@NonNull View itemView) {
        super(itemView);

        cateogrylist = (TextView)itemView.findViewById(R.id.cateogory);
        images = (CircleImageView)itemView.findViewById(R.id.catimg);

    }
}

}

要调用的HomeFragment上的方法

 private void fetchFeedJson(Integer startVal) {
    progressBar.setVisibility(View.GONE);

    shimmerFrameLayout.startShimmer();
    Integer studentid = PrefManager.getInstance(getContext()).getUser().getStudentid();
    Call<List<FeedList>> call = RetrofitClient
            .getInstance()
            .getApi()
            .fetchFeed(studentid, startVal);

    call.enqueue(new Callback<List<FeedList>>() {
        @Override
        public void onResponse(Call<List<FeedList>> call, Response<List<FeedList>> response) {

            List<FeedList> feed = response.body();
            feedAdapter = new FeedAdapter(feed, getContext());

            feedRecyclerView.setLayoutManager(manager);
            feedRecyclerView.setAdapter(feedAdapter);
            feedAdapter.notifyDataSetChanged();
            shimmerFrameLayout.setVisibility(View.GONE);

        }

        @Override
        public void onFailure(Call<List<FeedList>> call, Throwable t) {

            Toast.makeText(getContext(), "Some Error Occured", Toast.LENGTH_SHORT).show();
            shimmerFrameLayout.setVisibility(View.GONE);
        }
    });

}
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56451935

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档