从另一个回收站适配器类的onClick()调用方法以实现recyclerview

时间:2019-06-04 22:06:59

标签: android android-fragments android-recyclerview fragment adapter

我有一个片段HomeFragment,在它上面有2个recyclerviews,两个都有单独的适配器: 1.为了显示类别(正在从api中检索这些类别),我在HomeFragment中使用了fetchCat() 2.要使用带有类别ID的http调用获取这些类别的供稿,我在Home Fragment中使用了fetchFeed(categoryid); 我对如何从categoryadapter访问HomeFragment中的垂直recyclerview方法感到困惑。我需要知道使用哪个类别被单击,我必须调用驻留在HomeFragment内的方法。

enter image description here

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);
        }
    });

}

1 个答案:

答案 0 :(得分:1)

第一件事是将您的方法 fetchFeedJson 声明为公共

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));

    }
});