對於主要內容,當我使用導航抽屜和SwipeRefreshLayout
,並且當用戶在導航抽屜中選擇菜單項時,我想要替換SwipeRefreshLayout
與另一個片段。Android- FragmentTransaction.replace()只能使用一次
這是我的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
含有toolbar.That文件的另一佈局文件,反過來,是裏面的活動的主佈局文件內android.support.v4.widget.DrawerLayout
(基本上,這是Android Studio提供的導航抽屜活動添加活動時)
佈局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
@Bryan添加了xml文件 – Guest1997
發佈您的一個'AccountsFragment'類。到目前爲止我看到一個問題,但我不認爲它解釋了爲什麼你的碎片不會顯示。 – Bryan