android - 使用swiper refresh layout + recycle view 实现下拉刷新

访问量: 161

参考:https://developer.android.com/training/swipe/respond-refresh-request.html

先修改XML, 在recycle view 的外围增加 swipe refresh layout:

    <android.support.v4.widget.SwipeRefreshLayout
        android:paddingTop="20dp"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/swipe_refresh_market"
        >
        <android.support.v7.widget.RecyclerView
            android:id="@+id/recycler_view"
            android:layout_width="match_parent"
            android:layout_height="match_parent"/>
    </android.support.v4.widget.SwipeRefreshLayout>

然后,记得在fragment 或者activity中:

        swipeRefreshLayout = (SwipeRefreshLayout) rootView.findViewById(R.id.swipe_refresh_market);
        swipeRefreshLayout.setColorSchemeColors(getResources().getColor(R.color.market_loader_icon));

        swipeRefreshLayout.setOnRefreshListener(
                new SwipeRefreshLayout.OnRefreshListener(){

                    @Override
                    public void onRefresh() {
                        Log.d(TAG, "== onRefresh");
                        refreshData(true);
                        swipeRefreshLayout.setRefreshing(false);
                    }
                }
        );

        recyclerView = (RecyclerView) rootView.findViewById(R.id.recycler_view);
        final LinearLayoutManager layoutManager = new LinearLayoutManager(getContext());
        recyclerView.setLayoutManager(layoutManager);
        adapter = new MarketAdapter();
        recyclerView.setAdapter(adapter);

订阅/RSS Feed

Subscribe

分类/category