Skip to content Skip to sidebar Skip to footer

Can't Resolve The Context Or Application While Navigating From Adapter Of A Fragment(a) To Another Fragment (b)

Am trying to navigate from one fragment (A) to another (B), but the fragment, but the first fragment (A) has a recyclerView meaning when I click on any Item I should navigate to th

Solution 1:

It is not the responsibility of RecyclerView's adapter to redirect to another fragment.

Create interface like

publicinterfaceOnItemClickListener {
    voidonItemClicked(int position)
}

Inside your RecyclerView's adapter add method:

publicclassYourAdapterName extend RecyclerView.Adapter...
    private OnItemClickListener onItemClickListener

    voidsetOnItemClickListener(OnItemClickListener listener) {
        onItemClickListener = listener
    }

...

    @OverridepublicvoidonBindViewHolder(@NonNullfinal CoordinatesViewHolder holder, int position) {
        finalCoordinatescoord= mCoordinates.get(position);
        holder.place_name.setText(coord.getmUPlaceName());
        holder.view.setOnClickListener(newView.OnClickListener() {
            @OverridepublicvoidonClick(View v) {
                if(onItemClickListener != null) {
                     onItemClickListener.onItemClicked(position)
                }
            }
        });
    }

In your fragment with recycler, in place where you set adapter add code:

YourAdapterClassNameadapter=newYourAdapterClassName(...init adapter...)
adapter.setOnItemClickListener(newOnItemClickListener() {
    @OverridepublicvoidonItemClicked(int position) {
         //Navigate here 
    }
})
yourRecyclerName.setAdapter(adapter)

Hope it'll help )

Post a Comment for "Can't Resolve The Context Or Application While Navigating From Adapter Of A Fragment(a) To Another Fragment (b)"