您好,登錄后才能下訂單哦!
這期內(nèi)容當中小編將會給大家?guī)碛嘘P(guān)如何在Android應(yīng)用中分批顯示 ListView數(shù)據(jù),文章內(nèi)容豐富且以專業(yè)的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。
Android ListView數(shù)據(jù)的分批顯示
布局文件activity_call_safe2.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" > <RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content" > <TextView android:text="黑名單管理" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:text="添加" /> </RelativeLayout> <FrameLayout android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="11" > <LinearLayout android:id="@+id/ll_pb" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:visibility="invisible" android:orientation="vertical" > <ProgressBar android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="玩命加載中..." /> </LinearLayout> <include android:id="@+id/list_view_callsafe" layout="@layout/list_view_callsafe"/> </FrameLayout> </LinearLayout>邏輯文件,實現(xiàn)頁面的分頁顯示,監(jiān)聽ListView的滑動事件,滑動的時候就更新數(shù)據(jù)
CasllSafeActivity.java
package com.ldw.safe.Activity; import java.util.List; import android.app.Activity; import android.content.Context; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.view.View; import android.view.View.OnClickListener; import android.view.ViewGroup; import android.widget.AbsListView; import android.widget.AbsListView.OnScrollListener; import android.widget.EditText; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.ListView; import android.widget.TextView; import android.widget.Toast; import com.ldw.safe.R; import com.ldw.safe.adapter.MyBaseAdapter; import com.ldw.safe.bean.BlackNumberInfo; import com.ldw.safe.db.dao.BlackNumberDao; public class CallSafeActivity extends Activity { private ListView list_view_callsafe; private List<BlackNumberInfo> blackNumberInfos; private LinearLayout ll_pb; private BlackNumberDao dao; private CallSafeAdapter adapter; private int totalNumber;//總的條數(shù) private int mStartIndex = 0;//開始加載的位置 private int maxCount = 20;//每頁加載多少數(shù)據(jù) @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_call_safe2); initUi(); initData(); } //初始化數(shù)據(jù) private Handler handler = new Handler(){ @Override public void handleMessage(Message msg) { //數(shù)據(jù)初始化結(jié)束,關(guān)閉家在全的展示 ll_pb.setVisibility(View.INVISIBLE); //防止適配器一直刷新 if(adapter == null){ adapter = new CallSafeAdapter(blackNumberInfos, CallSafeActivity.this); //配置適配器 list_view_callsafe.setAdapter(adapter); }else{ adapter.notifyDataSetChanged(); } } }; private EditText et_page_number; //初始化數(shù)據(jù) private void initData() { //防止阻塞主線程,在子線程中初始化數(shù)據(jù)(后期數(shù)據(jù)會很多) new Thread(){ @Override public void run(){ dao = new BlackNumberDao(CallSafeActivity.this); //一共有多少條數(shù)據(jù) totalNumber = dao.getTotalNumber(); //分批加載數(shù)據(jù) if(blackNumberInfos == null){ //第一次加載 blackNumberInfos = dao.findPar2(mStartIndex, maxCount); }else{ //把后面的數(shù)據(jù)。追加到blackNumberInfos集合里面。防止黑名單被覆蓋 blackNumberInfos.addAll(dao.findPar2(mStartIndex, maxCount)); } handler.sendEmptyMessage(0); } }.start(); } //初始化UI private void initUi() { ll_pb = (LinearLayout) findViewById(R.id.ll_pb); //初始化的時候展示加載的圓圈 ll_pb.setVisibility(View.VISIBLE); list_view_callsafe = (ListView) findViewById(R.id.list_view_callsafe); //設(shè)置listView的滾動監(jiān)聽器 list_view_callsafe.setOnScrollListener(new OnScrollListener(){ //狀態(tài)改變時候回調(diào)的方法 /* * @param scrollState 表示滾動的狀態(tài) *AbsListView.OnScrollListener.SCROLL_STATE_IDLE 閑置狀態(tài) *AbsListView.OnScrollListener.SCROLL_STATE_TOUCH_SCROLL 手指觸摸的時候的狀態(tài) *AbsListView.OnScrollListener.SCROLL_STATE_FLING 慣性 */ @Override public void onScrollStateChanged(AbsListView view, int scrollState) { switch (scrollState) { case AbsListView.OnScrollListener.SCROLL_STATE_IDLE: //獲取到最后一條顯示的數(shù)據(jù)條數(shù) int lastVisiblePosition = list_view_callsafe.getLastVisiblePosition(); System.out.println("lastVisiblePosition==========" + lastVisiblePosition); if(lastVisiblePosition == blackNumberInfos.size() - 1){//判斷最后一個數(shù)據(jù)是不是每頁顯示的數(shù)據(jù)-1 // 加載更多的數(shù)據(jù)。 更改加載數(shù)據(jù)的開始位置 mStartIndex += maxCount; if (mStartIndex >= totalNumber) { Toast.makeText(getApplicationContext(), "沒有更多的數(shù)據(jù)了。", Toast.LENGTH_SHORT).show(); return; } initData(); } break; } } //listview滾動的時候調(diào)用的方法 //時時調(diào)用。當我們的手指觸摸的屏幕的時候就調(diào)用 @Override public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) { // TODO Auto-generated method stub } }); } //對代碼進行優(yōu)化,繼承自定義MyBaseAdapter,MyBaseAdapter繼承自BaseAdapter封裝一些常用的方法 private class CallSafeAdapter extends MyBaseAdapter<BlackNumberInfo>{ private CallSafeAdapter(List lists, Context mContext){ super(lists, mContext); } @Override public View getView(int position, View convertView, ViewGroup parent) { //谷歌推出的優(yōu)化方案 ViewHolder holder; if(convertView == null){ convertView = View.inflate(CallSafeActivity.this, R.layout.item_call_safe, null); holder = new ViewHolder(); holder.tv_number = (TextView) convertView.findViewById(R.id.tv_number); holder.tv_mode = (TextView) convertView.findViewById(R.id.tv_mode); holder.iv_delete = (ImageView) convertView.findViewById(R.id.iv_delete); convertView.setTag(holder); }else{ holder = (ViewHolder) convertView.getTag(); } holder.tv_number.setText(lists.get(position).getNumber()); String mode = lists.get(position).getMode(); if(mode.equals("1")){ holder.tv_mode.setText("電話和短信攔截"); }else if(mode.equals("2")){ holder.tv_mode.setText("電話攔截"); }else if(mode.equals("3")){ holder.tv_mode.setText("短信攔截"); } final BlackNumberInfo info = lists.get(position); //監(jiān)聽刪除數(shù)據(jù)按鈕,刪除數(shù)據(jù) holder.iv_delete.setOnClickListener(new OnClickListener(){ @Override public void onClick(View v) { String number = info.getNumber(); boolean result = dao.delete(number); if(result){ Toast.makeText(CallSafeActivity.this, "刪除成功", Toast.LENGTH_SHORT).show(); lists.remove(info); //更新ListView adapter.notifyDataSetChanged(); }else{ Toast.makeText(CallSafeActivity.this, "刪除失敗", Toast.LENGTH_SHORT).show(); } } }); return convertView; /* //谷歌推出的優(yōu)化方案 ViewHolder holder; if(convertView == null){ convertView = View.inflate(CallSafeActivity.this, R.layout.item_call_safe, null); holder = new ViewHolder(); holder.tv_number = (TextView) convertView.findViewById(R.id.tv_number); holder.tv_mode = (TextView) convertView.findViewById(R.id.tv_mode); convertView.setTag(holder); }else{ holder = (ViewHolder) convertView.getTag(); } holder.tv_number.setText(blackNumberInfos.get(position).getNumber()); String mode = blackNumberInfos.get(position).getMode(); if(mode.equals("1")){ holder.tv_mode.setText("電話和短信攔截"); }else if(mode.equals("2")){ holder.tv_mode.setText("電話攔截"); }else if(mode.equals("3")){ holder.tv_mode.setText("短信攔截"); } return convertView; */ /*初步的優(yōu)化 convertView = View.inflate(CallSafeActivity.this, R.layout.item_call_safe, null); TextView tv_number = (TextView) convertView.findViewById(R.id.tv_number); TextView tv_mode = (TextView) convertView.findViewById(R.id.tv_mode); tv_number.setText(blackNumberInfos.get(position).getNumber()); String mode = blackNumberInfos.get(position).getMode(); if(mode.equals("1")){ tv_mode.setText("電話和短信攔截"); }else if(mode.equals("2")){ tv_mode.setText("電話攔截"); }else if(mode.equals("3")){ tv_mode.setText("短信攔截"); } return convertView; */ /*傳統(tǒng)的方式 View view = View.inflate(CallSafeActivity.this, R.layout.item_call_safe, null); TextView tv_number = (TextView) view.findViewById(R.id.tv_number); TextView tv_mode = (TextView) view.findViewById(R.id.tv_mode); tv_number.setText(blackNumberInfos.get(position).getNumber()); String mode = blackNumberInfos.get(position).getMode(); if(mode.equals("1")){ tv_mode.setText("電話和短信攔截"); }else if(mode.equals("2")){ tv_mode.setText("電話攔截"); }else if(mode.equals("3")){ tv_mode.setText("短信攔截"); } return view; */ } } static class ViewHolder{ TextView tv_number; TextView tv_mode; ImageView iv_delete; } }
上述就是小編為大家分享的如何在Android應(yīng)用中分批顯示 ListView數(shù)據(jù)了,如果剛好有類似的疑惑,不妨參照上述分析進行理解。如果想知道更多相關(guān)知識,歡迎關(guān)注億速云行業(yè)資訊頻道。
免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。