Android內建下拉重新整理組件SwipeRefreshLayout

來源:互聯網
上載者:User

標籤:

也許下拉重新整理之前,你可能會使用一些第三方的開源庫,例如PullToRefresh, ActionBar-PullToRefresh等待,但現在有的正式組成部分---SwipeRefreshLayout,SwipeRefreshLayout是Google在support v4 19.1版本號碼的library更新的一個下拉重新整理組件,使用起來非常方便,能夠非常方便的實現Google Now的重新整理效果。

使用官方內建的控制項能夠保證通用性以及風格。SwipeRefreshLayout是繼承ViewGroup。來實現下拉重新整理,它能夠包括其它組件,使用過後感覺非常easy。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:paddingLeft="@dimen/activity_horizontal_margin"    android:paddingRight="@dimen/activity_horizontal_margin"    android:paddingTop="@dimen/activity_vertical_margin"    android:paddingBottom="@dimen/activity_vertical_margin"    tools:context="com.example.myapplication2.app.MainActivity">    <android.support.v4.widget.SwipeRefreshLayout        android:id="@+id/swip"        android:layout_width="match_parent"        android:layout_height="match_parent">        <ListView            android:id="@+id/listview"            android:layout_width="match_parent"            android:layout_height="match_parent">            </ListView>        </android.support.v4.widget.SwipeRefreshLayout></RelativeLayout>

import android.os.Handler;import android.os.Message;import android.support.v7.app.ActionBarActivity;import android.support.v4.widget.SwipeRefreshLayout;import android.support.v4.widget.SwipeRefreshLayout.OnRefreshListener;import android.os.Bundle;import android.view.Menu;import android.view.MenuItem;public class MainActivity extends ActionBarActivity implements OnRefreshListener{    private SwipeRefreshLayout swipeRefreshLayout;    Handler handler = new Handler(){        @Override        public void handleMessage(Message msg) {            super.handleMessage(msg);            swipeRefreshLayout.setRefreshing(false);        }    };    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        swipeRefreshLayout = (SwipeRefreshLayout)findViewById(R.id.swip);        swipeRefreshLayout.setOnRefreshListener(this);        swipeRefreshLayout.setColorScheme(android.R.color.holo_blue_bright,                android.R.color.holo_green_light,                android.R.color.holo_orange_light,                android.R.color.holo_red_light);    }    @Override    public void onRefresh() {        handler.sendEmptyMessageDelayed(1, 5000);    }}

另外,在上述onRefresh()功能被實現,以獲得資料的功能和更新資料,當您完成資料更新,轉讓swipeRefreshLayout.setRefreshing(false);要關閉重新整理。

Android內建下拉重新整理組件SwipeRefreshLayout

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.