Android 碎片赢了';无法打开回收查看项目单击
此项目使用Android 碎片赢了';无法打开回收查看项目单击,android,android-fragments,android-recyclerview,navigation-drawer,Android,Android Fragments,Android Recyclerview,Navigation Drawer,此项目使用导航抽屉、回收视图和一个界面来检测对回收视图项目的点击 问题是当我点击这些项目时,什么也没发生。 我试图开展一项效果良好的活动。所以我认为界面没有问题 我发现了一些类似的问题,但没有一个能帮助解决这个具体问题 我做错了什么 HomeFragment.java public class HomeFragment extends Fragment implements MyAdapter.OnNoteListener { RecyclerView recyclerView; MyAda
导航抽屉
、回收视图
和一个界面来检测对回收视图项目的点击
问题是当我点击这些项目时,什么也没发生。
我试图开展一项效果良好的活动。所以我认为界面没有问题
我发现了一些类似的问题,但没有一个能帮助解决这个具体问题
我做错了什么
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();
}}
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();
}}
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可见性消失/可见是为了确保代码正常工作。 正如你所看到的,你的书签视图是正确的 家庭布局中的问题。FrameLayout和Recyclerview都获得了全屏显示。所以当你们点击RV项目时,它会打开书签,但你们看不见 fragment_home中的FrameLayout用作书签片段的容器
因为您使用的是NavController,所以使用它可以像打开HomeFragment一样打开书签片段。RecyclerView visibility gone/visible是为了确保代码正常工作。 正如你所看到的,你的书签视图是正确的 家庭布局中的问题。FrameLayout和Recyclerview都获得了全屏显示。所以当你们点击RV项目时,它会打开书签,但你们看不见 fragment_home中的FrameLayout用作书签片段的容器
因为您使用的是NavController,所以使用它来打开书签片段,就像打开HomeFragment一样。我认为问题在于片段主布局。FragmentLayout和Recyclerview都获得了全屏显示。只是为了检查一下,当你打开书签片段时,回收者视图的可见性消失了。谢谢你的评论,Kishan。你能告诉我在打开书签片段时如何隐藏RecyclerView可见性吗?RecyclerView.setVisibility(View.GONE)在onNoteClick()内任何更新。你试过了吗?是的。它可以工作,但当向后导航时,它会显示空白的recyclerView。有办法解决吗?我想问题出在你的碎片家庭布局上。FragmentLayout和Recyclerview都获得了全屏显示。只是为了检查一下,当你打开书签片段时,回收者视图的可见性消失了。谢谢你的评论,Kishan。你能告诉我在打开书签片段时如何隐藏RecyclerView可见性吗?RecyclerView.setVisibility(View.GONE)在onNoteClick()内任何更新。你试过了吗?是的。它可以工作,但当向后导航时,它会显示空白的recyclerView。有办法解决吗?
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();
}}
<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>