【发布时间】:2019-10-06 13:56:39
【问题描述】:
这使用了RecyclerView、NavigationBar 和 2 个片段(主页和书签)。
Home_fragment.xml 实现 RecyclerView 并有 1 个 FrameLayout 在打开新片段(书签片段)时用作布局容器。
我正在使用一个界面来处理对项目的点击。
问题是在单击 RecyclerView 项目时打开新片段时,片段打开但仍保留在 RecyclerView 后面。
我在 OnClick 方法中使用了recyclerView.setVisibility(View.GONE),它可以工作,但我认为它会永久隐藏 RecyclerView,从书签导航回主页时会显示空白布局。
为了在导航返回时使其可见,我将recyclerView.setVisibility(View.VISIBLE) 包含在位于Main_Activity.java 中的 OnBackPressed 中,但如果我单击返回按钮,它会突然停止应用程序。
我检查了这个Fragment doesn't replace and hide another fragment,但使用建议的解决方案没有任何区别。
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"));
// + a few more items
myAdapter = new MyAdapter(data_items, this);
recyclerView.setAdapter(myAdapter);
myAdapter.notifyDataSetChanged();
}
@Override
public void onNoteClick(int position) {
Intent intent = new Intent(getContext(), NewActivity.class);
startActivity(intent);
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"
android:id="@+id/frag">
<!-- TODO: Update blank fragment layout -->
<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" />
</androidx.constraintlayout.widget.ConstraintLayout>
更多详情: 我问了一个与这个项目有关的不同问题。 Fragment won't open on RecyclerView item click
我需要做什么才能让它工作?
【问题讨论】:
-
只需更改您的 FrameLayout 标高 ViewCompat.setElevation(View, int)
-
请输入您的新片段代码。
-
@majidghafouri 检查我问的另一个问题,它包括所有代码stackoverflow.com/questions/58049572/…
-
@AnasMehar 我把它放在 OnNoteClick 方法中,但它什么也没做。新片段仍然在 RecyclerView 后面打开
-
@Traveller9211 然后设置recyclerview海拔-1和framelayout 2
标签: android android-fragments android-recyclerview