PullToRefresh(下拉刷新,上拉加載)

image.png

image.png

image.png

image.png

image.png

image.png

image.png

首先:一,
我們需要這個(gè)文件夾


image.png

二: 導(dǎo)入依賴庫


image.png

三:添加依賴

image.png

然后編寫項(xiàng)目
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.pulltorefresh.MainActivity">
<com.handmark.pulltorefresh.library.PullToRefreshListView
xmlns:ptr="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
ptr:ptrMode="both"
ptr:ptrDrawable="@drawable/ic_launcher"
ptr:ptrHeaderBackground="@color/colorAccent"
ptr:ptrHeaderTextColor="@color/colorPrimary"
ptr:ptrHeaderSubTextColor="@color/mycolor"
ptr:ptrShowIndicator="true"
ptr:ptrAnimationStyle="flip"
android:background="@color/mycolor"
ptr:ptrRefreshableViewBackground="@color/colorPrimary"
ptr:ptrScrollingWhileRefreshingEnabled="true"
android:id="@+id/pull"
>

</com.handmark.pulltorefresh.library.PullToRefreshListView>

</LinearLayout>

···


image.png

MainActivity
···
package com.example.pulltorefresh;

import android.os.Handler;
import android.os.Message;
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 android.widget.Toast;

import com.handmark.pulltorefresh.library.ILoadingLayout;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

public class MainActivity extends AppCompatActivity {
ArrayList<String > arrayList=new ArrayList<>();
PullToRefreshListView pull;
Handler hand=new Handler(){
@Override
public void handleMessage(Message msg) {
if (msg.what==1){
pull.onRefreshComplete();
}
}
};

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

private void init() {
    pull= (PullToRefreshListView) findViewById(R.id.pull);
    for (int i = 0; i <40 ; i++) {
         arrayList.add("測(cè)試: *"+i);

    }
    pull.setAdapter(new MyAdapter());
    pull.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
        @Override
        public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
            Toast.makeText(MainActivity.this, "下拉刷新", Toast.LENGTH_SHORT).show();
            hand.sendEmptyMessageDelayed(1,3000);
        }

        @Override
        public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
            Toast.makeText(MainActivity.this, "上滑刷新", Toast.LENGTH_SHORT).show();
            hand.sendEmptyMessageDelayed(1,3000);
        }
    });
    long time = System.currentTimeMillis();
    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy:MM:dd hh:mm:ss");
    Date date = new Date();
    simpleDateFormat.format(date);

    ILoadingLayout down = pull.getLoadingLayoutProxy(true,false);//設(shè)置上拉文字
    down.setPullLabel("下拉刷新11...");// 剛下拉時(shí),顯示的提示
    down.setRefreshingLabel("正在載入11...");// 刷新時(shí)
    down.setReleaseLabel("放開刷新11...");// 下來達(dá)到一定距離時(shí),顯示的提示
    down.setLastUpdatedLabel("上次刷新時(shí)間:"+time);
       //上拉
    ILoadingLayout up = pull.getLoadingLayoutProxy(false, true);
    up.setPullLabel("上拉刷新22...");// 剛下拉時(shí),顯示的提示
    up.setRefreshingLabel("正在載入22...");// 刷新時(shí)
    up.setReleaseLabel("放開刷新22...");// 下來達(dá)到一定距離時(shí),顯示的提示
    up.setLastUpdatedLabel("上次刷新時(shí)間:"+time);

}
class  MyAdapter extends BaseAdapter{


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

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

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

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        TextView tx = new TextView(MainActivity.this);
        tx.setText(arrayList.get(i));
        tx.setTextSize(22);
        tx.setTextColor(getResources().getColor(android.R.color.holo_green_dark));
        return tx;
    }
}

}

···
效果圖:


image.png

image.png

詳情:
https://blog.csdn.net/coder80/article/details/50998469

https://blog.csdn.net/dss886/article/details/10288925

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)容

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