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

2018-08-07  本文已影响0人  Summer_27d1
image.png
image.png
image.png
image.png
image.png
image.png
image.png

首先:一,
我们需要这个文件夹


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.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("测试: *"+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);//设置上拉文字
    down.setPullLabel("下拉刷新11...");// 刚下拉时,显示的提示
    down.setRefreshingLabel("正在载入11...");// 刷新时
    down.setReleaseLabel("放开刷新11...");// 下来达到一定距离时,显示的提示
    down.setLastUpdatedLabel("上次刷新时间:"+time);
       //上拉
    ILoadingLayout up = pull.getLoadingLayoutProxy(false, true);
    up.setPullLabel("上拉刷新22...");// 刚下拉时,显示的提示
    up.setRefreshingLabel("正在载入22...");// 刷新时
    up.setReleaseLabel("放开刷新22...");// 下来达到一定距离时,显示的提示
    up.setLastUpdatedLabel("上次刷新时间:"+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
上一篇 下一篇

猜你喜欢

热点阅读