所以我使用SwipeRefreshLayout和Fragments作为我正在处理的简单应用程序,我有三个文件: 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.i还有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;
}
}
当程序启动时,主片段加载和everthing工作正常,如在片段之间切换,但是当我开始刷新主片段时 复习出现但是当我切换到grade碎片时,它刷新它与两个碎片重叠,等级碎片将出现在主碎片下面。 但是当我等待刷新完成并切换片段时,它工作正常。 当等级片段可见时,我试图停止引用,但它似乎解决了问题。 所以任何人都可以帮助我,我发布了所有代码,因为我认为它可能是有用的,谢谢。
答案 0 :(得分:1)
在onDestroy()或onPause()中尝试此操作。它会对你有用。
@Override
public void onPause() {
super.onPause();
if (swipeRefreshLayout != null) {
swipeRefreshLayout.setRefreshing(false);
swipeRefreshLayout.destroyDrawingCache();
swipeRefreshLayout.clearAnimation();
}
}