欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android實現(xiàn)ListView分頁加載數(shù)據(jù)

 更新時間:2021年11月22日 13:14:29   作者:xiyangyang8110  
這篇文章主要為大家詳細介紹了Android實現(xiàn)ListView分頁加載數(shù)據(jù),文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實例為大家分享了ListView分頁加載數(shù)據(jù)的具體代碼,供大家參考,具體內(nèi)容如下

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;  // 當前窗口可見項總數(shù)
    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); //設(shè)置列表底部視圖

        initAdapter();

        setListAdapter(adapter);    //自動為id是list的ListView設(shè)置適配器

        listView.setOnScrollListener(this);  //添加滑動監(jiān)聽
    }

    /**
     * 初始化適配器
     */
    private void initAdapter() {
        ArrayList<String> items = new ArrayList<String>();
        for (int i = 0; i < 16; i++) {
            items.add(String.valueOf(i + 1));
        }
        adapter = new ListViewAdapter(this, items);
    }

    /**
     * 滑動時被調(diào)用
     */
    @Override
    public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
        this.visibleItemCount = visibleItemCount;
        visibleLastIndex = firstVisibleItem + visibleItemCount - 1;
    }

    /**
     * 滑動狀態(tài)改變時被調(diào)用
     */
    @Override
    public void onScrollStateChanged(AbsListView view, int scrollState) {
        int itemsLastIndex = adapter.getCount() - 1; //數(shù)據(jù)集最后一項的索引
        int lastIndex = itemsLastIndex + 1;    //加上底部的loadMoreView項
        if (scrollState == AbsListView.OnScrollListener.SCROLL_STATE_IDLE && visibleLastIndex == lastIndex) {
            //如果是自動加載,可以在這里放置異步加載數(shù)據(jù)的代碼
            Log.e("wy", "loading...");

            handler.postDelayed(new Runnable() {
                @Override
                public void run() {

                    loadData();

                    adapter.notifyDataSetChanged(); //數(shù)據(jù)集變化后,通知adapter
                    listView.setSelection(visibleLastIndex - visibleItemCount + 1); //設(shè)置選中項

                    loadMoreButton.setText("load more"); //恢復(fù)按鈕文字
                }
            }, 1000);
        }
    }

    /**
     * 點擊按鈕事件
     * @param view
     */
    public void loadMore(View view) {
        loadMoreButton.setText("loading..."); //設(shè)置按鈕文字loading
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {

                loadData();

                adapter.notifyDataSetChanged(); //數(shù)據(jù)集變化后,通知adapter
                listView.setSelection(visibleLastIndex - visibleItemCount + 1); //設(shè)置選中項

                loadMoreButton.setText("load more"); //恢復(fù)按鈕文字
            }
        }, 1000);
    }

    /**
     * 模擬加載數(shù)據(jù)
     */
    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<String> items;
    private LayoutInflater inflater;

    public ListViewAdapter(Context context, List<String> 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

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:paddingLeft="3dp"
    android:paddingRight="3dp">
    <ListView
        android:id="@id/android:list"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"/>
</LinearLayout>

list_fy_item.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <TextView
        android:id="@+id/list_item_text"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:text="aa"
        android:gravity="center"
        android:textSize="20sp"
        android:paddingTop="10dp"
        android:paddingBottom="10dp"/>
</LinearLayout>

load_more.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content">
    <Button
        android:id="@+id/loadMoreButton"
        android:visibility="gone"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="load more"
        android:onClick="loadMore"/>
</LinearLayout>

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Android頭像上傳功能的實現(xiàn)代碼(獲取頭像加剪切)

    Android頭像上傳功能的實現(xiàn)代碼(獲取頭像加剪切)

    最近在做一個頭像上傳的項目,下面小編給大家分享Android頭像上傳功能的實現(xiàn)代碼,需要的的朋友參考下吧
    2017-08-08
  • Android判斷是否有拍照權(quán)限的實例代碼

    Android判斷是否有拍照權(quán)限的實例代碼

    android在開發(fā)中有時候要判斷應(yīng)用中是否有某項權(quán)限,下面通過本文給大家分享Android判斷是否有拍照權(quán)限的實例代碼,需要的的朋友參考下吧
    2017-07-07
  • Android RxJava創(chuàng)建操作符Interval

    Android RxJava創(chuàng)建操作符Interval

    這篇文章主要為大家詳細介紹了Android RxJava創(chuàng)建操作符Interval的方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-12-12
  • Android?RecyclerBarChart繪制使用教程

    Android?RecyclerBarChart繪制使用教程

    這篇文章主要為大家介紹了Android?RecyclerBarChart繪制使用教程,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-12-12
  • Android仿硅谷商城實現(xiàn)購物車實例代碼

    Android仿硅谷商城實現(xiàn)購物車實例代碼

    這篇文章主要介紹了Android購物車編輯實現(xiàn),小編覺得挺不錯的,一起跟隨小編過來看看吧
    2018-05-05
  • Android實現(xiàn)獲取SERIAL信息的方法

    Android實現(xiàn)獲取SERIAL信息的方法

    這篇文章主要介紹了Android實現(xiàn)獲取SERIAL信息的方法,涉及Android操作SERIAL的相關(guān)技巧,具有一定參考借鑒價值,需要的朋友可以參考下
    2015-10-10
  • Android波紋擴散效果之仿支付寶咻一咻功能實現(xiàn)波紋擴散特效

    Android波紋擴散效果之仿支付寶咻一咻功能實現(xiàn)波紋擴散特效

    這篇文章主要介紹了Android波紋擴散效果之仿支付寶咻一咻功能實現(xiàn)波紋擴散特效的相關(guān)資料,需要的朋友可以參考下
    2016-02-02
  • Android中Dialog對話框的使用小結(jié)

    Android中Dialog對話框的使用小結(jié)

    這篇文章主要給大家總結(jié)了一些關(guān)于Android中Dialog對話框的使用方法,這其中包括普通對話框、確定取消對話框、多按鈕對話框、列表對話框、帶Adapter的對話框、單選對話框以及多選對話框等,需要的朋友可以參考學(xué)習(xí),下面來一起看看詳細的介紹吧。
    2017-04-04
  • Android中程序的停止狀態(tài)詳細介紹

    Android中程序的停止狀態(tài)詳細介紹

    這篇文章主要介紹了Android中程序的停止狀態(tài)詳細介紹,本文講解了什么是程序的停止狀態(tài)、為什么Android要引入這一狀態(tài)、激活狀態(tài)和停止狀態(tài)的切換、如何變?yōu)橥V範顟B(tài)等內(nèi)容,需要的朋友可以參考下
    2015-01-01
  • Kotlin中的Checked Exception機制淺析

    Kotlin中的Checked Exception機制淺析

    這篇文章主要給大家介紹了關(guān)于Kotlin中Checked Exception機制的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-10-10

最新評論