安卓基礎(十六),安卓基礎

來源:互聯網
上載者:User

安卓基礎(十六),安卓基礎
拉出一個小東西

  • 拉出一個小東西
    • 簡介
    • 本文
    • 擴充閱讀

目標人群:沒有基礎的安卓初學者
知識點:在Android Studio中使用support V4包來實現下拉重新整理的效果
目標:在頁面上實現下拉重新整理功能

簡介
  • support V4包的匯入

  • SwipeRefreshLayout類的簡單使用

本文

1.首先我們需要在build.gradle中添加對support V4包的引用,代碼如下

dependencies {    ...    compile 'com.android.support:support-v4:21.0.3'    ...}
  • 也可以在項目中選擇Open Module Settings-Dependencies-點擊右側加號-Library Dependency-選中appcompat-V4來進行添加

2.建立一個Activity頁面並使其實現android.support.v4.widget.SwipeRefreshLayout.OnRefreshListener介面,代碼如下

import android.app.Activity;import android.os.Bundle;import android.support.v4.widget.SwipeRefreshLayout;public class MainActivity extends Activity implements SwipeRefreshLayout.OnRefreshListener {    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);    }    @Override    public void onRefresh() {        //當處於重新整理狀態時需要執行的載入資料代碼,此處略    }}

3.建立一個包含SwipeRefreshLayout控制項的布局,代碼如下

<android.support.v4.widget.SwipeRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:id="@+id/swipe_refresh_widget"    android:layout_width="match_parent"    android:layout_height="match_parent">    <ListView        android:layout_width="match_parent"        android:layout_height="match_parent"        android:id="@+id/content" /></android.support.v4.widget.SwipeRefreshLayout>
  • SwipeRefreshLayout 通常作為布局中的根控制項存在,但是並非是必須的

  • 為了避免使用過程中出現異常, SwipeRefreshLayout 內部應當有子控制項存在

4.回到Activity中,對SwipeRefreshLayout 進行初始化,並類比下拉重新整理的過程,代碼如下:

    private SwipeRefreshLayout refreshLayout;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        //下拉重新整理控制項        refreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipe_refresh_widget);        //重新整理過程中的顏色展示        refreshLayout.setColorSchemeResources(R.color.red, R.color.blue, R.color.green,                R.color.yellow);        //指定接聽回調介面        refreshLayout.setOnRefreshListener(this);        //文字顯示控制項    }    @Override    public void onRefresh() {        //延遲2000毫秒,類比資料載入        new Handler().postDelayed(new Runnable() {            public void run() {                refreshLayout.setRefreshing(false);                Toast.makeText(MainActivity.this, "載入完成", Toast.LENGTH_LONG).show();            }        }, 2000);    }
  • 想禁用SwipeRefreshLayout的手勢下拉以及動畫效果的話,調用setEnabled(false) 方法即可
擴充閱讀

聯繫我們

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