【发布时间】: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