如何在不重叠的情况下在片段之间切换?
How to switch between fragments without overlapping?
所以我正在使用 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 这是 java 代码 fragment_main.xml 这是代码
//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;
}
}
当程序开始加载主片段时,一切正常,就像在片段之间切换一样,但是当我开始刷新主片段时
刷新器出现但是当我切换到成绩片段时它刷新它与两个片段重叠等级片段将出现在主片段下方。
但是当我等待刷新完成并切换片段时它工作正常。
当成绩片段可见时,我试图停止引用,但它似乎确实解决了问题。
所以任何人都可以帮助我,我发布了所有代码,因为我认为它可能有用,谢谢。
在 onDestroy() 或 onPause() 中试试这个。它会为你工作。
@Override
public void onPause() {
super.onPause();
if (swipeRefreshLayout != null) {
swipeRefreshLayout.setRefreshing(false);
swipeRefreshLayout.destroyDrawingCache();
swipeRefreshLayout.clearAnimation();
}
}
所以我正在使用 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 这是 java 代码 fragment_main.xml 这是代码
//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;
}
}
当程序开始加载主片段时,一切正常,就像在片段之间切换一样,但是当我开始刷新主片段时
刷新器出现但是当我切换到成绩片段时它刷新它与两个片段重叠等级片段将出现在主片段下方。
但是当我等待刷新完成并切换片段时它工作正常。
当成绩片段可见时,我试图停止引用,但它似乎确实解决了问题。
所以任何人都可以帮助我,我发布了所有代码,因为我认为它可能有用,谢谢。
在 onDestroy() 或 onPause() 中试试这个。它会为你工作。
@Override
public void onPause() {
super.onPause();
if (swipeRefreshLayout != null) {
swipeRefreshLayout.setRefreshing(false);
swipeRefreshLayout.destroyDrawingCache();
swipeRefreshLayout.clearAnimation();
}
}