下拉刷新SwipeRefreshLayout的使用

image.png

image.png

image.png

用法:
main.xml
···

xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.lenovo.test_03.MainActivity">
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/swipe"
>
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/listView"
>

···
--------------MainActivity ------------------
···
package com.example.lenovo.test_03;

import android.os.Build;
import android.os.Handler;
import android.os.Message;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AppCompatActivity;
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;

public class MainActivity extends AppCompatActivity {
ListView listView;
SwipeRefreshLayout swipe;
MyAdapter adapter;
ArrayListarray=new ArrayList<>();
ArrayList datalist=new ArrayList<>();
int index=20; //最开始加载的数据
Handler hand=new Handler(){
@Override
public void handleMessage(Message msg) {
if (msg.what==1){
adapter.notifyDataSetChanged();
swipe.setRefreshing(false);//刷新完毕 ,图标消失
}
}
};

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

private void init() {
    for (int i = 0; i <100 ; i++) {
        array.add("我是数据:"+i);

    }
    for (int i = 0; i < 20; i++) {
        datalist.add(array.get(i));
    }
    swipe = (SwipeRefreshLayout) findViewById(R.id.swipe);
    swipe.setSize(SwipeRefreshLayout.DEFAULT);//设置加载默认图标
   swipe.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
       @Override
       public void onRefresh() {
           int indexMax = index + 20;
           for (int i = index; i < indexMax; i++) {
               datalist.add(array.get(i));
           }
           index = indexMax;
           hand.sendEmptyMessageDelayed(1,3000);
       }
   });
    if (Build.VERSION.SDK_INT>=23){
        swipe.setProgressBackgroundColorSchemeColor(
                getResources().getColor(android.R.color.holo_orange_light,getTheme()));
        swipe.setColorSchemeColors(//刷新控件动画中的颜色
                getResources().getColor(android.R.color.holo_blue_dark,getTheme()),
                getResources().getColor(android.R.color.holo_red_dark,getTheme()),
                getResources().getColor(android.R.color.holo_green_dark,getTheme())
        );
    }
    listView= (ListView) findViewById(R.id.listView);
    adapter=new MyAdapter();
    listView.setAdapter(adapter);
}
class  MyAdapter extends BaseAdapter{

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

    @Override
    public Object getItem(int i) {
        return datalist.get(i);
    }

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

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        TextView textView = new TextView(MainActivity.this);
        textView.setTextSize(22);
        //判断兼容版本是否是6.0以上
        if (Build.VERSION.SDK_INT>=23){
            textView.setTextColor(getResources().getColor(android.R.color.holo_green_dark,
                    //getTheme()获取当前Activity主题
                    getTheme()));
        }else {

// textView.setTextColor(getColor(android.R.color.holo_green_dark));

        }
        textView.setText(datalist.get(i));
        return textView;
    }
}

}
···
效果图:


image.png

你可能感兴趣的:(下拉刷新SwipeRefreshLayout的使用)