其他分享
首页 > 其他分享> > SwipeRefreshLayout

SwipeRefreshLayout

作者:互联网

下拉刷新控件SwipeRefreshLayout,通过下拉刷新实现列表的刷新。
activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    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"
    tools:context="com.fitsoft.MainActivity">

    <android.support.v4.widget.SwipeRefreshLayout
        android:id="@+id/swipe_refresh_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <ListView
            android:id="@+id/my_list_view"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

        </ListView>
    </android.support.v4.widget.SwipeRefreshLayout>
</android.support.constraint.ConstraintLayout>

在下拉刷新控件中添加了一个ListView列表控件。
MainActivity:

package com.fitsoft;

import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AppCompatActivity;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    SwipeRefreshLayout swipeRefreshLayout;
    ListView listView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        swipeRefreshLayout = findViewById(R.id.swipe_refresh_layout);
        listView = findViewById(R.id.my_list_view);

        final List<String> list = new ArrayList<>();
        for(int i=0; i<30; i++){
            list.add(i+"");
        }

        final ArrayAdapter<String> adapter = new ArrayAdapter<>(this, 
                android.R.layout.simple_list_item_1,
                android.R.id.text1, list);

        listView.setAdapter(adapter);
        swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        swipeRefreshLayout.setRefreshing(false);
                        Toast.makeText(MainActivity.this, "加载完成!", Toast.LENGTH_SHORT).show();
                        for(int i=0; i<20; i++){
                            list.add("新加的数据:"+i);
                        }
                        adapter.notifyDataSetChanged();
                    }
                }, 2000);
            }
        });
        //循环一圈颜色
        swipeRefreshLayout.setColorSchemeColors(Color.RED, Color.GREEN, Color.BLUE, Color.CYAN);
        //下拉高度
        swipeRefreshLayout.setProgressViewOffset(false, 200, 500);
    }
}

这里首先为ListView绑定适配器,并且设置数据源,数据源就是0-29的字符串。
这里将变量定义为final,为什么呢?
https://blog.csdn.net/tianjindong0804/article/details/81710268
这个博主解释的很好,我很赞同(小声BB),然后在swipeRefreshLayout的下拉刷新方法中开启一个2秒的定时器,2秒后:

swipeRefreshLayout.setRefreshing(false);

将刷新的图标关闭,并且在原来的数据源的基础上新加了20条数据。
这里高度设置有点大,有点丑,凑合看吧(不然你也没招对不对=_=)
效果图:


标签:swipeRefreshLayout,SwipeRefreshLayout,new,刷新,import,android
来源: https://www.cnblogs.com/zqm-sau/p/11564992.html