实现SwipeRefresh布局

时间:2014-12-18 07:10:10

标签: java android android-fragments swiperefreshlayout

我正在尝试在Android中实现Pull to refresh功能。 为此,我最终使用了支持库中的SwipeRefresLayout。 但它并没有像我想的那样工作。

我需要在片段内实现它。 这是fragment_home.xml的代码

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<FrameLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

    <android.support.v4.widget.SwipeRefreshLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/swipe_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

        <ListView
            android:id="@+id/group_content"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginBottom="45dp" />
    </android.support.v4.widget.SwipeRefreshLayout>

    <com.google.android.gms.ads.AdView
        android:id="@+id/adView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_gravity="bottom"
        ads:adSize="BANNER"
        ads:adUnitId="@string/banner_ad_unit_id" >
    </com.google.android.gms.ads.AdView>
</FrameLayout>

</LinearLayout>

这是放在MainActivity.java

中的片段
public static class HomeFragment extends Fragment {
    public static final String ARG_CATEGORY_NUMBER = "category_number";
    private UiLifecycleHelper uiHelper;
    public int currentimageindex = 0;
    private SwipeRefreshLayout swipeLayout;

    public HomeFragment() {
        // Empty constructor required for fragment subclasses
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_home, container,
                false);

        //SWIPE TO REFRESH
        swipeLayout = (SwipeRefreshLayout) container.findViewById(R.id.swipe_container);
        swipeLayout.setOnRefreshListener((OnRefreshListener) getActivity());
        swipeLayout.setColorScheme(android.R.color.holo_blue_bright,
                android.R.color.holo_green_light,
                android.R.color.holo_orange_light,
                android.R.color.holo_red_light);


        ListView hlvCategory = (ListView) rootView
                .findViewById(R.id.group_content);

        AdView mAdView = (AdView) rootView.findViewById(R.id.adView);
        HomeJsonData hjd = new HomeJsonData(getActivity(), hlvCategory,
                mAdView, mDrawerList);

        hjd.execute();

        return rootView;
    }

    //SWIPE TO REFRESH
    public void onRefresh() {
        new Handler().postDelayed(new Runnable() {
            @Override public void run() {
                swipeLayout.setRefreshing(false);
            }
        }, 5000);
    }
}

但是这会给出错误,因为它无法找到SwipeRefresh Layout的类。 你能建议我怎样才能在这里实现它?

3 个答案:

答案 0 :(得分:0)

答案 1 :(得分:0)

您应该在细分中实施OnRefreshListener

public static class HomeFragment extends Fragment implements SwipeRefreshLayout.OnRefreshListener

并设置swipeLayout.setOnRefreshListener(this);

答案 2 :(得分:0)

在你的片段中,我发现问题在于设置刷新监听器

a & 4 ? ...;

}

并确保添加了supportV4库

尝试如上所述,这将有助于您,快乐编码:)