android自定义控件之ListView上拉加载

自定义控件LoadLayout

import android.content.Context;
import android.graphics.drawable.Animatable;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewConfiguration;
import android.widget.AbsListView;
import android.widget.FrameLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.xh.boke.R;

/**
 * 自定义控件ListView上拉加载
 * Created by Administrator on 2015/10/22 0022.
 */
public class LoadLayout extends FrameLayout implements AbsListView.OnScrollListener {

    private enum Operation {
        START, STOP
    }

    /**
     * 滑动到最下面时的上拉操作
     */

    private int mTouchSlop;
    /**
     * listview实例
     */
    private ListView mListView;

    /**
     * 底部文字
     */
    private TextView mFootText;

    /**
     * 上拉监听器, 到了最底部的上拉加载操作
     */
    private OnLoadListener mOnLoadListener;

    /**
     * ListView的加载中footer
     */
    private View mListViewFooter;

    /**
     * 按下时的y坐标
     */
    private int mYDown;
    /**
     * 抬起时的y坐标, 与mYDown一起用于滑动到底部时判断是上拉还是下拉
     */
    private int mLastY;
    /**
     * 是否在加载中 ( 上拉加载更多 )
     */
    private boolean isLoading = false;

    /**
     * @param context
     */
    public LoadLayout(Context context) {
        this(context, null);
    }

    public LoadLayout(Context context, AttributeSet attrs) {
        super(context, attrs);

        mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();

        mListViewFooter = LayoutInflater.from(context).inflate(R.layout.view_lv_footer, null, false);
        mFootText = (TextView) mListViewFooter.findViewById(R.id.pull_to_refresh_loadmore_text);
    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);

        // 初始化ListView对象
        if (mListView == null) {
            getListView();
        }
    }

    /**
     * 获取ListView对象
     */
    private void getListView() {
        int childs = getChildCount();
        if (childs > 0) {
            for (int i = 0; i < childs; i++) {
                View childView = getChildAt(i);
                if (childView instanceof ListView) {
                    mListView = (ListView) childView;
                    // 设置滚动监听器给ListView, 使得滚动的情况下也可以自动加载
                    mListView.setOnScrollListener(this);
                }
            }
        }
    }

    /*
     * (non-Javadoc)
     * @see android.view.ViewGroup#dispatchTouchEvent(android.view.MotionEvent)
     */
    @Override
    public boolean dispatchTouchEvent(MotionEvent event) {
        final int action = event.getAction();

        switch (action) {
            case MotionEvent.ACTION_DOWN:
                // 按下
                mYDown = (int) event.getRawY();
                break;

            case MotionEvent.ACTION_MOVE:
                // 移动
                break;

            case MotionEvent.ACTION_UP:
                mLastY = (int) event.getRawY();
                // 抬起
                if (canLoad()) {
                    loadData();
                }
                break;
            default:
                break;
        }

        return super.dispatchTouchEvent(event);
    }

    /**
     * 是否可以加载更多, 条件是到了最底部, listview不在加载中, 且为上拉操作.
     *
     * @return
     */
    private boolean canLoad() {
        return isBottom() && !isLoading && isPullUp();
    }

    /**
     * 判断是否到了最底部
     */
    private boolean isBottom() {

        if (mListView != null && mListView.getAdapter() != null) {
            return mListView.getLastVisiblePosition() == (mListView.getAdapter().getCount() - 1);
        }
        return false;
    }

    /**
     * 是否是上拉操作
     *
     * @return
     */
    private boolean isPullUp() {
        return (mYDown - mLastY) >= mTouchSlop;
    }

    /**
     * 如果到了最底部,而且是上拉操作.那么执行onLoad方法
     */
    private void loadData() {
        if (mOnLoadListener != null) {
            // 设置状态
            setLoading(true);
            //
            mOnLoadListener.onLoad();

        }
    }

    /**
     * @param loading
     */
    public void setLoading(boolean loading) {
        isLoading = loading;
        if (isLoading) {
            changeAnimation(Operation.START);
            mListView.addFooterView(mListViewFooter, null, false);
        } else {
            changeAnimation(Operation.STOP);
            mListView.removeFooterView(mListViewFooter);
            mYDown = 0;
            mLastY = 0;
        }
    }

    /**
     * @param loadListener
     */
    public void setOnLoadListener(OnLoadListener loadListener) {
        mOnLoadListener = loadListener;
    }

    @Override
    public void onScrollStateChanged(AbsListView view, int scrollState) {

    }

    @Override
    public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount,
                         int totalItemCount) {
        // 滚动时到了最底部也可以加载更多
        if (canLoad()) {
            loadData();
        }
    }

    /**
     * 加载更多的监听器
     *
     * @author mrsimple
     */
    public static interface OnLoadListener {
        public void onLoad();
    }

    private void changeAnimation(Operation operation) {
        Drawable[] drawables = mFootText.getCompoundDrawables();
        for (Drawable drawable : drawables) {
            if (drawable != null && drawable instanceof Animatable) {
                Animatable animatable = ((Animatable) drawable);
                switch (operation) {
                    case START:
                        animatable.start();
                        break;
                    case STOP:
                        animatable.stop();
                        break;
                }
            }
        }
    }
}

view_lv_footer.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="#ffffff"
    android:gravity="center"
    android:paddingBottom="8dip"
    android:paddingTop="5dip" >

    <TextView
        android:id="@+id/pull_to_refresh_loadmore_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:gravity="center"
        android:paddingTop="5dip"
        android:drawableLeft="@anim/rotating_loading"
        android:drawablePadding="8dp"
        android:text="加载中..."
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textColor="@android:color/black"
        android:textSize="@dimen/activity_horizontal_margin"
        android:textStyle="bold" />

</RelativeLayout>

rotating_loading.xml

<?xml version="1.0" encoding="utf-8"?>
<animated-rotate
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:pivotX="50%"
  android:pivotY="50%"
  android:drawable="@mipmap/ic_ref"
  android:duration="10" />

使用方法(先implements LoadLayout.OnLoadListener,在onLoad()方法中实现加载操作)

setContentView(R.layout.activity_main);
LoadLayout load = (LoadLayout) findViewById(R.id.load);
load.setOnLoadListener(this);

关闭loading

load.setLoading(false);

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<com.xh.boke.component.LoadLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/load"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ListView
        android:id="@+id/lv"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</com.xh.boke.component.LoadLayout>
时间: 2024-08-06 18:25:58

android自定义控件之ListView上拉加载的相关文章

android自定义控件之ListView上拉加载与下拉刷新

自定义控件LoadLayout import android.content.Context; import android.graphics.drawable.Animatable; import android.graphics.drawable.Drawable; import android.util.AttributeSet; import android.view.LayoutInflater; import android.view.MotionEvent; import andr

ListView上拉加载和下拉刷新多种实现方式

ListView上拉加载和下拉刷新多种实现方式 该篇为ListView下拉刷新和上拉加载实现的各种方法大合集.可能在具体的细节逻辑上处理不太到位,但基本上完成逻辑的实现.细节方面,个人可以根据自己的需求进行完善. 该博客将以四种思路来完成下拉刷新和上拉加载 自定义View实现上拉加载和下拉刷新 使用PullToRefresh 实现上拉加载和下拉刷新 使用Ultra-Pull-To-Refresh实现上拉加载和下拉刷新 使用SwipeToRefreshLayout实现上拉加载和下拉刷新 如果你对L

ListView上拉加载下拉刷新

主要用到了这个几个文件,MainActivity是界面的Activity,MyAdapter是ListView的自定义适配,MyListView是自定义带头部LIistView,如果只需要上拉加载就不需要:activity_main.xml是住界面,item.xml是ListView的子布局里面只有一个TextView,listview_footer.xml是listview的加载更多的底部布局,listview_header.xml是listview的头部布局. MainActivity.ja

android ListView上拉加载更多 下拉刷新功能实现(采用pull-to-refresh)

Android实现上拉加载更多功能以及下拉刷新功能, 采用了目前比较火的PullToRefresh,他是目前实现比较好的下拉刷新的类库. 目前他支持的控件有:ListView, ExpandableListView,GridView,WebView等. 下载地址:https://github.com/chrisbanes/Android-PullToRefresh 首先第一步当然是导入libriay到咱们的项目了,具体导入方式,这里不再赘述. 下面是个例子采用的是ListView,当然其余的和这

Android中ListView上拉加载更多及下拉刷新

做几乎每一个Android应用开发,都少不了用到一个控件,那就是ListView,用于加载多条数据,并用一定的样式展示出来.但是为了性能问题(一次性加载太多数据,比如100000条,耗费时间长,消耗资源多等)及用户体验问题(比如用户只想看最新的10条数据,结果一下子把所有的上万条数据都加载了,不方便用户选择)等原因,所以我们要把ListView的数据进行分页加载,常用的就是ListView的上拉加载更多及下拉刷新最新数据. 我们可以自己封装一个带上下拉功能的ListView,通常就是加上头部He

ListView上拉加载,下拉刷新

直接粘贴package com.chen.listTongyong; public interface Pullable { /** * 鍒ゆ柇鏄惁鍙互涓嬫媺锛屽鏋滿?闇?涓嬫媺鍔熻兘鍙互鐩存帴return false * * @return true濡傛灉鍙互涓嬫媺鍚??杩斿洖false */ boolean canPullDown(); /** * 鍒ゆ柇鏄惁鍙互涓婃媺锛屽鏋滿?闇?涓婃媺鍔熻兘鍙互鐩存帴return false * * @return true濡傛灉鍙

robotium listview上拉加载更多/下拉刷新

ListView listview = (ListView) solo.getView("id/list"); int[] location = new int[2]; listview.getLocationOnScreen(location); location[1] = location[1] + listview.getBottom(); Log.i(TAG, "[Location x]: " + Integer.toString(location[0]))

listview上拉加载上一页 下拉加载下一页共通处理

先什么都不说了,上效果图: 第一页默认显示: 上拉加载下一页: 拉至一定高度: 松开 加载中: 下拉加载上一页: 下拉至一定高度: 松开 加载中: 代码已经上传:http://download.csdn.net/detail/zengchao2013/8837971 共通的header和footer可以直接使用. 需要传入的三个参数:是否是第一页,是否是最后一页,当前页数 用于header和footer的UI显示,第一页header隐藏,最后一页footer隐藏.

ListView上拉加载,下拉刷新 PullToRefresh的使用

PullToRefresh是一套实现非常好的下拉刷新库,它支持:ListViewExpandableListViewGridViewWebViewScrollViewHorizontalScrollViewViewPager等多种常用的需要刷新的View类型,而且使用起来也十分方便.(下载地址:https://github.com/chrisbanes/Android-PullToRefresh) 使用PullToRefresh,需要导入第三方library 导入方法可以参考我的另一边文章——<