Android實戰簡易教程-第十九槍(SwipeRefreshLayout下拉重新整理使用執行個體),swiperefreshlayout

來源:互聯網
上載者:User

Android實戰簡易教程-第十九槍(SwipeRefreshLayout下拉重新整理使用執行個體),swiperefreshlayout

我們來看SwipeRefreshLayout的具體用法,顧名思義此組件就是一個布局,只不過要注意的是此布局內只能有一個直接子View。其實通過文檔我們可以知道SwipeRefreshLayout只不過是繼承了ViewGroup。

 查看文檔,我們可以知道,在SwipRefreshLayout中存在一個介面,通過此介面我們可以監聽滑動手勢,其實使用此組件最重要的步驟就是實現此介面的onRefresh方法,在此方法中實現資料的更新操作。如下:


介面中的方法:


 除了OnRefreshListener介面外,SwipRefreshLayout中還有一些其他重要的方法,具體如下:

 

         1、setOnRefreshListener(SwipeRefreshLayout.OnRefreshListener listener):設定手勢滑動監聽器。

 

         2、setProgressBackgroundColor(int colorRes):設定進度圈的背景色。

 

         3、setColorSchemeResources(int… colorResIds):設定進度動畫的顏色。

 

         4、setRefreshing(Boolean refreshing):設定組件的重新整理狀態。

 

         5、setSize(int size):設定進度圈的大小,只有兩個值:DEFAULT、LARGE。

下面我們通過一個執行個體來看一下具體怎麼用

1.main.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent" >    <android.support.v4.widget.SwipeRefreshLayout        android:id="@+id/swipe_refresh"        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></LinearLayout>
2.list_item.xml:

<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="wrap_content" >    <TextView        android:id="@+id/item_name"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_margin="15dp"        android:gravity="center"        android:singleLine="true"        android:textSize="16sp"        android:textStyle="bold" /></RelativeLayout>

3.ItemInfo.java:

package com.demo.downrefresh;/** * ListView中item屬性 *  * @author w.w */public class ItemInfo {/** * id */private int id;/** * name */private String name;public int getId() {return id;}public void setId(int id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}}

4.ListViewAdapter.java:

package com.demo.downrefresh;import java.util.List;import android.content.Context;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.widget.ArrayAdapter;import android.widget.TextView;/** * ListView適配器 * @author w.w */public class ListViewAdapter extends ArrayAdapter<ItemInfo> {private LayoutInflater inflater;public ListViewAdapter(Context context, List<ItemInfo> list) {super(context, 0, list);inflater = LayoutInflater.from(context);}@Overridepublic View getView(int position, View convertView, ViewGroup parent) {ItemInfo info = getItem(position);if (convertView == null) {convertView = inflater.inflate(R.layout.item_listview, null);}TextView name = (TextView) convertView.findViewById(R.id.item_name);name.setText(info.getName());return convertView;}}
5.MainActivity.java:

package com.demo.downrefresh;import java.util.ArrayList;import java.util.List;import android.R.integer;import android.app.Activity;import android.os.Bundle;import android.os.Handler;import android.support.v4.widget.SwipeRefreshLayout;import android.widget.ListView;/** * 首頁 * @author w.w */public class MainActivity extends Activity implements SwipeRefreshLayout.OnRefreshListener {/** * 給ListView添加下拉重新整理 */private SwipeRefreshLayout swipeLayout;/** * ListView */private ListView listView;/** * ListView適配器 */private ListViewAdapter adapter;private List<ItemInfo> infoList;private int i=0;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);swipeLayout = (SwipeRefreshLayout) this.findViewById(R.id.swipe_refresh);swipeLayout.setOnRefreshListener(this);// 頂部重新整理的樣式swipeLayout.setColorScheme(android.R.color.holo_red_light, android.R.color.holo_green_light,android.R.color.holo_blue_bright, android.R.color.holo_orange_light);infoList = new ArrayList<ItemInfo>();ItemInfo info = new ItemInfo();info.setName("coin");infoList.add(info);listView = (ListView) this.findViewById(R.id.listview);adapter = new ListViewAdapter(this, infoList);listView.setAdapter(adapter);}/** * 下拉重新整理 */public void onRefresh() {new Handler().postDelayed(new Runnable() {public void run() {swipeLayout.setRefreshing(false);ItemInfo info = new ItemInfo();info.setName("coin-refresh"+i);infoList.add(info);i++;adapter.notifyDataSetChanged();//重新整理ListView}}, 500);}}
6.運行執行個體如下:

總結

1.此布局內只能有一個直接子View;

2.setOnRefreshListener(SwipeRefreshLayout.OnRefreshListener listener):設定手勢滑動監聽器;

3.覆寫public void onRefresh()。


喜歡的朋友請關注,謝謝!





 


聯繫我們

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