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

Here in your fragment i found the problem is with set up the refresh listener

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);
}

}

and make sure you have added supportV4 library

try as above this will help you,happy coding :)

于 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 に答える
0

このリンクを参照してください。これが役立つ場合があります

https://developer.android.com/samples/SwipeRefreshLayoutBasic/src/com.example.android.swiperefreshlayoutbasic/SwipeRefreshLayoutBasicFragment.html

http://antonioleiva.com/swiperefreshlayout/

于 2014-12-18T07:33:20.467 に答える