码迷,mamicode.com
首页 > 移动开发 > 详细

Android的ListView分页功能

时间:2015-08-16 13:43:36      阅读:170      评论:0      收藏:0      [点我收藏+]

标签:

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

技术分享 技术分享

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

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

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

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/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="http://schemas.android.com/apk/res/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);
    }
}

Android的ListView分页功能

标签:

原文地址:http://www.cnblogs.com/wucaiyun1/p/4733998.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!