相关疑难解决方法(0)

Android替换片段仍会显示一些被替换的片段

我有一个标签,其中包含带有searchview和listview的线性布局.当用户单击其中一个搜索结果时,我想将该布局替换为包含所选内容详细信息的另一个布局.

当我替换搜索片段时,只会替换布局的listview部分会发生什么; 搜索视图在屏幕上仍然可见并处于活动状态.

这是我的搜索布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/search_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <SearchView
        android:id="@+id/public_calendar_search"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:iconifiedByDefault="false"
    </SearchView>

    <ListView
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@android:id/list"
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight="1.0"
        android:choiceMode="singleChoice" />

    <TextView
        android:id="@android:id/empty"
        android:layout_width="match_parent"
        android:layout_height="fill_parent"
        android:gravity="center"/>

</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

这是详细布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/detail_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:baselineAligned="false"
        android:orientation="vertical" >

        <Space
            android:layout_width="0dip"
            android:layout_height="20dip"
            android:layout_weight="1" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:baselineAligned="false"
        android:orientation="horizontal" >

        <Space
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight=".1" />

        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight=".8"
            android:text="@string/label_Name" />

        <Space
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight=".1" />
    </LinearLayout>

    .........

</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

我的代码替换搜索片段: …

android android-fragments

15
推荐指数
3
解决办法
2万
查看次数

以前的片段在新片段下可见

我有一个带有ViewPager的TabLayout.ViewPager有四个片段F1,F2,F3和F4.F1包含一个FrameLayout,它可以有2个片段F11和F12.最初我在FrameLayout中使用以下代码添加F11.

Fragment11 fragment11 = new Fragment11();
fragment11.setArguments(getActivity().getIntent().getExtras());
getActivity().getSupportFragmentManager().beginTransaction()
                .add(R.id.fragment_container, fragment11, Constants.FRAGMENT_11)
                .commit();
Run Code Online (Sandbox Code Playgroud)

F11包含一个ListView.当我单击此ListView中的任何项目/行时,F11将替换为F12.F12是一个细节片段.

Fragment12 fragment12 = new Fragment12();
FragmentTransaction fragmentTransaction = getActivity().getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.fragment_container, fragment12, Constants.FRAGMENT_12)
                    .addToBackStack(null)
                    .commit();
Run Code Online (Sandbox Code Playgroud)

对于正常的应用流程,它工作正常.现在假设我现在在Fragment12中并按下主页按钮.现在我打开任何繁重的应用程序(相机或任何其他应用程序)从内存中删除我的应用程序.现在我又开始了我的应用程序.

现在有两个Fragment11和一个Fragment12可见,都在同一时间.当我按下时,Fragment12被移除,现在可以看到两个Fragment11.当我单击Fragment11的ListView行时,顶部Fragment11被Fragment12替换,但是底部Fragment11仍然存在.

这就是我想要的:当应用程序从后台到达前景时,Fragment12应该是可见的,当我按下时,应该弹出Fragment12以显示Fragment11.

我怎样才能做到这一点?

android listview android-fragments

3
推荐指数
1
解决办法
1824
查看次数

标签 统计

android ×2

android-fragments ×2

listview ×1