片段不会在RecyclerView项上打开 [英] Fragment won't open on RecyclerView item click

查看:77
本文介绍了片段不会在RecyclerView项上打开的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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.

我发现了一些类似的问题如何打开其他片段在recyclerview OnClick上,但是它们都没有帮助解决此特定问题.

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

我做错了什么?

HomeFragment.java

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

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

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

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

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

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>

推荐答案

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

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

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

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.

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

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

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

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

这篇关于片段不会在RecyclerView项上打开的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆