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

Android_ListView上拉加载更多(ListView分页功能)

时间:2016-05-12 19:39:03      阅读:397      评论:0      收藏:0      [点我收藏+]

标签:

先上效果图

技术分享

加载完数据

技术分享

首先定义一个底部正在加载的布局footer_layout.xml

<?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">

    <LinearLayout
        android:id="@+id/load_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:orientation="horizontal"
        android:paddingBottom="10dip"
        android:paddingTop="10dip" >

        <ProgressBar
            style="?android:attr/progressBarStyleSmall"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="16sp"
            android:textColor="@color/red"
            android:textStyle="bold"
            android:text="正在加载..." />
    </LinearLayout>
</LinearLayout>

自定义ListView---------LoadListView.java

package com.zwb.listviewloaddata.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AbsListView;
import android.widget.ListView;

import com.zwb.listviewloaddata.R;

/**
 * 类描述:自定义的ListView
 * 作者:zwb
 * 时间:16-5-9 11:21
 * 邮箱:@163.com
 */
public class LoadListView extends ListView implements AbsListView.OnScrollListener {

    View footer;// 底部布局;
    int totalItemCount;// 总数量;
    int lastVisibleItem;// 最后一个可见的item;
    boolean isLoading;// 正在加载;
    ILoadListener iLoadListener;
    public LoadListView(Context context) {
        super(context);
        initView(context);
    }

    public LoadListView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView(context);
    }

    public LoadListView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        initView(context);
    }

    /**
     * 添加底部加载提示布局到listview
     *
     * @param context
     */
    private void initView(Context context) {
        LayoutInflater inflater = LayoutInflater.from(context);
        footer = inflater.inflate(R.layout.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) {
        // TODO Auto-generated method stub
        this.lastVisibleItem = firstVisibleItem + visibleItemCount;
        this.totalItemCount = totalItemCount;
    }

    @Override
    public void onScrollStateChanged(AbsListView view, int scrollState) {
        // TODO Auto-generated method stub
        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

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:background="@color/white"
    android:layout_height="match_parent"
    tools:context="com.zwb.listviewloaddata.MainActivity">

    <com.zwb.listviewloaddata.view.LoadListView
        android:id="@+id/listView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/white"
        android:cacheColorHint="#00000000"
        android:dividerHeight="5dip"
        />
</RelativeLayout>

ListView的item资源文件:

item_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginLeft="5dp"
    android:layout_marginRight="5dp"
    android:layout_centerInParent="true"
    android:background="@color/white"
    card_view:cardCornerRadius="8dp">

    <LinearLayout
        android:gravity="center_vertical"
        android:orientation="horizontal"
        android:background="@color/white"
        android:layout_width="match_parent"
        android:layout_height="60dp">

        <ImageView
            android:id="@+id/item_img"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:background="@mipmap/ic_launcher"
            android:layout_marginLeft="10dp"/>

        <LinearLayout

            android:layout_marginLeft="10dp"
            android:orientation="vertical"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">
            <TextView
                android:id="@+id/item_name"
                android:text="我是标题"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />
            <TextView
                android:id="@+id/item_msg"
                android:text="今天天气还可以啊"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />

        </LinearLayout>

    </LinearLayout>
</android.support.v7.widget.CardView>

 接下来就是在Activity中初始化数据和对ListView设置数据

MainActivity.java

package com.zwb.listviewloaddata;

import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ListView;

import com.zwb.listviewloaddata.adapter.ItemAdapter;
import com.zwb.listviewloaddata.entity.ItemEntity;
import com.zwb.listviewloaddata.view.LoadListView;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity implements LoadListView.ILoadListener{

    private ArrayList<ItemEntity> list = new ArrayList<>();
    private LoadListView listView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        //初始化数据的方法
        getData();
        showListView(list);
    }

    private ItemAdapter adapter;

    private void showListView(ArrayList<ItemEntity> list) {
        if (adapter == null){
            listView = ((LoadListView) findViewById(R.id.listView));
            listView.setInterface(this);
            adapter = new ItemAdapter(getApplicationContext() ,list);
            listView.setAdapter(adapter);
        }else {
            adapter.onDateChange(list);
        }
    }


    private void getMoreData() {
        for (int i = 0; i < 3; i++) {
            ItemEntity entity = new ItemEntity();
            entity.setName("新的测试标题");
            entity.setMsg("我是一条新的测试的内容啊~");
            list.add(entity);
        }
    }


    private void getData() {
        for (int i = 0; i < 10; i++) {
            ItemEntity entity = new ItemEntity();
            entity.setName("测试标题");
            entity.setMsg("我是一条测试的内容啊~");
            list.add(entity);
        }
    }


    @Override
    public void onLoad() {
        Handler handler = new Handler();
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {

                //加载数据
                getMoreData();

                //更新ListView的显示
                showListView(list);

                //ListView加载完毕
                listView.loadComplete();

            }
        } , 2000);
    }

}

ListView的Adapter

ItemAdapter.java

package com.zwb.listviewloaddata.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.zwb.listviewloaddata.R;
import com.zwb.listviewloaddata.entity.ItemEntity;

import java.util.ArrayList;
import java.util.List;

/**
 * 类描述:ListView的Adapter
 * 作者:zwb
 * 时间:16-5-9 11:06
 * 邮箱:@163.com
 */
public class ItemAdapter extends BaseAdapter{

    private Context context;
    private ArrayList<ItemEntity> list;

    public ItemAdapter(Context context, ArrayList<ItemEntity> list) {
        this.context = context;
        this.list = list;
    }
    public void onDateChange(ArrayList<ItemEntity> list) {
        this.list = list;
        this.notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        ItemEntity entity = list.get(position);
        ViewHolder holder;
        if (convertView == null){
            holder = new ViewHolder();
            convertView = LayoutInflater.from(context).inflate(R.layout.item_layout , parent ,false);
            holder.name = ((TextView) convertView.findViewById(R.id.item_name));
            holder.msg = ((TextView) convertView.findViewById(R.id.item_msg));
            convertView.setTag(holder);
        }else {
            holder = ((ViewHolder) convertView.getTag());
        }
        holder.name.setText(entity.getName());
        holder.msg.setText(entity.getMsg());

        return convertView;
    }


    class ViewHolder{
        TextView name;
        TextView msg;
    }
}

ok大功告成。

下载链接

Android_ListView分页

Android_ListView上拉加载更多(ListView分页功能)

标签:

原文地址:http://blog.csdn.net/zuo_0625/article/details/51353129

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