[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);
            }
        }
    };
Next: [Android] 處理 Can't create handler inside thread that has not called Looper.prepare() Prev: 解決 Error:Execution failed for task 'preDexDebug'. > com.android.ide.common.process.ProcessException: org.gradle.process.internal.ExecException: Process 'command finished with non-zero exit value 1