您好,登錄后才能下訂單哦!
本篇內容介紹了“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<String> items = new ArrayList<String>(); 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<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>
“Android怎么實現ListView分頁加載數據”的內容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業相關的知識可以關注億速云網站,小編將為大家輸出更多高質量的實用文章!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。