从另一个回收器适配器的 onClick() 调用方法来运行回收器视图 class
Calling a method to function a recyclerview from an onClick() of another recycler adapter class
我有一个片段 HomeFragment,它有 2 个 recyclerviews,它们都有单独的适配器:
1. 为了显示类别(这些类别是从 api 中检索到的),我在 HomeFragment 中使用了 fetchCat()
2. 要使用带有类别 ID 的 http 调用获取这些类别的提要,我在 Home Fragment 中使用 fetchFeed(categoryid);
我不知道如何从 categoryadapter 访问 HomeFragment 中的垂直 recyclerview 方法。我需要知道单击了哪个类别,我必须调用驻留在 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);
}
});
}
首先是将您的方法 fetchFeedJson 声明为 public
public void fetchFeedJson(Integer startVal) {
......
}
然后有多种方法可以使用
第一个例子:
在你的 HomeFagment
里面
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;
}
在你的听众里面:
cateogoryViewHolder.images.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v){
((Home)fragment).ruleName(position);
this.homeFragment.fetchFeedJson(Integer.valueOf(position));
}
});
我有一个片段 HomeFragment,它有 2 个 recyclerviews,它们都有单独的适配器: 1. 为了显示类别(这些类别是从 api 中检索到的),我在 HomeFragment 中使用了 fetchCat() 2. 要使用带有类别 ID 的 http 调用获取这些类别的提要,我在 Home Fragment 中使用 fetchFeed(categoryid); 我不知道如何从 categoryadapter 访问 HomeFragment 中的垂直 recyclerview 方法。我需要知道单击了哪个类别,我必须调用驻留在 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);
}
});
}
首先是将您的方法 fetchFeedJson 声明为 public
public void fetchFeedJson(Integer startVal) {
......
}
然后有多种方法可以使用
第一个例子: 在你的 HomeFagment
里面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;
}
在你的听众里面:
cateogoryViewHolder.images.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v){
((Home)fragment).ruleName(position);
this.homeFragment.fetchFeedJson(Integer.valueOf(position));
}
});