SwipeRefreshLayout是Android自帶的實(shí)現(xiàn)下拉刷新的控件。
下面我們重點(diǎn)看一下SwipeRefreshLayout的使用吧。
activity_main.xml文件:
[html] view plain copy<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent"> <android.support.v4.widget.SwipeRefreshLayout android:id="@+id/swiPRefresh" android:layout_width="match_parent" android:layout_height="match_parent" android:padding="10dp"> <GridView android:id="@+id/gridView" android:layout_width="match_parent" android:layout_height="match_parent" android:numColumns="3"></GridView> </android.support.v4.widget.SwipeRefreshLayout> </RelativeLayout> 在MainActivity中的具體實(shí)現(xiàn):[java] view plain copypackage com.example.administrator.swiperefreshdemo; import android.graphics.Color; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.support.v4.widget.SwipeRefreshLayout; import android.support.v7.app.AppCompatActivity; import android.widget.ArrayAdapter; import android.widget.GridView; import java.util.ArrayList; import java.util.List; public class MainActivity extends AppCompatActivity implements SwipeRefreshLayout.OnRefreshListener { private SwipeRefreshLayout swipeRefreshLayout; private ArrayAdapter<String> adapter; private int index = 1; private List<String> list = new ArrayList<>(); private GridView gridView; private Handler handler = new Handler() { @Override public void handleMessage(Message msg) { super.handleMessage(msg); } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initView(); } private void initView() { swipeRefreshLayout = (android.support.v4.widget.SwipeRefreshLayout) findViewById(R.id.swipRefresh); gridView = (GridView) findViewById(R.id.gridView); for (int i = 0; i < 20; i++) { list.add("你好"); } adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, list); gridView.setAdapter(adapter); //改變加載顯示的顏色 swipeRefreshLayout.setColorSchemeColors(Color.RED, Color.RED); //設(shè)置背景顏色 swipeRefreshLayout.setBackgroundColor(Color.YELLOW); //設(shè)置初始時(shí)的大小 swipeRefreshLayout.setSize(SwipeRefreshLayout.LARGE); //設(shè)置監(jiān)聽 swipeRefreshLayout.setOnRefreshListener(this); //設(shè)置向下拉多少出現(xiàn)刷新 swipeRefreshLayout.setDistanceToTriggerSync(100); //設(shè)置刷新出現(xiàn)的位置 swipeRefreshLayout.setProgressViewEndTarget(false, 200); } @Override public void onRefresh() { //設(shè)置每次刷新時(shí)需要更新的數(shù)據(jù) list.clear(); index++; for (int i = 0; i < 20; i++) { list.add("我好,哈哈" + index); } new Thread(new Runnable() { @Override public void run() { try { //然刷新控件停留兩秒后消失 Thread.sleep(2000); handler.post(new Runnable() {//在主線程執(zhí)行 @Override public void run() { //更新數(shù)據(jù) adapter.notifyDataSetChanged(); //停止刷新 swipeRefreshLayout.setRefreshing(false); } }); } catch (InterruptedException e) { e.printStackTrace(); } } }).start(); } } 運(yùn)行結(jié)果:

新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注