Android Studio 3.4.1 下拉刷新 SwipRefreshLayout

导入依赖

implementation 'androidx.swiperefreshlayout:swiperefreshlayout:latest.release'

 

主布局文件

SwipRefreshLayout嵌套ListView




    
        
    

ListView显示布局文件 就一个TextView




    


MainActivity

package com.dzqc.card;

import androidx.appcompat.app.AppCompatActivity;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

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

public class MainActivity extends AppCompatActivity {

    private SwipeRefreshLayout swip;
    private ListView lv;
    String[] fruits = {"香蕉", "苹果", "桔子", "西瓜", "橙子", "葡萄"};

    List frutisLists = new ArrayList<>();


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

        swip = findViewById(R.id.swip);

        lv = findViewById(R.id.lv);


        //三种颜色切换
        swip.setColorSchemeResources(R.color.blue, R.color.red, R.color.black);
        //让刷新停止
        swip.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                //获取数据  停止刷新
                refreshFruits();
            }
        });
      /*  //自动刷新
        swip.measure(0,0);
        swip.setRefreshing(true);*/

    }

    private void refreshFruits() {

        new Thread() {
            @Override
            public void run() {

                try {
                    Thread.sleep(2000);

                } catch (Exception e) {
                    e.printStackTrace();
                }

                //更新UI
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        initFrutis();

                        //获取数据
                        lv.setAdapter(new MyAdapter());

                        //刷新消失
                        swip.setRefreshing(false);
                    }
                });

            }
        }.start();
    }

    private void initFrutis() {
        //加载数据
        for (int i = 0; i < fruits.length; i++) {

            Random random = new Random();

            int index = random.nextInt(fruits.length);


            Fruits f = new Fruits();
            f.setName(fruits[index]);

            frutisLists.add(f);
        }

    }

    public class MyAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            return frutisLists.size();
        }

        @Override
        public Object getItem(int i) {
            return null;
        }

        @Override
        public long getItemId(int i) {
            return 0;
        }

        @Override
        public View getView(int i, View view, ViewGroup viewGroup) {

            View v;

            if (view == null) {
                v = View.inflate(getApplicationContext(), R.layout.item, null);
            } else {
                v = view;
            }

            TextView tv_c = v.findViewById(R.id.tv_c);

            Fruits fruits = frutisLists.get(i);

            tv_c.setText(fruits.getName());


            return v;
        }
    }

}

效果图

Android Studio 3.4.1 下拉刷新 SwipRefreshLayout_第1张图片

你可能感兴趣的:(教程,原创,Android)