Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/201.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 在RecyclerView项上打开另一个片段单击';不要隐藏RecylerView_Android_Android Fragments_Android Recyclerview - Fatal编程技术网

Android 在RecyclerView项上打开另一个片段单击';不要隐藏RecylerView

Android 在RecyclerView项上打开另一个片段单击';不要隐藏RecylerView,android,android-fragments,android-recyclerview,Android,Android Fragments,Android Recyclerview,这使用了RecyclerView、NavigationBar和2个片段(主页和书签)。 Home\u fragment.xml实现了RecyclerView,并有1个FrameLayout在打开新片段(书签片段)时用作布局容器。 我正在使用一个界面来处理对项目的点击 问题是,当单击RecyclerView项打开新片段时,片段将打开,但仍保留在RecyclerView后面 我在OnClick方法中使用了recyclerView.setVisibility(View.GONE),它可以工作,但我认为

这使用了
RecyclerView
NavigationBar
和2个片段(主页和书签)。
Home\u fragment.xml
实现了RecyclerView,并有1个
FrameLayout
在打开新片段(书签片段)时用作布局容器。 我正在使用一个界面来处理对项目的点击

问题是,当单击RecyclerView项打开新片段时,片段将打开,但仍保留在RecyclerView后面

我在OnClick方法中使用了
recyclerView.setVisibility(View.GONE)
,它可以工作,但我认为它会永久隐藏recyclerView,当从书签导航回主页时,它会显示一个空白布局。 为了使它在返回时可见,我在OnBackPressed中加入了
recyclerView.setVisibility(View.VISIBLE)
,它位于
Main\u Activity.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"));
    // + 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();
}}
公共类HomeFragment扩展片段实现MyAdapter.OnNoteListener{
回收视图回收视图;
我的适配器我的适配器;
LinkedList data_items=新建LinkedList();
@凌驾
创建视图上的公共视图(布局、充气机、视图组容器、,
Bundle savedInstanceState){
返回充气机。充气(R.layout.fragment_home,容器,假);
}
@凌驾
已创建公用void onview(@NonNull视图,@Nullable Bundle savedInstanceState){
super.onViewCreated(视图,savedInstanceState);
recyclerView=view.findViewById(R.id.recyclerView);
setLayoutManager(新的LinearLayoutManager(getContext());
数据项。添加(新数据项(R.drawable.a,“粉色”,“这是粉色”);
//+还有几项
myAdapter=新的myAdapter(数据项,此项);
recyclerView.setAdapter(myAdapter);
myAdapter.notifyDataSetChanged();
}
@凌驾
公共void onNoteClick(内部位置){
Intent Intent=newintent(getContext(),NewActivity.class);
星触觉(意向);
FragmentManager FragmentManager=getActivity().getSupportFragmentManager();
FragmentTransaction FragmentTransaction=fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.layout_容器,newbookmarkfragment());
fragmentTransaction.addToBackStack(空);
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>


我需要做什么才能让它工作?

这就是我让它工作的方式-

  • activity\u main.xml
    应该包含
    layout\u容器(FrameLayout)
    。没有别的了
  • HomeFragment.xml
    应包含
    RecyclerView
    导航抽屉
  • Main_activity.java应该有代码来替换
    layout_容器
    HomeFragment只是将代码放在那里,不需要任何侦听器
  • HomeFragment.java
    中,我们可以在RecyclerView项上设置侦听器以打开另一个 片段/活动

只需更改FrameLayout elevation ViewCompat。setElevation(View,int)请放入新的片段代码。@majidghafouri检查我问的另一个问题,它包括我在OnNoteClick方法中放入的所有代码@AnasMehar,但它没有做任何事情。新的片段仍然在后面打开RecyclerView@Traveller9211然后设置recyclerview标高-1和框架布局2