Android SwipeRefreshLayout½Ì³Ì

ÅÝÔÚÍøÉϵÄÈÕ×Ó / ÎÄ ·¢±íÓÚ2014-10-28 12:12 µÚ´ÎÔĶÁ ListView

SwipeRefreshLayoutÒ²ÊÇÒ»ÖÖÏÂÀ­Ë¢Ð¿ؼþ£¬²»Í¬µÄËüµÄË¢ÐÂ״̬Ч¹ûºÍ´«Í³µÄPuulToRefreshÍêÈ«²»Ò»Ñù£¬¾ßÌåЧ¹û¿ÉÒԲο¼Google NowµÄË¢ÐÂЧ¹û£¬¼ûÏÂͼ£º


SwipeRefreshLayoutÒѾ­±»·Åµ½ÁËsdkÖУ¬ÔÚversion 19.1Ö®ºóSwipeRefreshLayout ±»·Åµ½support v4ÖС£


SwipeRefreshLayout¿Ø¼þÖµÔÊÐíÓÐÒ»¸ö×ÓÔªËØ£ºÎÒÃÇÏ뻬¶¯Ë¢ÐµĶÔÏó¡£ËüʹÓÃListener»úÖÆÀ´¸æÖ®³ÖÓÐSwipeRefreshLayoutµÄ×齨ij¸öʼþ·¢ÉúÁË£¬Ò²¾ÍÊÇ˵¼ÙÈçÊÇactivity³ÖÓÐSwipeRefreshLayout£¬ÄÇôactivity¾Í±ØÐëʵÏÖÒ»¸ö½Ó¿ÚÀ´½ÓÊÕ֪ͨ£¬Õâ¸ö½Ó¿ÚÖÐÐèҪʵÏÖµÄÖ÷ÒªÊÇonRefresh()·½·¨¡£

³ý´ËÖ®ÍâSwipeRefreshLayout»¹ÌṩÁËһЩ¹«¹²·½·¨£º

   setOnRefreshListener(OnRefreshListener): Ϊ²¼¾ÖÌí¼ÓÒ»¸öListener

   setRefreshing(boolean): ÏÔʾ»òÒþ²ØË¢Ð½ø¶ÈÌõ

   isRefreshing(): ¼ì²éÊÇ·ñ´¦ÓÚË¢ÐÂ״̬

setColorScheme(): ÉèÖýø¶ÈÌõµÄÑÕÉ«Ö÷Ì⣬×î¶àÄÜÉèÖÃËÄÖÖ


ÈçºÎʹÓÃ


ÎÒ½«Í¨¹ýÒ»¸ö²úÉúËæ»úÊý×ÖµÄdemoÀ´½²½âÈçºÎʹÓÃSwipeRefreshLayout£¬Ë¢ÐÂÒ»´Î¾Í²úÉúËæ»úÊý×Ö¡£


android_swipetorefreshlayout[5]

Ò»°ãSwipeRefreshLayout×÷Ϊ¸ù½Úµã±»·ÅÈë²¼¾ÖÎļþÖУº

<android.support.v4.widget.SwipeRefreshLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:id="@+id/swipe">
    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Random number:"
                android:id="@+id/lbl"/>
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/rndNum"
                android:layout_toRightOf="@id/lbl"/>
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@id/lbl"
                android:layout_centerHorizontal="true"
                android:layout_marginTop="20dp"
                android:text="Swipe to Refresh"
                style="@android:style/TextAppearance.Medium"/>
        </RelativeLayout>
    </ScrollView>
</android.support.v4.widget.SwipeRefreshLayout>


ÉÏÃæµÄ´úÂëÖÐSwipeRefreshLayoutÖ»ÓÐÒ»¸öΪscrollViewµÄ×ÓÔªËØ¡£½ÓÏÂÀ´activityÖУº

...   
@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        final SwipeRefreshLayout swipeView = (SwipeRefreshLayout) findViewById(R.id.swipe);
        final TextView rndNum = (TextView) findViewById(R.id.rndNum);
        swipeView.setColorScheme(android.R.color.holo_blue_dark, android.R.color.holo_blue_light, android.R.color.holo_green_light, android.R.color.holo_green_light);
        swipeView.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                swipeView.setRefreshing(true);
                Log.d("Swipe", "Refreshing Number");
                ( new Handler()).postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        swipeView.setRefreshing(false);
                        double f = Math.random();
                        rndNum.setText(String.valueOf(f));
                    }
                }, 3000);
            }
        });
    }
....

ËùÓеĴúÂë¶¼ÔÚonCreateÖÐÁË¡£


ÉÏÃæµÄ´úÂëºÜ¼òµ¥£¬Ö»ÐèÒª¸øSwipeRefreshLayoutÌí¼ÓÒ»¸ölistener£¬ÖµµÃ˵Ã÷µÄÊÇsetColorScheme·½·¨ÊÇÉèÖÃˢнø¶ÈÌõµÄÑÕÉ«£¬×î¶àÖ»ÄÜÉèÖÃ4ÖÖÑ­»·ÏÔʾ£¬Ä¬ÈϵÚÒ»¸öÊÇËæÓû§ÊÖÊÆ¼ÓÔØµÄÑÕÉ«½ø¶ÈÌõ¡£

Ô´Âë

githubÉÏÓÐSwipeRefreshLayoutµÄÀý×ÓÔ´Â룬µØÖ·ÔÚ£ºSwipeRefreshLayoutDemo