【发布时间】:2016-12-12 14:07:45
【问题描述】:
所以我正在使用 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;
}
}
当程序开始加载主片段时,一切正常,就像在片段之间切换一样,但是当我开始刷新主片段时 刷新器出现但是当我切换到成绩片段时,它刷新它与两个片段重叠,成绩片段将出现在主片段下。 但是当我等待刷新完成并切换片段时它工作正常。 当成绩片段可见时,我试图停止引用,但它似乎并没有解决问题。 所以任何人都可以帮助我,我发布了所有代码,因为我认为它可能有用,谢谢。
【问题讨论】:
-
请检查该片段是否在您的框架布局中可用。如果 is 包含片段,那么您必须替换它,否则您必须添加新片段。
-
是的,它可以正常工作,无需刷新
-
您还没有设置检查框架布局是否包含片段的条件?所以首先把那个条件像'stackoverflow.com/questions/18445264',如果在framelayout上没有返回片段那么你必须使用fragmentTransaction.add否则fragmentTransaction.replace..
-
问题依然存在...你在电脑上试过了吗?也许如果我做错了什么......或者你能指出我这个问题的其他解决方案吗?请使用带有片段和导航抽屉的 swiperefreshlayout
-
其实没有,我的电脑上没试过,不过是个建议,你能把你的截图放上来吗?
标签: android android-fragments swiperefreshlayout