android系統內建下拉重新整理控制項的實現,android控制項

來源:互聯網
上載者:User

android系統內建下拉重新整理控制項的實現,android控制項

android系統內建的下拉重新整理控制項SwipeRefreshLayout位於android.support.v4.widget包下,實現步驟如下:

1.在布局檔案中添加該控制項,該控制項一般作為父控制項,而且只能包含有一個子控制項,並且這個子控制項是能夠滑動的,比如scrollview,listview等

2.實現OnRefreshListener介面,並重寫onRefresh函數


詳細代碼如下:

<android.support.v4.widget.SwipeRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:id="@+id/swipe_refresh"    android:layout_width="match_parent"    android:layout_height="match_parent" >    <ScrollView        android:id="@+id/scrollview"        android:layout_width="match_parent"        android:layout_height="match_parent" >                <LinearLayout             android:layout_width="match_parent"            android:layout_height="wrap_content"            android:orientation="vertical"            >                        <TextView                 android:id="@+id/tip_text"                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:text="@string/pull_fresh"                android:textSize="15sp"                />            <TextView                 android:id="@+id/random_text"                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:textColor="#FF000000"                android:textSize="15sp"                />                    </LinearLayout>            </ScrollView></android.support.v4.widget.SwipeRefreshLayout>

import android.app.Activity;import android.os.Bundle;import android.os.Handler;import android.support.v4.widget.SwipeRefreshLayout;import android.support.v4.widget.SwipeRefreshLayout.OnRefreshListener;import android.widget.TextView;/** * MainActivity---系統下拉重新整理控制項的實現 * @author seabear * */public class MainActivity extends Activity implements OnRefreshListener{private SwipeRefreshLayout mSwipeRefreshLayout;private TextView mRandomText;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);mSwipeRefreshLayout = (SwipeRefreshLayout)this.findViewById(R.id.swipe_refresh);mSwipeRefreshLayout.setOnRefreshListener(this);mRandomText = (TextView)this.findViewById(R.id.random_text);}@Overridepublic void onRefresh() {mSwipeRefreshLayout.setRefreshing(true);(new Handler()).postDelayed(new Runnable() {@Overridepublic void run() {//3秒後停止重新整理mSwipeRefreshLayout.setRefreshing(false);int num = (int)(Math.random() * 100 + 1);String s = mRandomText.getText().toString();s = s + " " + num;mRandomText.setText(s);}}, 3000);}}


著作權聲明:本文為博主原創文章,未經博主允許不得轉載。

聯繫我們

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