Android system comes with the implementation of the drop-down refresh control, android Control
The drop-down refresh control SwipeRefreshLayout provided by android is located in the android. support. v4.widget package. The implementation steps are as follows:
1. Add the control to the layout file. This control is generally used as the parent control and can only contain one child control, which can be slide, such as scrollview and listview.
2. Implement the OnRefreshListener interface and rewrite the onRefresh function.
The Code is as follows:
<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 --- implement the system pull-down refresh control * @ author seabear **/public class MainActivity extends Activity implements OnRefreshListener {private SwipeRefreshLayout listener; 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 () {// stop refreshing mSwipeRefreshLayout after 3 seconds. setRefreshing (false); int num = (int) (Math. random () * 100 + 1); String s = mRandomText. getText (). toString (); s = s + "" + num; mRandomText. setText (s) ;}}, 3000 );}}
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.