【问题标题】:Android- FragmentTransaction.replace() works only onceAndroid- FragmentTransaction.replace() 只工作一次
【发布时间】:2016-09-23 13:09:54
【问题描述】:

我使用导航抽屉和 SwipeRefreshLayout 作为主要内容,当用户在导航抽屉中选择一个菜单项时,我想用另一个片段替换 SwipeRefreshLayout 中的片段。

这就是我的onNavigationItemSelected() 的样子:

// Handle navigation view item clicks here.
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
selectedNavItem = item.getItemId();

if(selectedNavItem == R.id.nav_files){
    filesFragment = new FilesFragment();
    fm = getSupportFragmentManager();
    FragmentTransaction transaction = fm.beginTransaction();
    transaction.replace(R.id.swipeLayout,filesFragment,"files");
    transaction.commit();

} else if(selectedNavItem == R.id.nav_accounts){
    accountsFragment = new AccountsFragment();
    fm = getSupportFragmentManager();
    FragmentTransaction transaction = fm.beginTransaction();
    transaction.replace(R.id.swipeLayout,accountsFragment,"accounts");
    transaction.commit();
}
return true;

但这永远不会奏效。当我单击导航抽屉中的项目时,该片段被空白屏幕替换。我的onCreate 方法也使用FragmentTransaction.replace(),但这似乎工作正常。

我也试过FragmentTransaction.remove() 然后FragmentTransaction.add() 但即使这样似乎也不起作用。

编辑:布局文件:

导航抽屉内容视图的布局:

<android.support.v4.widget.SwipeRefreshLayout  
    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"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="com.harshallele.cloudpool.MainActivity"
    tools:showIn="@layout/app_bar_main"
    android:id="@+id/swipeLayout">


</android.support.v4.widget.SwipeRefreshLayout>

这包含在另一个布局文件中,该文件包含一个包含工具栏的CoordinatorLayout。该文件又位于android.support.v4.widget.DrawerLayout 内的活动主布局文件中

(基本上这是Android Studio在添加Activity时提供的Navigation Drawer Activity)

FilesFragment 的布局:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".fragments.FilesFragment">


        <android.support.v7.widget.RecyclerView
            android:id="@+id/fileListView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:scrollbars="vertical"
        />


        <ProgressBar
            android:id="@+id/itemsLoadingProgress"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom"
            style="?android:attr/progressBarStyleHorizontal"
            android:indeterminateOnly="true"
            android:visibility="invisible"
        />

</FrameLayout>

AccountsFragment的布局(这只是默认的空白片段,因为我还没有完成):

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.harshallele.cloudpool.fragments.AccountsFragment">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="@string/hello_blank_fragment" />

</FrameLayout>

编辑 2:

AccountsFragment:

public class AccountsFragment extends Fragment {


    public AccountsFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_accounts, container, false);
    }

}

【问题讨论】:

  • 请发布您的 XML。
  • @Bryan 添加了 xml 文件
  • 也发布您的AccountsFragment 课程之一。到目前为止,我看到了一个问题,但我认为它不能解释为什么您的片段不会显示。
  • 另外,只是一个提示;粘贴代码时,确保突出显示整个代码块,然后点击编辑器中的{} 按钮。不要只在第一行和最后一行代码之前放四个空格。
  • @Bryan 添加了AccountsFragment

标签: android android-fragments navigation-drawer


【解决方案1】:

仅凭这一点代码,我们无法为您提供太多帮助。问题可能是:

  1. FilesFragmentAccountsFragment 没有以正确的方式初始化;

  2. id为swipeLayoutLayout可以有visibility = gone

  3. FilesFragmentAccountsFragmentLayout可以为空;

这只是您的代码无法正常工作的一些无限原因,因此请分享更多关于两个Fragments 和相关XML 的代码。

【讨论】:

  • 如果问题缺乏细节,并且有多种潜在原因,那么发布答案还为时过早。等待 OP 提供更多信息,然后针对实际问题创建解决方案。
  • 添加布局文件和 AccountsFragment
猜你喜欢
  • 2020-09-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-02-17
  • 2021-06-10
  • 1970-01-01
  • 1970-01-01
  • 2011-08-30
相关资源
最近更新 更多