How to open fragment from RecyclerView Adapter?
Do two small changes in your code-
Adapter
public CatAdapter(ArrayList<String> catUrlImage, ArrayList<String> cuteCatName, Context catContext, OnRecyclerViewItemClickListener listenerthis) {
this.catUrlImage = catUrlImage;
this.cuteCatName = cuteCatName;
this.catContext = catContext;
this.listenerthis = listenerthis;
}
MainActivity
private void initRecycleView() {
CatAdapter catAdapter = new CatAdapter(catArrayImageUrl, catArray, this, this);
recyclerView.setAdapter(catAdapter);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
}
thats it. Hope it will be helpful.
Jetpack navigation: How do I open a new fragment from a recyclerview adapter?
As navigation occurs among fragments, so normally you'd keep the fragment's the responsibility to do that.
As you told that adapter which can be used in multiple fragments, so it should be attached to a particular fragment at a time, which normally it's the fragment that instantiated it.
So, you can pass a listener interface to the adapter which is implemented by the fragment; where you can trigger its callback in the adapter whenever you want to navigate to another fragment in the nav graph.
This callback method will be executed at the fragment which already implements the listener, and you can normally use the traditional navigation code:
Navigation.findNavController(view).navigate(R.id.action_startFragment_to_destinationFragment);
This way your adapter can be reused, and every time a fragment wants to reuse it, it should implements the listener.
Note: Probably you can pass an int
argument to the listener callback that pass in the row number in the adapter back to the fragment so that you might decide to navigate to some other fragment.
This way you can keep the navigation only through fragments.
how to open a different fragment on recyclerview OnClick
So simplified version, here's my onbindVH:
@Override
public void onBindViewHolder(FeedsViewHolder holder, int position) {
final Feed item = mFeeds.get(position);
holder.textView1.setText(item.getText());
holder.imageView.setImageDrawable(mContext.getResources().getDrawable(item.getImage));
holder.imageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
fragmentJump(item);
}
});
}
here's fragmentJump:
private void fragmentJump(Feed mItemSelected) {
mFragment = new Fragment2();
mBundle = new Bundle();
mBundle.putParcelable("item_selected_key", mItemSelected);
mFragment.setArguments(mBundle);
switchContent(R.id.frag1, mFragment);
}
here's switchContent on the adapter:
public void switchContent(int id, Fragment fragment) {
if (mContext == null)
return;
if (mContext instanceof MainActivity) {
MainActivity mainActivity = (MainActivity) mContext;
Fragment frag = fragment;
mainActivity.switchContent(id, frag);
}
}
and lastly, here's the switchContent on MainActivity:
public void switchContent(int id, Fragment fragment) {
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(id, fragment, fragment.toString());
ft.addToBackStack(null);
ft.commit();
}
I know it can still be simplified, but it works as is. I hope it helps. :)
Related Topics
How to Disable Behind View Click Event Framelayout
Android Notification Has a Color Icon Instead of Being Turned White
Android Studio 0.4.3 - Task 'Assemble' Not Found in Root Project
Setcontentview(R.Layout.Main); Gives Error from Start
Keep Screen Alive and App in Front of Lockscreen
How to Set the Preview Image in Videoview Before Playing
Recyclerview Is Cutting Off the Last Item
Android Studio Inline Compiler Showing Red Errors, But Compilation With Gradle Works Fine
How to Show Only Specific Part of Website in Android Webview
How to Give Pause or Gap Between Words in Tts in Android
Android Navigationview: Reduce Space Between Icon and Text and 'Itembackground' Not Working
Flutter Keyboard Makes Textfield Hide
How to Add the Json Response into Spinner in Android Using Retrofit
How to Post Raw Whole Json in the Body of a Retrofit Request
React Native | Failed to Install the App. Please Accept All Necessary Sdk Licenses Using Sdk Manager