Android中SwipeRefreshLayout的使用
来源:互联网 发布:中国历史延续 知乎 编辑:程序博客网 时间:2024/04/30 13:33
SwipeRefreshLayout是Android自带的实现下拉刷新的控件。
下面我们重点看一下SwipeRefreshLayout的使用吧。
activity_main.xml文件:
<?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中的具体实现:
package 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); //设置背景颜色 swipeRefreshLayout.setBackgroundColor(Color.YELLOW); //设置初始时的大小 swipeRefreshLayout.setSize(SwipeRefreshLayout.LARGE); //设置监听 swipeRefreshLayout.setOnRefreshListener(this); //设置向下拉多少出现刷新 swipeRefreshLayout.setDistanceToTriggerSync(100); //设置刷新出现的位置 swipeRefreshLayout.setProgressViewEndTarget(false, 200); } @Override public void onRefresh() { //设置每次刷新时需要更新的数据 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() {//在主线程执行 @Override public void run() { //更新数据 adapter.notifyDataSetChanged(); //停止刷新 swipeRefreshLayout.setRefreshing(false); } }); } catch (InterruptedException e) { e.printStackTrace(); } } }).start(); }}运行结果:
0 0
- Android中SwipeRefreshLayout的使用
- Android中SwipeRefreshLayout的使用
- Android中SwipeRefreshLayout的使用
- Android SwipeRefreshLayout的使用
- android--UI--SwipeRefreshLayout的使用
- Android Material的SwipeRefreshLayout使用
- Android中自定义SwipeRefreshLayout的实现
- 解决Android SwipeRefreshLayout & RecyclerView使用的Bug
- Android 下拉刷新控件SwipeRefreshLayout 的使用
- 解决Android SwipeRefreshLayout & RecyclerView使用的Bug
- Android官方刷新组件 SwipeRefreshLayout 的使用
- Android SwipeRefreshLayout 下拉刷新组件的使用
- android学习笔记 SwipeRefreshLayout 的使用
- 【Android - V】之SwipeRefreshLayout的使用
- SwipeRefreshLayout 使用中遇到的问题
- Android SwipeRefreshLayout 使用
- android-swipeRefreshLayout使用详解
- android的SwipeRefreshLayout 扩展
- java怎么实现两个对象内容的交换
- 黑马程序员---IO流(一)
- Swift 入门 基础知识
- spring 读取操作系统上的文件
- iOS开发记录<1>
- Android中SwipeRefreshLayout的使用
- 程明明-关于文献阅读和寻找新的科研题目
- hdu 1269 迷宫城堡
- 临界区,互斥量,信号量,事件的区别
- java基础之对properties配置文件的读写操作
- 黑马程序员——OC-Setter/Getter函数及@proterty和@synthesize
- 黑马程序员---集合框架(一)
- 数据结构例程——图的邻接矩阵存储结构及算法
- Java 8中的增强的Arrays