如何在SwipeRefreshLayout中设置自定义刷新视图?

时间:2015-11-23 07:20:44

标签: android swiperefreshlayout

我必须使用自定义视图更改默认刷新视图。有人能告诉我这样做吗?

任何帮助都将不胜感激。

1 个答案:

答案 0 :(得分:4)

你走了:

第1步:Gradle依赖

  

compile' com.reginald.swiperefresh:library:1.1.1'

第2步:xml config

<com.reginald.swiperefresh.CustomSwipeRefreshLayout
    xmlns:swiperefresh="http://schemas.android.com/apk/res-auto"
    android:id="@+id/swipelayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    swiperefresh:refresh_mode="pull_mode"
    swiperefresh:keep_refresh_head="true"
    swiperefresh:enable_top_progress_bar="true"
    swiperefresh:time_out_refresh_complete="2000"
    swiperefresh:time_out_return_to_top="1000"
    swiperefresh:return_to_top_duration="500"
    swiperefresh:return_to_header_duration="500"
    swiperefresh:top_progress_bar_color_1="@color/common_red"
    swiperefresh:top_progress_bar_color_2="#ee5522"
    swiperefresh:top_progress_bar_color_3="#ffa600"
    swiperefresh:top_progress_bar_color_4="@color/common_yellow">

<!-- Attention: you can add ONLY one view in CustomSwipeRefreshLayout either in xml or java code -->

    <ListView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:cacheColorHint="#00000000"
        android:id="@+id/listview">
    </ListView>

</com.reginald.swiperefresh.CustomSwipeRefreshLayout>

第3步:Java代码:

// Set a custom HeadView. use default HeadView if not provided
    mCustomSwipeRefreshLayout.setCustomHeadview(new MyCustomHeadView(this));
    // Set refresh mode to swipe mode
    // (CustomSwipeRefreshLayout.REFRESH_MODE_PULL or CustomSwipeRefreshLayout.REFRESH_MODE_SWIPE)
    mSwipeRefreshLayout.setRefreshMode(CustomSwipeRefreshLayout.REFRESH_MODE_SWIPE);
    // Enable the top progress bar
    mSwipeRefreshLayout.enableTopProgressBar(true);
    // Keep the refreshing head movable(true stands for fixed) on the top
    mSwipeRefreshLayout.enableTopRefreshingHead(false);
    // Timeout to return to original state when the swipe motion stay in the same position
    mSwipeRefreshLayout.setmReturnToOriginalTimeout(200);
    // Timeout to show the refresh complete information on the refreshing head.
    mSwipeRefreshLayout.setmRefreshCompleteTimeout(1000);
    // Duration of the animation from the top of the content view to parent top.(e.g. when refresh complete)
    mCustomSwipeRefreshLayout.setReturnToTopDuration(500);
    // Duration of the animation from the top of the content view to the height of header.(e.g. when content view is released)
    mCustomSwipeRefreshLayout.setReturnToHeaderDuration(500);
    // Set progress bar colors
    mSwipeRefreshLayout.setProgressBarColor(color1, color2,color3, color4);
    // Set the height of Progress bar, in dp. Default is 4dp
    mCustomSwipeRefreshLayout.setProgressBarHeight(4);
    // Set the resistance factor. Default is 0.5f
    mCustomSwipeRefreshLayout.setResistanceFactor(0.5f);
    // Set the trigger distance. in dp. Default is 100dp
    // (pull -> release distance for PULL mode or swipe refresh distance for SWIPE mode)
    mCustomSwipeRefreshLayout.setTriggerDistance(100);

第4步:处理刷新事件

CustomSwipeRefreshLayout mSwipeRefreshLayout;

//set onRefresh listener
    mSwipeRefreshLayout.setOnRefreshListener(new CustomSwipeRefreshLayout.OnRefreshListener() {
        @Override
        public void onRefresh() {
            // do something here when it starts to refresh
            // e.g. to request data from server
        }
    });

    //set RefreshCheckHandler (OPTIONAL)
    mCustomSwipeRefreshLayout.setRefreshCheckHandler(new CustomSwipeRefreshLayout.RefreshCheckHandler() {
        @Override
        public boolean canRefresh() {
            // return false when you don't want to trigger refresh
            // e.g. return false when network is disabled.
        }
    });

    // to tell the CustomSwipeRefreshLayout when your refreshing process is complete
    // e.g. when received data from server
    mSwipeRefreshLayout.refreshComplete();

第5步:处理滚动事件

// to handle scrolling up event
    mCustomSwipeRefreshLayout.setScrollUpHandler(new CustomSwipeRefreshLayout.ScrollUpHandler() {
        @Override
        public boolean canScrollUp(View view) {
            // e.g. check whether the scroll up event can be consumed by the RecyclerView
            if (view == mRecyclerView){
                return ((GridLayoutManager)mLayoutManager).findFirstCompletelyVisibleItemPosition() != 0;
            }
            return false;
        }
    });

    // to handle scrolling left of right event
    mCustomSwipeRefreshLayout.setScrollLeftOrRightHandler(new CustomSwipeRefreshLayout.ScrollLeftOrRightHandler() {
        @Override
        public boolean canScrollLeftOrRight(View view, int direction) {
            // e.g. check whether the scroll left or right event can be consumed by your Custom View
            if (view == myCustomView){
              return myCustomView.canScrollHorizontal(direction);
            }
            return false;
        }
    });

我只想发布链接,但SOF不允许我这样做。无论如何,这是链接https://github.com/xyxyLiu/SwipeRefreshLayout