博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
android ListView上拉加载更多
阅读量:5884 次
发布时间:2019-06-19

本文共 8560 字,大约阅读时间需要 28 分钟。

package com.example.listviewf5;import java.text.SimpleDateFormat;import android.content.Context;import android.util.AttributeSet;import android.view.MotionEvent;import android.view.View;import android.view.animation.RotateAnimation;import android.widget.AbsListView;import android.widget.ImageView;import android.widget.ListView;import android.widget.ProgressBar;import android.widget.TextView;import android.widget.AbsListView.OnScrollListener;public class MyListView extends ListView implements OnScrollListener{    private View v;    private int headHeight;// 头部的高度    private int downY;// 按下时候Y坐标    private final int PULL_REF = 0;// 下拉    private final int REL_REF = 1;// 松开刷新    private final int REFING = 2;// 刷新中    private int currentState = PULL_REF;    private TextView tv;    private TextView tvtiem;    private ImageView img;    private ProgressBar pb;        private int footHeight;    private View foot;        private boolean footstate=false;//当前是否正在处于加载更多    // 旋转动画    private RotateAnimation upAnimation, downAnimation;    public MyListView(Context context, AttributeSet attrs, int defStyle) {        super(context, attrs, defStyle);        // TODO Auto-generated constructor stub        initView(context);    }    public MyListView(Context context, AttributeSet attrs) {        super(context, attrs);        // TODO Auto-generated constructor stub        initView(context);    }    public MyListView(Context context) {        super(context);        // TODO Auto-generated constructor stub        initView(context);    }    /**     * 加载顶部布局文件     *      * @param context     */    private void initView(Context context) {        setOnScrollListener(this);        // LayoutInflater in =LayoutInflater.from(context);        // v = in.inflate(R.layout.head,null);        v = View.inflate(context, R.layout.head, null);        this.addHeaderView(v);        v.measure(0, 0);// 通知系统测量宽高        headHeight = v.getMeasuredHeight();// 得到测量后的高度        v.setPadding(0, -headHeight, 0, 0);// 进行隐藏head,就是把paddingtop设置成负高度        tv = (TextView) v.findViewById(R.id.head_tv);        tvtiem = (TextView) v.findViewById(R.id.head_tvtime);        img = (ImageView) v.findViewById(R.id.head_img);        pb = (ProgressBar) v.findViewById(R.id.pb);        initFoot();        initHeadRotateAnimation();            }    //加载底部布局    private void initFoot() {        foot = View.inflate(getContext(), R.layout.foot, null);        addFooterView(foot);        foot.measure(0, 0);        footHeight = foot.getMeasuredHeight();        foot.setPadding(0, 0, 0,-footHeight);            }    private void initHeadRotateAnimation() {        upAnimation = new RotateAnimation(0, -180,                RotateAnimation.RELATIVE_TO_SELF, 0.5f,                RotateAnimation.RELATIVE_TO_SELF, 0.5f);        upAnimation.setDuration(300);        upAnimation.setFillAfter(true);        downAnimation = new RotateAnimation(-180, -360,                RotateAnimation.RELATIVE_TO_SELF, 0.5f,                RotateAnimation.RELATIVE_TO_SELF, 0.5f);        downAnimation.setDuration(300);        downAnimation.setFillAfter(true);    }    @Override    public boolean onTouchEvent(MotionEvent ev) {        switch (ev.getAction()) {        case MotionEvent.ACTION_DOWN:            downY = (int) ev.getY();            break;        case MotionEvent.ACTION_MOVE:            // 如果是正在刷新状态滑动没有            if (currentState == REFING) {                break;            }            int deltaY = (int) (ev.getY() - downY);// 得到移动的距离            int currentHeight = -headHeight + deltaY;            // 判断当前的距离是不是大于headHeight 并且显示的是第一个位置            if (currentHeight > -headHeight && getFirstVisiblePosition() == 0) {                v.setPadding(0, currentHeight, 0, 0);// 展现头部                if (currentHeight >= 0 && currentState == PULL_REF) {                    currentState = REL_REF;                    refHeadView();                } else if (currentHeight < 0 && currentState == REL_REF) {                    currentState = PULL_REF;                    refHeadView();                }                return true;// 拦截事件不让listview处理            }            break;        case MotionEvent.ACTION_UP:            // 判断当前是不是要刷新状态            if (currentState == PULL_REF) {                v.setPadding(0, -headHeight, 0, 0);            } else if (currentState == REL_REF) {                currentState = REFING;                v.setPadding(0, 0, 0, 0);                refHeadView();                if (listener != null) {                    listener.setPullRfe();                }            }            break;        }        return super.onTouchEvent(ev);    }    private void refHeadView() {        switch (currentState) {        case PULL_REF:            tv.setText("下拉刷新");            img.startAnimation(downAnimation);            break;        case REL_REF:            tv.setText("松开刷新");            img.startAnimation(upAnimation);            break;        case REFING:            tv.setText("正在刷新……");            img.setVisibility(View.GONE);            img.clearAnimation();            pb.setVisibility(View.VISIBLE);            break;        default:            break;        }    }    // 刷新完成要把控件和状态重置    public void completeRef() {        v.setPadding(0, -headHeight, 0, 0);// 展现头部        currentState = PULL_REF;        tv.setText("下拉刷新");        img.setVisibility(View.VISIBLE);        pb.setVisibility(View.GONE);        SimpleDateFormat sDateFormat = new SimpleDateFormat(                "yyyy-MM-dd hh:mm:ss");        String date = sDateFormat.format(new java.util.Date());        tvtiem.setText("最后刷新:" + date);    }    private OnRefLisner listener;    public void setOnRefLisner(OnRefLisner listener) {        this.listener = listener;    }    // 回调接口    public interface OnRefLisner {        void setPullRfe();        void setDownRfe();    }    @Override    public void onScroll(AbsListView arg0, int arg1, int arg2, int arg3) {        // TODO Auto-generated method stub            }    @Override    public void onScrollStateChanged(AbsListView arg0, int state) {        if(state==OnScrollListener.SCROLL_STATE_IDLE                && getLastVisiblePosition()==(getCount()-1)&&footstate==false){            foot.setPadding(0, 0, 0, 0);            setSelection(getCount());            footstate = true;            if(listener!=null){                listener.setDownRfe();            }        }            }    public void completeDown(){        footstate=false;        foot.setPadding(0, 0, 0,-footHeight);    }}
package com.example.listviewf5;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map;import com.example.listviewf5.MyListView.OnRefLisner;import android.os.Bundle;import android.os.Handler;import android.app.Activity;import android.view.Menu;import android.widget.ListView;import android.widget.SimpleAdapter;import android.widget.Toast;public class MainActivity extends Activity {    private MyListView lv;    private List
> list; private SimpleAdapter sa; private Handler handler = new Handler(){ public void handleMessage(android.os.Message msg) { switch (msg.what) { case 0: sa.notifyDataSetChanged(); lv.completeRef(); break; case 1: lv.completeDown(); break; default: break; } }; }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); lv = (MyListView) findViewById(R.id.lv); list = new ArrayList
>(); for (int i = 0; i <20; i++) { Map
map = new HashMap
(); map.put("key1","神马都是浮云"); map.put("key2","heheda"); list.add(map); } sa = new SimpleAdapter(this, list,R.layout.item_lv, new String[]{"key1","key2"},new int[]{R.id.tv,R.id.tv2}); lv.setAdapter(sa); lv.setOnRefLisner(new OnRefLisner() { @Override public void setPullRfe() { Map
map = new HashMap
(); map.put("key1","下拉更新的数据"); map.put("key2","hehedaheheda"); list.add(0, map); handler.sendEmptyMessageDelayed(0,3000); } @Override public void setDownRfe() { for (int i = 0; i < 2; i++) { Map
map = new HashMap
(); map.put("key1","下拉更新的数据"+i); map.put("key2","hehedaheheda"); list.add(list.size(), map); } handler.sendEmptyMessageDelayed(1,3000); } }); }}

 

转载于:https://www.cnblogs.com/84126858jmz/p/5057471.html

你可能感兴趣的文章
Android前后端gson传输数据和listview获取position
查看>>
-webkit-flex 被移除了
查看>>
iOS测试规范
查看>>
Java interview question
查看>>
IntelliJ IDEA 内存优化最佳实践
查看>>
Cloud Insight!StatsD 系监控产品新宠!
查看>>
友盟2015年Q2、Q3季度中国移动互联网趋势报告
查看>>
聊聊MongoDB - MongoDB索引介绍分享
查看>>
设计模式(10)状态模式(讲解+应用)
查看>>
从理论到实践,全方位认识DNS(理论篇)
查看>>
JIRA issue 中的标记语言(Textile)
查看>>
GhostBSD 19.04 发布,注重安全与稳定性的 FreeBSD 发行版
查看>>
开源软件受云服务商影响,共用条款终止开源滥用现象
查看>>
SQL 、 NoSQL 和 NewSQL 的优缺点比较
查看>>
自定义布局实现侧滑菜单1
查看>>
开源SQL-on-Hadoop系统一览
查看>>
【3-2 报名中】Apache RocketMQ 开发者沙龙 成都站
查看>>
Java后端学习路线图,你真的只需要这一张!
查看>>
C++进程间通信的十一种方法
查看>>
通过DataWorks数据集成归档日志服务数据至MaxCompute进行离线分析 ...
查看>>