This uses RecyclerView
, NavigationBar
and 2 fragments(Home and Bookmark).
Home_fragment.xml
implements RecyclerView and has 1 FrameLayout
to be used as a Layout Container when opening new fragment(Bookmark Fragment).
I am using an interface to handle clicks on items.
The problem is when opening new fragment on RecyclerView item click, the fragment opens but remains behind the RecyclerView.
I used recyclerView.setVisibility(View.GONE)
inside OnClick method, it works, but i think it hides the RecyclerView permanently, it shows a blank layout when navigating back from Bookmark to Home.
To make it visivle when navigating back, i included recyclerView.setVisibility(View.VISIBLE)
in OnBackPressed which is in Main_Activity.java
, but it abruptly stops the application if i click the back button.
I checked this Fragment doesn't replace and hide another fragment, but using the suggested solution didn't make any difference.
HomeFragment.java
public class HomeFragment extends Fragment implements MyAdapter.OnNoteListener {
RecyclerView recyclerView;
MyAdapter myAdapter;
LinkedList<Data_Items> data_items = new LinkedList<Data_Items>();
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_home, container, false);
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
recyclerView = view.findViewById(R.id.recyclerView);
recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
data_items.add(new Data_Items(R.drawable.a, "Pink", "This is a pink color"));
// + a few more items
myAdapter = new MyAdapter(data_items, this);
recyclerView.setAdapter(myAdapter);
myAdapter.notifyDataSetChanged();
}
@Override
public void onNoteClick(int position) {
Intent intent = new Intent(getContext(), NewActivity.class);
startActivity(intent);
FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.layout_container, new BookmarkFragment());
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
}}
fragment_home.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".HomeFragment"
android:id="@+id/frag">
<!-- TODO: Update blank fragment layout -->
<FrameLayout
android:id="@+id/layout_container"
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
</FrameLayout>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
More details: I have asked a different question related to this project. Fragment won't open on RecyclerView item click
what is i need to do to make it work?