下拉刷新SwipeRefreshLayout的使用

image.png

image.png

image.png

用法:
main.xml
···
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
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.support.v4.widget.SwipeRefreshLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/swipe"
>
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/listView"
>
</ListView>

</android.support.v4.widget.SwipeRefreshLayout>

</LinearLayout>

···
--------------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;
ArrayList<String>array=new ArrayList<>();
ArrayList<String> datalist=new ArrayList<>();
int index=20; //最開始加載的數(shù)據(jù)
Handler hand=new Handler(){
@Override
public void handleMessage(Message msg) {
if (msg.what==1){
adapter.notifyDataSetChanged();
swipe.setRefreshing(false);//刷新完畢 ,圖標(biāo)消失
}
}
};

@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("我是數(shù)據(jù):"+i);

    }
    for (int i = 0; i < 20; i++) {
        datalist.add(array.get(i));
    }
    swipe = (SwipeRefreshLayout) findViewById(R.id.swipe);
    swipe.setSize(SwipeRefreshLayout.DEFAULT);//設(shè)置加載默認(rèn)圖標(biāo)
   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(//刷新控件動(dòng)畫中的顏色
                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()獲取當(dāng)前Activity主題
                    getTheme()));
        }else {

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

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

}
···
效果圖:


image.png
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Android 自定義View的各種姿勢(shì)1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 178,781評(píng)論 25 709
  • activity_main============================================...
    穎殤_b9fc閱讀 902評(píng)論 0 0
  • afinalAfinal是一個(gè)android的ioc,orm框架 https://github.com/yangf...
    wgl0419閱讀 6,568評(píng)論 1 9
  • 今天算是把公司的事物處理妥當(dāng)了,也松了口氣,回到住所就放慢了所有的節(jié)奏,晚餐也是慢條斯理做,慢條斯理的吃,...
    南山默少閱讀 133評(píng)論 0 0
  • 今天說是不開心的一天也可以,今天二寶發(fā)燒了,一天都沒怎么吃東西。不過今天走親戚去了,二寶在那玩的也挺開心的。下午早...
    靠你右肩閱讀 272評(píng)論 0 0

友情鏈接更多精彩內(nèi)容