所以我用SwipeRefreshLayout和片段的,我的工作在一個簡單的應用程序,我有三個文件: 1.app_bar_main.xml含有的FrameLayout這對碎片的容器繼承人的代碼:如何在不重疊的情況下切換片段?
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout 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"
android:fitsSystemWindows="true"
tools:context="com.example.surafel.tryingfragment.MainActivity">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<FrameLayout
android:id="@+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="58dp"
>
</FrameLayout>
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="@dimen/fab_margin"
android:src="@android:drawable/ic_dialog_email" />
</android.support.design.widget.CoordinatorLayout>
2.fragment_main.xml包含主要片段和SwipeRefreshLayout繼承人的代碼的組件:
<android.support.v4.widget.SwipeRefreshLayout 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"
android:id="@+id/refreshMainContent"
tools:context="com.example.surafel.tryingfragment.MainFragment">
<ScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="MAIN"
android:padding="8dp"
android:textColor="#fff"
android:background="@color/colorPrimary"
android:textSize="28sp"
android:id="@+id/buttonmain"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
/>
</RelativeLayout>
</ScrollView>
</android.support.v4.widget.SwipeRefreshLayout>
3.MainFragment.java這是爲fragment_main.xml繼承人的代碼中的Java代碼
//i have imported all the necessary classes
public class MainFragment extends Fragment implements SwipeRefreshLayout.OnRefreshListener {
SwipeRefreshLayout swipeView;
public MainFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View layout = inflater.inflate(R.layout.fragment_main,container,false);
swipeView = (SwipeRefreshLayout) layout.findViewById(R.id.refreshMainContent);
swipeView.setColorSchemeColors(getResources().getColor(android.R.color.holo_blue_dark),getResources().getColor(android.R.color.holo_red_dark),getResources().getColor(android.R.color.holo_green_light),getResources().getColor(android.R.color.holo_orange_dark));
swipeView.setOnRefreshListener(this);
return layout;
}
public void onRefresh() {
Log.d("Refreshing","The refresh is working");
}
}
4,我也有MainActivity.java其在這裏管理片段交易代碼: - 當一個菜單項被選中它傳遞的setFragment方法的位置和方法使用switch語句
需要照顧它private void setFragment(int position) {
FragmentManager fragmentManager;
FragmentTransaction fragmentTransaction;
switch (position) {
case 0:
fragmentManager = getSupportFragmentManager();
fragmentTransaction = fragmentManager.beginTransaction();
MainFragment mainFragment = new MainFragment();
fragmentTransaction.replace(R.id.fragmentContainer, mainFragment);
fragmentTransaction.commit();
break;
case 1:
fragmentManager = getSupportFragmentManager();
fragmentTransaction = fragmentManager.beginTransaction();
GradesFragment gradesFragment = new GradesFragment();
fragmentTransaction.replace(R.id.fragmentContainer, gradesFragment);
fragmentTransaction.commit();
break;
}
}
在程序啓動時的主要片段的負載和寄託都工作正常樣片段之間的切換,但是當我開始刷新主片段 復新出現,但是當我切換到等級片段而其刷新它重疊的兩個片段的等級片段將出現在主片段下。 但當我等待刷新完成並切換它的工作正常。 我試圖停止退票時,等級片段是可見的,但它似乎解決問題。 所以任何人都可以請幫助我,我張貼所有的代碼,因爲我認爲它可能是有用的,謝謝。
請檢查是否在您的framelayout上有片段可用。如果包含片段,則必須將其替換,否則必須添加新片段。 –
是的,它可用它工作正常,無需刷新 – surafel
你還沒有把檢查的條件是framelayout包含片段或不?所以首先把這樣的條件,如 'http://stackoverflow.com/questions/18445264/',如果沒有在framelayout上返回片段,那麼你必須使用fragmentTransaction.add否則fragmentTransaction.replace .. –