国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統(tǒng) > Android > 正文

Android中SwipeRefreshLayout的使用

2019-11-09 17:49:15
字體:
供稿:網(wǎng)友

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é)果:


發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 灌阳县| 湘乡市| 孙吴县| 鄂伦春自治旗| 乐昌市| 温宿县| 义马市| 建昌县| 张掖市| 肃北| 绥宁县| 石泉县| 昭通市| 谷城县| 洪湖市| 茂名市| 扶绥县| 桂林市| 定襄县| 庆阳市| 包头市| 荔波县| 青阳县| 河间市| 繁昌县| 札达县| 枞阳县| 镇安县| 拉萨市| 呼图壁县| 通河县| 芦溪县| 许昌市| 上蔡县| 社旗县| 闸北区| 二手房| 万年县| 崇信县| 抚远县| 瓦房店市|