千家信息网

Android怎么实现ListView分页加载数据

发表于:2025-01-20 作者:千家信息网编辑
千家信息网最后更新 2025年01月20日,本篇内容介绍了"Android怎么实现ListView分页加载数据"的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能
千家信息网最后更新 2025年01月20日Android怎么实现ListView分页加载数据

本篇内容介绍了"Android怎么实现ListView分页加载数据"的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!

FenyeActivity

package com.example.myapplication.fenye;import androidx.appcompat.app.AppCompatActivity;import android.app.ListActivity;import android.os.Bundle;import android.os.Handler;import android.util.Log;import android.view.View;import android.widget.AbsListView;import android.widget.Button;import android.widget.ListView;import com.example.myapplication.R;import java.util.ArrayList;public class FenyeActivity extends ListActivity implements AbsListView.OnScrollListener {    private ListView listView;    private int visibleLastIndex = 0; //最后的可视项索引    private int visibleItemCount;  // 当前窗口可见项总数    private ListViewAdapter adapter;    private View loadMoreView;    private Button loadMoreButton;    private Handler handler = new Handler();    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_fenye);        loadMoreView = getLayoutInflater().inflate(R.layout.load_more, null);        loadMoreButton = (Button) loadMoreView.findViewById(R.id.loadMoreButton);        listView = getListView();    //获取id是list的ListView        listView.addFooterView(loadMoreView); //设置列表底部视图        initAdapter();        setListAdapter(adapter);    //自动为id是list的ListView设置适配器        listView.setOnScrollListener(this);  //添加滑动监听    }    /**     * 初始化适配器     */    private void initAdapter() {        ArrayList items = new ArrayList();        for (int i = 0; i < 16; i++) {            items.add(String.valueOf(i + 1));        }        adapter = new ListViewAdapter(this, items);    }    /**     * 滑动时被调用     */    @Override    public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {        this.visibleItemCount = visibleItemCount;        visibleLastIndex = firstVisibleItem + visibleItemCount - 1;    }    /**     * 滑动状态改变时被调用     */    @Override    public void onScrollStateChanged(AbsListView view, int scrollState) {        int itemsLastIndex = adapter.getCount() - 1; //数据集最后一项的索引        int lastIndex = itemsLastIndex + 1;    //加上底部的loadMoreView项        if (scrollState == AbsListView.OnScrollListener.SCROLL_STATE_IDLE && visibleLastIndex == lastIndex) {            //如果是自动加载,可以在这里放置异步加载数据的代码            Log.e("wy", "loading...");            handler.postDelayed(new Runnable() {                @Override                public void run() {                    loadData();                    adapter.notifyDataSetChanged(); //数据集变化后,通知adapter                    listView.setSelection(visibleLastIndex - visibleItemCount + 1); //设置选中项                    loadMoreButton.setText("load more"); //恢复按钮文字                }            }, 1000);        }    }    /**     * 点击按钮事件     * @param view     */    public void loadMore(View view) {        loadMoreButton.setText("loading..."); //设置按钮文字loading        handler.postDelayed(new Runnable() {            @Override            public void run() {                loadData();                adapter.notifyDataSetChanged(); //数据集变化后,通知adapter                listView.setSelection(visibleLastIndex - visibleItemCount + 1); //设置选中项                loadMoreButton.setText("load more"); //恢复按钮文字            }        }, 1000);    }    /**     * 模拟加载数据     */    private void loadData() {        int count = adapter.getCount();        for (int i = count; i < count + 10; i++) {            adapter.addItem(String.valueOf(i + 1));        }    }}

ListViewAdapter

package com.example.myapplication.fenye;import java.util.List;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.example.myapplication.R;public class ListViewAdapter extends BaseAdapter {    private List items;    private LayoutInflater inflater;    public ListViewAdapter(Context context, List items) {        this.items = items;        inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);    }    @Override    public int getCount() {        return items.size();    }    @Override    public Object getItem(int position) {        return items.get(position);    }    @Override    public long getItemId(int position) {        return position;    }    @Override    public View getView(int position, View view, ViewGroup parent) {        if (view == null) {            view = inflater.inflate(R.layout.list_fy_item, null);        }        TextView text = (TextView) view.findViewById(R.id.list_item_text);        if(null!=items){            text.setText(items.get(position));        }        return view;    }    /**     * 添加列表项     * @param item     */    public void addItem(String item) {        items.add(item);    }}

activity_fenye.xml

    

list_fy_item.xml

    

load_more.xml

    

"Android怎么实现ListView分页加载数据"的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注网站,小编将为大家输出更多高质量的实用文章!

0