片段不会在RecyclerView项上打开

时间:2019-09-22 13:13:04

标签: android android-fragments android-recyclerview navigation-drawer

该项目使用Navigation DrawerRecyclerView和一个界面来检测对RecyclerView项目的点击。

how to open a different fragment on recyclerview OnClick

问题是当我单击这些项目时,什么都没有发生。 我试图打开一个工作正常的活动。所以我认为界面没有问题。

我发现了一些类似的问题this,但是没有一个问题有助于解决这个特定问题。

我做错了什么?

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>

1 个答案:

答案 0 :(得分:0)

RecyclerView可见性消失/可见是为了确保您的代码正常运行。 如您所见,您的书签视图正变得正确。

fragment_home布局中的问题。 FrameLayout和Recyclerview都已获得全屏显示。因此,当您单击RV项目时,它正在打开书签,但对您不可见。

fragment_home中的FrameLayout用作书签片段的容器。

由于您使用的是NavController,因此像打开HomeFragment一样使用它来打开Bookmark片段。