Android 调用方法从另一个recycler适配器类的onClick()调用recyclerview函数
我有一个片段HomeFragment,上面有两个recyclerviews,它们都有单独的适配器: 1.为了显示类别(这些类别是从api检索的),我在HomeFragment中使用了fetchCat() 2.为了使用类别id为的http调用获取这些类别的提要,我在Home片段中使用fetchFeed(categoryid); 我一直想知道如何访问categoryadapter中HomeFragment中的垂直recyclerview方法。我需要知道单击了哪个类别,我必须调用驻留在HomeFragment中的方法 公共类CategoryAdapter扩展了RecyclerView.Adapter{ 列出分类表; 语境; 公共CategoryAdapter(列出CategoryList、上下文){ this.categorylists=categorylists; this.context=上下文; } @非空 @凌驾 public CategoryViewHolder onCreateViewHolder(@NonNull ViewGroup ViewGroup,int i){ 视图=布局更平坦。从(上下文)。充气(R.layout.horizontal\u列表,视图组,false); CategoryViewHolder CategoryViewHolder=新的CategoryViewHolder(视图); 返回CategoryViewHolder; } @凌驾 BindViewHolder上的公共无效(@NonNull final categoryViewHolder categoryViewHolder,int i){ CategoryViewHolder.CategoryList.setText(CategoryList.get(i.getCategory()); 带(上下文)滑动 .load(cateograylists.get(i).getImage()) .into(categoryviewholder.images); CategoryViewHolder.images.setOnClickListener(新视图.OnClickListener(){ @凌驾 公共void onClick(视图v){ ((主)片段)。规则名称(位置); } }); } @凌驾 public int getItemCount(){ 返回cateogoryList.size(); } 公共类CategoryViewHolder扩展了RecyclerView.ViewHolder{ 文本视图分类列表; 圆形图像查看图像; public categoryviewholder(@NonNull View itemView){ 超级(项目视图); cateogrylist=(TextView)itemView.findViewById(R.id.cateogray); images=(CircleImageView)itemView.findViewById(R.id.catimg); } } } 要调用的HomeFragment上的方法Android 调用方法从另一个recycler适配器类的onClick()调用recyclerview函数,android,android-fragments,android-recyclerview,fragment,adapter,Android,Android Fragments,Android Recyclerview,Fragment,Adapter,我有一个片段HomeFragment,上面有两个recyclerviews,它们都有单独的适配器: 1.为了显示类别(这些类别是从api检索的),我在HomeFragment中使用了fetchCat() 2.为了使用类别id为的http调用获取这些类别的提要,我在Home片段中使用fetchFeed(categoryid); 我一直想知道如何访问categoryadapter中HomeFragment中的垂直recyclerview方法。我需要知道单击了哪个类别,我必须调用驻留在HomeFrag
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);
}
});
}
private void fetchFeedJson(整数startVal){
progressBar.setVisibility(View.GONE);
shimmerFrameLayout.startShimmer();
整数studentid=PrefManager.getInstance(getContext()).getUser().getStudentid();
Call=Call客户端
.getInstance()
.getApi()
.fetchFeed(studentid,startVal);
call.enqueue(新回调(){
@凌驾
公共void onResponse(调用、响应){
列表提要=response.body();
feedAdapter=新的feedAdapter(feed,getContext());
feedRecyclerView.setLayoutManager(管理器);
feedRecyclerView.setAdapter(feedAdapter);
feedAdapter.notifyDataSetChanged();
shimmerFrameLayout.setVisibility(View.GONE);
}
@凌驾
失败时公共无效(调用调用,可丢弃的t){
Toast.makeText(getContext(),“发生了一些错误”,Toast.LENGTH_SHORT.show();
shimmerFrameLayout.setVisibility(View.GONE);
}
});
}
第一件事是将您的方法fetchFeedJson声明为公共方法
public void fetchFeedJson(Integer startVal) {
......
}
然后有多种方法可以使用它
第一个示例:
在您的主页中
class HomeFragment extends Fragment{
public static HomeFragment homeFragment;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_past_cycle, container, false);
homeFragment = this;
return view ;
}
public void fetchFeedJson(Integer startVal) {
......
}
}
在你的适配器中,这样调用你的方法
cateogoryViewHolder.images.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v){
((Home)fragment).ruleName(position);
HomeFragment.homeFragment.fetchFeedJson(Integer.valueOf(position));
}
});
第二个示例
将主片段实例作为参数传递给适配器,如下所示
List<CateogoryList> cateogoryLists;
Context context;
HomeFragment homeFragment;
public CateogoryAdapter(List<CateogoryList> cateogoryLists, Context context , HomeFragment homeFragment)
{
this.cateogoryLists = cateogoryLists;
this.context = context;
this.homeFragment = homeFragment;
}
第一件事是将您的方法fetchFeedJson声明为public
public void fetchFeedJson(Integer startVal) {
......
}
然后有多种方法可以使用它
第一个示例:
在您的主页中
class HomeFragment extends Fragment{
public static HomeFragment homeFragment;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_past_cycle, container, false);
homeFragment = this;
return view ;
}
public void fetchFeedJson(Integer startVal) {
......
}
}
在你的适配器中,这样调用你的方法
cateogoryViewHolder.images.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v){
((Home)fragment).ruleName(position);
HomeFragment.homeFragment.fetchFeedJson(Integer.valueOf(position));
}
});
第二个示例
将主片段实例作为参数传递给适配器,如下所示
List<CateogoryList> cateogoryLists;
Context context;
HomeFragment homeFragment;
public CateogoryAdapter(List<CateogoryList> cateogoryLists, Context context , HomeFragment homeFragment)
{
this.cateogoryLists = cateogoryLists;
this.context = context;
this.homeFragment = homeFragment;
}
听起来你需要一个监听器来从RecyclerView适配器通信回片段,如果你有两个适配器,那么一旦你截获了来自监听器的调用,你就必须调用第二个适配器上的一个方法来执行你的逻辑,你也不必将逻辑保留在改型排队方法中,将逻辑移到外部,并在获得数据后通知适配器。听起来您需要一个侦听器从RecyclerView适配器通信回片段,如果您有两个适配器,而不是一次截获来自侦听器的调用,则必须调用第二个适配器上的方法来执行逻辑,此外,您不必将逻辑保留在翻新排队方法中,将逻辑移到外部,并在获得数据后通知适配器。