0

我正在尝试在 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 的类一样。你能建议我如何在这里实现它吗?

4

3 回答 3

1

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

public static class HomeFragment extends Fragment implements OnRefreshListener{
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(this);
    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);
}

}

并确保您已添加 supportV4 库

如上所述尝试这将帮助你,快乐的编码:)

于 2015-06-25T11:11:14.597 回答
0

您应该在您的Fargment中实现 OnRefreshListener

public static class HomeFragment extends Fragment implements SwipeRefreshLayout.OnRefreshListener

并设置swipeLayout.setOnRefreshListener(this);

于 2014-12-18T07:39:50.363 回答