如何将SwipeRefreshLayout与Fragments一起使用

问题描述 投票:0回答:1

所以我使用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 inflater.inflate(R.layout.fragment_main, container, false);
    }

    public void onRefresh() {
    Log.d("Refreshing","The refresh is working");
    }

}

4.i也有MainActivity.java文件,其中包含所有必要的代码

问题是它没有改变swipeView的颜色,甚至没有改变onRefresh()方法。当我运行应用程序并向下滑动刷新时,它的颜色是黑色,它没有执行onRefresh()方法。并且没有编译时和运行时错误。所以任何人都可以帮助我,我发布了所有代码,因为我认为它可能是有用的,谢谢。

android fragment swiperefreshlayout
1个回答
0
投票

看起来你已经调用了代码inflater.inflate(R.layout.fragment_main,container,false);两次。

尝试返回对象布局而不是inflater.inflate(R.layout.fragment_main,container,false)

© www.soinside.com 2019 - 2024. All rights reserved.