5/15/2015

[Android] 下拉更新 SwipeRefreshLayout

Layout Xml

                <android.support.v4.widget.SwipeRefreshLayout
                    android:id="@+id/laySwipe"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent" >

                        <ListView
                            ... />
                </android.support.v4.widget.SwipeRefreshLayout>



Java Code

OnRefreshListener onSwipeToRefresh = new OnRefreshListener() {
        @Override
        public void onRefresh() {
            laySwipe.setRefreshing(true);
            //dosomething
            laySwipe.setRefreshing(false);
        }
    };

((SwipeRefreshLayout)findViewById(R.id.laySwipe)).setOnRefreshListener(onSwipeToRefresh);


幫listview補上下面的code避免滑動衝突


    private OnScrollListener onListScroll = new OnScrollListener() {

        @Override
        public void onScrollStateChanged(AbsListView view, int scrollState) {

        }

        @Override
        public void onScroll(AbsListView view, int firstVisibleItem,
                int visibleItemCount, int totalItemCount) {
            if (firstVisibleItem == 0) {
                laySwipe.setEnabled(true);
            }else{
                laySwipe.setEnabled(false);
            }
        }
    };

沒有留言:

張貼留言