2016-12-12 61 views
0

所以我用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; 
     } 

    } 

在程序启动时的主要片段的负载和寄托都工作正常样片段之间的切换,但是当我开始刷新主片段 复新出现,但是当我切换到等级片段而其刷新它重叠的两个片段的等级片段将出现在主片段下。 但当我等待刷新完成并切换它的工作正常。 我试图停止退票时,等级片段是可见的,但它似乎解决问题。 所以任何人都可以请帮助我,我张贴所有的代码,因为我认为它可能是有用的,谢谢。 this is the screenshot

+0

请检查是否在您的framelayout上有片段可用。如果包含片段,则必须将其替换,否则必须添加新片段。 –

+0

是的,它可用它工作正常,无需刷新 – surafel

+0

你还没有把检查的条件是framelayout包含片段或不?所以首先把这样的条件,如 'http://stackoverflow.com/questions/18445264/',如果没有在framelayout上返回片段,那么你必须使用fragmentTransaction.add否则fragmentTransaction.replace .. –

回答

1

在onDestroy()或onPause()中试试这个。它会为你工作。

@Override 
public void onPause() { 
     super.onPause(); 
    if (swipeRefreshLayout != null) { 
      swipeRefreshLayout.setRefreshing(false); 
      swipeRefreshLayout.destroyDrawingCache(); 
      swipeRefreshLayout.clearAnimation(); 
    } 
} 
+0

这些是活动的方法吗?我对android编程 – surafel

+0

是一种新颖的,也可以在Fragment中使用。 –

+0

@surafel也更新了帖子。试试这个。 –

相关问题