博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Android的ListView分页功能
阅读量:5098 次
发布时间:2019-06-13

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

一、功能分析----ListView“加载更多”,功能如下图所示:

这个效果是当你上拉拖动页面时(注意有区别于下拉刷新),页面提示正在加载,2秒后显示留言更多内容;具体功能知道那我就来讲解下如何实现这个功能。

二、创建自定义ListView及其底部布局footer

首先做一个加载提示布局,布局footer_layout如下图

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="
    android:id="@+id/load_layout"
    android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_gravity="center" >
        <ProgressBar
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           style="?android:attr/progressBarStyleSmall"/>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="正在加载..." />
</LinearLayout>

将布局添加至ListView,自定义一个ListView

package com.mg.message.view;

import android.content.Context;

import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.ListView;
import com.mg.message.R;

public class LX_LoadListView extends ListView implements OnScrollListener{

    View footer;//底部布局
    int totalItemCount;//总数量
    int lastVisibleItem;//最后一个可见的Item
    boolean isLoading;//正在加载
    ILoadListener iLoadListener;
    public LX_LoadListView(Context context) {
        super(context);
        initView(context);
    }
   
    public LX_LoadListView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView(context);
    }
   
    public LX_LoadListView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        initView(context);
    }
/**
* 添加底部加载提示布局到listview
* @param context
*/
    private void initView(Context context) {
        LayoutInflater layoutInflater=LayoutInflater.from(context);
        footer=layoutInflater.inflate(R.layout.lx_footer_layout, null);
        footer.findViewById(R.id.load_layout).setVisibility(View.GONE);
        this.addFooterView(footer);
        this.setOnScrollListener(this);
    }

    @Override

    public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount)
    {
        this.lastVisibleItem=firstVisibleItem+visibleItemCount;
        this.totalItemCount=totalItemCount;
    }

    @Override

    public void onScrollStateChanged(AbsListView view, int scrollState) {
        if (totalItemCount==lastVisibleItem&&scrollState==SCROLL_STATE_IDLE) {
            if (!isLoading) {
                isLoading=true;
                footer.findViewById(R.id.load_layout).setVisibility(View.VISIBLE);
                iLoadListener.onLoad();//加载更多数据
            }
        }
    }
    //加载完毕
    public void loadComplete() {
        isLoading=false;
        footer.findViewById(R.id.load_layout).setVisibility(View.GONE);
    }
    public void setInterface(ILoadListener iLoadListener) {
        this.iLoadListener=iLoadListener;
    }
    //加载更多数据的回调接口
    public interface ILoadListener
    {
        public void onLoad();
    }
}

三、给ListView设置Adapter并实现接口中的Load方法

①自定义ListView布局,lx_get_content_activity

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:layout_marginTop="2dp"
    android:layout_marginBottom="2dp"
    android:layout_marginLeft="2dp"
    android:layout_marginRight="2dp"
    android:background="@drawable/edit_normal">
    <com.mg.message.view.LX_LoadListView
        android:id="@+id/list_content"
        android:layout_width="match_parent"
        android:background="@drawable/edit_normal"
        android:layout_height="0dip"
        android:focusable="true"
        android:layout_weight="1.20" >
    </com.mg.message.view.LX_LoadListView>
</LinearLayout>

②给ListView设置Adapter,并实现接口的Load方法

package com.mg.message.ui;

import java.util.ArrayList;

import java.util.List;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.AdapterView.OnItemClickListener;
import com.mg.core.base.BaseActivity;
import com.mg.core.net.OperateCode;
import com.mg.core.net.ThreadMessage;
import com.mg.message.R;
import com.mg.message.data.GetMessageData;
import com.mg.message.domain.Message;
import com.mg.message.ui.adapter.ContentAdapter;
import com.mg.message.utils.ViewUtil;
import com.mg.message.view.LX_LoadListView;
import com.mg.message.view.LX_LoadListView.ILoadListener;
/* * 获得留言*/
public class LX_GetContentActivity extends BaseActivity implements ILoadListener
{
    private LX_LoadListView listView;
    private List<Message> totalMessages=new ArrayList<Message>();
    private static ContentAdapter myAdapter;
    private int totalSize;
    List<Message> messages=new ArrayList<Message>();
    @Override
    protected void initEvents()
    {
        listView.setOnItemClickListener(new OnItemClickListener() //进入删除页
        {
            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int position,
                    long arg3)
            {
                Bundle bundle = new Bundle(); //传递多参数时使用Bundle
                bundle.putString("ID", messages.get(position).getID());
                bundle.putString("UContent", messages.get(position).getContent().toString());
                bundle.putString("UTime", messages.get(position).getCreateTime());
                   startCOActivity(LX_DeleteActivity.class,bundle);
                   finish();
            }
        });
        //修改留言
        listView.setOnItemLongClickListener(new OnItemLongClickListener()
        {
            @Override
            public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
                    int position, long arg3) {
                Bundle bundle = new Bundle(); //传递多参数时使用Bundle
                bundle.putString("ID", messages.get(position).getID());
                   startCOActivity(LX_AddMessageActivity.class,bundle);
                   finish();
                return false;
            }
        });
    }

    @Override

    protected void initViews()
    {
        setContentView(R.layout.lx_get_content_activity);
        mTopBar.setVisibility(View.VISIBLE);
        mTopBar.SetTvName("查看留言");
        listView=(LX_LoadListView)findViewById(R.id.list_content);
        listView.setInterface(this);
          myAdapter=new ContentAdapter(messages);
          listView.setAdapter(myAdapter);
    }
    @Override
    protected void onResume() {
        super.onResume();
        showToast("onResume", false);
        sendToBackgroud(OperateCode.i_getMessageInfo);
    }
    @Override
    protected void receiveRequest(ThreadMessage threadMessage)
    {
        hideProgressDialog();
        OperateCode operateCode = threadMessage.getOperateCode();
        List<Message> messageList;
        switch (operateCode)
        {  //查看留言
            case i_getMessageInfo:
                messageList=GetMessageData.getMessagesList();
                if(ViewUtil.isNotEmpty(messageList))
                {
                    totalMessages.clear();
                    messages.clear();
                    totalMessages.addAll(messageList);
                    totalSize=totalMessages.size();
                    getData();
                    myAdapter.notifyDataSetChanged();
                }
                break;
            default:
                break;
        }
    }
    //listview初始化条数
    private void  getData()
    {
        try {
            if (totalSize<9) {
                messages.addAll(GetMessageData.getMessagesList());
                showToast("留言小于9加载完毕", false);
            }
            else {
                messages.addAll(GetMessageData.getMessagesList().subList(0, 8));
            }
        }
        catch (Exception e) {
        }
    }
    //加载数据
    private void getLoadData()
    {
        int lastsize=totalSize-messages.size();
        if(messages.size()<totalSize)
        {
            if (lastsize<5) {
                messages.clear();
                messages.addAll(GetMessageData.getMessagesList());
                showToast("加载项<5加载完毕", false);
            }
            else {
                messages.addAll(totalMessages.subList(messages.size(), messages.size()+5));
            }
        }       
        else
        {
            messages.clear();
            messages.addAll(GetMessageData.getMessagesList());
            showToast("全部加载完毕", false);
        }
        myAdapter.notifyDataSetChanged();
    }
    @Override
    public void onLoad()
    {
        android.os.Handler handler=new android.os.Handler();
        handler.postDelayed(new Runnable()
        {
            @Override
            public void run() {
                getLoadData();//获取更多数据
                listView.loadComplete();//通知listview加载完毕
            }
        }, 2000);
    }
}

转载于:https://www.cnblogs.com/wucaiyun1/p/4733998.html

你可能感兴趣的文章
EF6学习笔记十九:不一样的复杂类型
查看>>
UITableView 的用法
查看>>
http://jingyan.baidu.com/article/dca1fa6fa07000f1a44052f6.html
查看>>
第三方支付架构设计之—帐户体系
查看>>
诸城项目-开发日志
查看>>
fdisk (二) 详解(转)
查看>>
hdu 2768 Cat vs. Dog 最大独立集 巧妙的建图
查看>>
简单将集合的内容转为字符串
查看>>
Python pandas 0.19.1 Intro to Data Structures 数据结构介绍 文档翻译
查看>>
《寿康宝鉴》
查看>>
CentOS7下安装jdk8环境
查看>>
Mongodb
查看>>
struts2 DMI
查看>>
管道和I/O重定向
查看>>
django重点url,视图函数,模板语言
查看>>
Base64编码与图片互转
查看>>
bzoj 3997 Dilworth定理
查看>>
web在线页面编辑实现-abtest可视化实验
查看>>
iOS直播技术学习笔记 iOS中实现推流(八)
查看>>
搞懂JavaScript引擎运行原理
查看>>