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

依赖网咯

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

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

先依赖选本地依赖小项目

  compile project(':PullToRefresh')

xml布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.wangye.androidxmlc_class9.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:ptrDrawable="@drawable/ic_launcher"
    ptr:ptrMode="both"
    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>

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<String> 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<ListView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> 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<ListView> 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;
      }
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

友情链接更多精彩内容