[英]Fragment won't open on RecyclerView item click
This project uses Navigation Drawer
, RecyclerView
and an Interface to detect the clicks on RecyclerView items.这个项目使用
Navigation Drawer
、 RecyclerView
和一个 Interface 来检测对 RecyclerView 项目的点击。
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.我发现了一些类似的问题,如何在 recyclerview OnClick 上打开不同的片段,但没有一个有助于解决这个特定问题。
What is it I am doing wrong?我做错了什么?
HomeFragment.java首页Fragment.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书签Fragment.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 visibility gone/visible was to ensure that your code is working fine. RecyclerView 可见性消失/可见是为了确保您的代码工作正常。 As you can see that your bookmark view is coming proper.
如您所见,您的书签视图正在正常运行。
The issue in fragment_home layout. fragment_home 布局中的问题。 FrameLayout and Recyclerview, both have acquired full screen.
FrameLayout 和 Recyclerview,都获得了全屏。 So when you click on RV items, It is opening bookmark but it is not visible to you.
因此,当您单击 RV 项目时,它正在打开书签,但您看不到它。
FrameLayout in fragment_home is working as a container for Bookmark fragment. fragment_home 中的 FrameLayout 用作书签片段的容器。
Since you are using NavController, so use this to open your Bookmark fragment like you do to open HomeFragment.由于您使用的是 NavController,因此可以像打开 HomeFragment 一样使用它来打开书签片段。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.