SwipeRefreshLayout簡單說明

SwipeRefreshLayout是谷歌官方的下拉刷新控件,代碼在V4包中。java

使用SwipeRefreshLayout有幾個要注意的:android

  1. SwipeRefreshLayout和ScrollView同樣只能有一個字控件。ide

  2. setOnRefreshListener設置監聽刷新。spa

  3. setProgressBackgroundColor設置刷新時圓形進度條的背景色code

  4. setColorSchemeResources設置刷新時進度條顏色xml

  5. setRefreshing設置刷新狀態事件

  6. setSize設置大小,如今只有SwipeRefreshLayout.DEFAULT,SwipeRefreshLayout.LARGEip

若是在SwipeRefreshLayout的子控件中嵌套了其餘可滾動控件例如get

 <android.support.v4.widget.SwipeRefreshLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical" >
                <ScrollView
                    android:layout_width="match_parent"
                    android:layout_height="0dip"
                    android:layout_weight="1"
                    android:id="@id/svCar"
                    android:fadingEdge="none" >
                    <TextView
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"/>
                </ScrollView>
                    <TextView
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"/>
        </LinearLayout>        
</android.support.v4.widget.SwipeRefreshLayout>

當ScrollView滾動到下方後,向下拉ScrollView不會滾動而會出現下拉刷新動做,這時須要將ScrollView設置onTouch事件
it

    OnTouchListener onTouchListener=new OnTouchListener() {
        
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
            case MotionEvent.ACTION_MOVE:
            case MotionEvent.ACTION_UP:
                if (svCar.getScrollY() > 0) {
                    srlCar.setEnabled(false);
                } else {
                    srlCar.setEnabled(true);
                }
                break;
            }
            return false;
        }
    };
相關文章
相關標籤/搜索