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);}}
著作權聲明:本文為博主原創文章,未經博主允許不得轉載。