[含執行個體Demo]Google官方下拉重新整理組件---SwipeRefreshLayout

來源:互聯網
上載者:User

官方連結:https://developer.android.com/reference/android/support/v4/widget/SwipeRefreshLayout.html#inhfields


情景:前天小海告訴我說Google除了自己的重新整理控制項,沒錯就是SwipeRefreshLayout。

 SwipeRefreshLayout字面意思就是下拉重新整理的布局,繼承自ViewGroup,在support v4相容包下(android.support.v4.widget.SwipeRefreshLayout),但必須把你的support library的版本升級到19.1。 提到下拉重新整理大家一定對ActionBarPullToRefresh比較熟悉,而如今google推出了更官方的下拉重新整理組件,這無疑是對開發人員來說比較好的訊息。利用這個組件可以很方便的實現Google Now的重新整理效果,見:


xml布局檔案

This layout should be made the parent of the view that will be refreshed as a result of the gesture and can only support one direct child.

只要在需要重新整理的控制項最外層加上SwipeRefreshLayout,然後他的child首先是可滾動的view,如ScrollView或者ListView。
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:id="@+id/container"    android:layout_width="match_parent"    android:layout_height="match_parent"    tools:ignore="MergeRootFrame" >    <android.support.v4.widget.SwipeRefreshLayout        android:id="@+id/swipe_container"        android:layout_width="match_parent"        android:layout_height="match_parent" >        <ListView            android:id="@+id/list"            android:layout_width="match_parent"            android:layout_height="match_parent" >        </ListView>    </android.support.v4.widget.SwipeRefreshLayout></FrameLayout>

Activity代碼:

package com.jabony.swiperefreshlayou;import java.util.ArrayList;import android.app.Activity;import android.os.Bundle;import android.os.Handler;import android.support.v4.widget.SwipeRefreshLayout;import android.widget.ListView;import com.jabony.swiperefreshlayout.R;public class SwipRefreshLayoutActivity extends Activity implementsSwipeRefreshLayout.OnRefreshListener {private SwipeRefreshLayout swipeLayout;private ListView listView;private ListViewAdapter adapter;private ArrayList<SoftwareClassificationInfo> list;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.swipe_refresh_layout);swipeLayout = (SwipeRefreshLayout) findViewById(R.id.swipe_container);swipeLayout.setOnRefreshListener(this);//載入顏色是迴圈播放的,只要沒有完成重新整理就會一直迴圈,color1>color2>color3>color4swipeLayout.setColorScheme(android.R.color.white,android.R.color.holo_green_light,android.R.color.holo_orange_light, android.R.color.holo_red_light);list = new ArrayList<SoftwareClassificationInfo>();list.add(new SoftwareClassificationInfo(1, "asdas"));listView = (ListView) findViewById(R.id.list);adapter = new ListViewAdapter(this, list);listView.setAdapter(adapter);}public void onRefresh() {new Handler().postDelayed(new Runnable() {public void run() {swipeLayout.setRefreshing(false);list.add(new SoftwareClassificationInfo(2, "ass"));adapter.notifyDataSetChanged();}}, 3000);}}

主要方法:

  • setOnRefreshListener(OnRefreshListener): 為布局添加一個Listener
  • setRefreshing(boolean): 顯示或隱藏重新整理進度條
  • isRefreshing(): 檢查是否處於重新整理狀態
  • setColorScheme(): 設定進度條的顏色主題,最多能設定四種

總結:每次當有朋友對我說有新的事物時,都非常開心,因為這又是一個進步的機會,希望大家與我一同進步。Google也在不斷完善自己的SDK,那麼我們還有什麼理由不跟進時代學習新的設計呢,快清明了 祝大家節日愉快,最好不加班哦!

附件:稍後附上(免積分)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.