我有一个片段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);
}
});
}
https://stackoverflow.com/questions/56451935
复制相似问题