与隐藏片段相互作用 - android



我以我首先隐藏当前片段然后添加新的片段的方式将片段添加到我的活动中。我的问题是,当我展示我的新片段并开始与他互动时,它也与上一个互动。

我用来添加新的和隐藏当前片段的代码是:

public void add(int containerViewId, Fragment fragment, boolean addToBackStack){
    FragmentTransaction ft = fragmentManager.beginTransaction().hide(currentFragment).add(containerViewId, fragment);
    if (addToBackStack){
        ft.addToBackStack(null);
    }
    ft.commit();
    currentFragment = fragment;
    backStackCount++;
}

发生了什么事,以及如何隐藏片段,以便我只能与最后一个添加的片段进行互动?replace不是一个选项,因为我不想删除当前片段。

我也有类似的问题。我不知道可能是什么可能创建这个问题,但是我为解决它做了什么,那就是我将一个单击的侦听器设置为片段的最外部布局。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/top_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFFFFF"
android:clickable="false"
android:orientation="vertical"
tools:context=".Fragments.TopicsFragment">
...other components
</LinearLayout>

片段:

LinearLayout topLayout = (LinearLayout) fragmentView.findViewById(R.id.top_layout);
topLayout.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            //do nothing 
        }
    });

您也可能会看到我在此最外面的布局中添加了#ffffff的背景,因为在mycase中,上一个片段的内容在新的片段后面也可见。因此,这也解决了这个问题。

最新更新