PullToRefreshListView下拉刷新上拉加载(需要本地依赖)

依赖网咯

compile 'com.android.support:support-v4:23.3.0' 

compile 'com.github.userswlwork:pull-to-refresh:1.0.0'

先依赖选本地依赖小项目

  compile project(':PullToRefresh')

xml布局









mainactivity

package com.example.wangye.androidxmlc_class9;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
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 arrayList = new ArrayList<>();
    PullToRefreshListView pull;
    MyAdapter adapter;
    Handler hand = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            if(msg.what == 1){
               pull.onRefreshComplete();
                adapter.notifyDataSetChanged();
            }
        }
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        init();
    }
    public void init(){
        pull = (PullToRefreshListView) findViewById(R.id.pull);
        for (int i = 0; i < 40; i++) {
            arrayList.add("测试。。。");
        }
        adapter = new MyAdapter();
        pull.setAdapter(adapter);
        int i=1;
        pull.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase refreshView) {
                Toast.makeText(MainActivity.this, "下拉刷新", Toast.LENGTH_SHORT).show();
                hand.sendEmptyMessageDelayed(1,3000);
                for (int i = 0; i < 40; i++) {
                    arrayList.add(0,"测试1111"+i++);
                }
            }
            @Override
            public void onPullUpToRefresh(PullToRefreshBase refreshView) {
                Toast.makeText(MainActivity.this, "上滑刷新", Toast.LENGTH_SHORT).show();
                hand.sendEmptyMessageDelayed(1,3000);
                for (int i = 0; i < 40; i++) {
                    arrayList.add("测试2222"+i++);
                }
            }
        });
        long time = System.currentTimeMillis();
        SimpleDateFormat df = new SimpleDateFormat("yyyy/MM/dd hh:mm:ss");
        Date date = new Date();
        String time1 = df.format(date);
        ILoadingLayout startLabels = pull.getLoadingLayoutProxy(true, false);
        startLabels.setPullLabel("下拉刷新11...");// 刚下拉时,显示的提示
        startLabels.setRefreshingLabel("正在载入11...");// 刷新时
        startLabels.setReleaseLabel("放开刷新11...");// 下来达到一定距离时,显示的提示
        startLabels.setLastUpdatedLabel("上次刷新时间:"+time1);

        ILoadingLayout endLabels = pull.getLoadingLayoutProxy(false, true);
        endLabels.setPullLabel("上拉刷新22...");// 刚下拉时,显示的提示
        endLabels.setRefreshingLabel("正在载入22...");// 刷新时
        endLabels.setReleaseLabel("放开刷新22...");// 下来达到一定距离时,显示的提示

        endLabels.setLastUpdatedLabel("上次刷新时间:"+time1);
        Log.d("time",time1);

    }

    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

在使用的时候用到FragmentViewPager的时候可能会爆
java.lang.IllegalArgumentException: Wrong state class, expecting View State but received class cn.etouch.ecalendar.waterfallview.StaggeredGridView$GridListSavedState instead. This usually happens when two views of different type have the same id in the same hierarchy. This view's id is id/refresh_gridView. Make sure other views do not use the same id.
详细访问
https://blog.csdn.net/yilip/article/details/44345655
将重写的方法修改为

    @Override
    protected void onRestoreInstanceState(Parcelable state) {
        try {
            super.onRestoreInstanceState(state);
        }catch (Exception e) {}
        state=null;
      }

你可能感兴趣的:(PullToRefreshListView下拉刷新上拉加载(需要本地依赖))