1

This project uses Navigation Drawer, RecyclerView and an Interface to detect the clicks on RecyclerView items.

enter image description here

Problem is when I click on the items nothing happens. I tried to open an activity that works fine. so i think there's no problem with the interface.

I found some similar questions how to open a different fragment on recyclerview OnClick but none of them helped to solve this specific problem.

What is it I am doing wrong?

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"));
    // .......... 10 more items

    myAdapter = new MyAdapter(data_items, this);
    recyclerView.setAdapter(myAdapter);
    myAdapter.notifyDataSetChanged();

}

@Override
public void onNoteClick(int position) {
    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">

<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" />

BookmarkFragment.java

public class BookmarkFragment extends Fragment {

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    return inflater.inflate(R.layout.fragment_bookmark, container, false);
}}

MyAdapter.java

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> {
LinkedList<Data_Items> data_items;
private OnNoteListener mOnNoteListener;

public MyAdapter(LinkedList<Data_Items> data_items, OnNoteListener onNoteListener)
{
    this.data_items = data_items;
    this.mOnNoteListener = onNoteListener;
}
class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener
{
    ImageView imageView;
    TextView textView1;
    TextView textView2;
    OnNoteListener onNoteListener;

    public MyViewHolder(@NonNull View itemView, OnNoteListener onNoteListener) {
        super(itemView);
        imageView = itemView.findViewById(R.id.imageView);
        textView1 = itemView.findViewById(R.id.textView1);
        textView2 = itemView.findViewById(R.id.textView2);
        this.onNoteListener = onNoteListener;
        itemView.setOnClickListener(this);
    }
    public void  setData(int resource, String text1, String text2)
    {
        imageView.setImageResource(resource);
        textView1.setText(text1);
        textView2.setText(text2);
    }

    @Override
    public void onClick(View view) {
        onNoteListener.onNoteClick(getAdapterPosition());

    }
}
public interface OnNoteListener{
        void onNoteClick(int position);
}
@NonNull
@Override
public MyAdapter.MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.one_item_layout, parent, 
false);
    return new MyViewHolder(view, mOnNoteListener);     }

@Override
public void onBindViewHolder(@NonNull MyAdapter.MyViewHolder holder, int position) {
    int resouce = data_items.get(position).getImage();
    String text1 = data_items.get(position).getText1();
    String text2 = data_items.get(position).getText2();
    holder.setData(resouce, text1, text2);
}
@Override
public int getItemCount() {
    return data_items.size();
}}

MainActivity.java

public class MainActivity extends AppCompatActivity{
DrawerLayout drawerLayout;
NavigationView navigationView;
NavController navController;
AppBarConfiguration appBarConfiguration;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    drawerLayout = findViewById(R.id.drawer);
    navigationView = findViewById(R.id.navigationView);

    navController = Navigation.findNavController(this, R.id.navigation_resource_file);

    appBarConfiguration =
            new AppBarConfiguration.Builder(navController.getGraph())
                    .setDrawerLayout(drawerLayout)
                    .build();

    NavigationUI.setupActionBarWithNavController(this, navController, appBarConfiguration);
    NavigationUI.setupWithNavController(navigationView, navController);

}

@Override
public boolean onSupportNavigateUp() {
    return NavigationUI.navigateUp(navController, appBarConfiguration);
}

@Override
public void onBackPressed() {
    if(drawerLayout.isDrawerOpen(GravityCompat.START))
        drawerLayout.closeDrawer(GravityCompat.START);
    else
    super.onBackPressed();
}}

activity_main.xml

<androidx.drawerlayout.widget.DrawerLayout 
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"
android:id="@+id/drawer">

<androidx.constraintlayout.widget.ConstraintLayout
    android:id="@+id/constraintLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <fragment
        android:id="@+id/navigation_resource_file"
        android:name="androidx.navigation.fragment.NavHostFragment"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:defaultNavHost="true"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:navGraph="@navigation/navigation_resource_file" />

</androidx.constraintlayout.widget.ConstraintLayout>

<com.google.android.material.navigation.NavigationView
    android:id="@+id/navigationView"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    app:menu="@menu/menu_resource_file">

</com.google.android.material.navigation.NavigationView>
</androidx.drawerlayout.widget.DrawerLayout>
User9211
  • 194
  • 1
  • 2
  • 17

1 Answers1

0

RecyclerView visibility gone/visible was to ensure that your code is working fine. As you can see that your bookmark view is coming proper.

The issue in fragment_home layout. FrameLayout and Recyclerview, both have acquired full screen. So when you click on RV items, It is opening bookmark but it is not visible to you.

FrameLayout in fragment_home is working as a container for Bookmark fragment.

Since you are using NavController, so use this to open your Bookmark fragment like you do to open HomeFragment.

Kishan Maurya
  • 3,356
  • 8
  • 21