Ayush Shrivastava
Ayush Shrivastava

Reputation: 109

Calling a method to function a recyclerview from an onClick() of another recycler adapter class

I have a fragment HomeFragment on which it has 2 recyclerviews both having separated adapters: 1. To show categories(these are being retrieved from api), I'm using fetchCat() in HomeFragment 2. To fetch feed of those categories using http call with category id, I'm using fetchFeed(categoryid) in Home Fragment; I'm stuck as to how would i access vertical recyclerview method which is in HomeFragment from categoryadapter. I need to know which category has been clicked using that I have to call a method which resides inside 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);

    }
}

}

method on HomeFragment to be called

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

}

Upvotes: 0

Views: 767

Answers (1)

elhoucine ayoub
elhoucine ayoub

Reputation: 1009

first thing is to declare your method fetchFeedJson as public

public void fetchFeedJson(Integer startVal) {
     ......
}

then there are multiple ways you can do it with

first exemple : inside your 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) {
        ......
    }

}

and with in your adapter call your methode like that

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


        HomeFragment.homeFragment.fetchFeedJson(Integer.valueOf(position));

    }
});

second exemple

pass your home fragment instance as parameter to your adapter like this

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;

}

and inside your listener :

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

        this.homeFragment.fetchFeedJson(Integer.valueOf(position));

    }
});

Upvotes: 2

Related Questions