您好,登錄后才能下訂單哦!
這期內容當中小編將會給大家帶來有關如何在Android應用中分批顯示 ListView數據,文章內容豐富且以專業的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。
Android ListView數據的分批顯示
布局文件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>邏輯文件,實現頁面的分頁顯示,監聽ListView的滑動事件,滑動的時候就更新數據
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;//總的條數 private int mStartIndex = 0;//開始加載的位置 private int maxCount = 20;//每頁加載多少數據 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_call_safe2); initUi(); initData(); } //初始化數據 private Handler handler = new Handler(){ @Override public void handleMessage(Message msg) { //數據初始化結束,關閉家在全的展示 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; //初始化數據 private void initData() { //防止阻塞主線程,在子線程中初始化數據(后期數據會很多) new Thread(){ @Override public void run(){ dao = new BlackNumberDao(CallSafeActivity.this); //一共有多少條數據 totalNumber = dao.getTotalNumber(); //分批加載數據 if(blackNumberInfos == null){ //第一次加載 blackNumberInfos = dao.findPar2(mStartIndex, maxCount); }else{ //把后面的數據。追加到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); //設置listView的滾動監聽器 list_view_callsafe.setOnScrollListener(new OnScrollListener(){ //狀態改變時候回調的方法 /* * @param scrollState 表示滾動的狀態 *AbsListView.OnScrollListener.SCROLL_STATE_IDLE 閑置狀態 *AbsListView.OnScrollListener.SCROLL_STATE_TOUCH_SCROLL 手指觸摸的時候的狀態 *AbsListView.OnScrollListener.SCROLL_STATE_FLING 慣性 */ @Override public void onScrollStateChanged(AbsListView view, int scrollState) { switch (scrollState) { case AbsListView.OnScrollListener.SCROLL_STATE_IDLE: //獲取到最后一條顯示的數據條數 int lastVisiblePosition = list_view_callsafe.getLastVisiblePosition(); System.out.println("lastVisiblePosition==========" + lastVisiblePosition); if(lastVisiblePosition == blackNumberInfos.size() - 1){//判斷最后一個數據是不是每頁顯示的數據-1 // 加載更多的數據。 更改加載數據的開始位置 mStartIndex += maxCount; if (mStartIndex >= totalNumber) { Toast.makeText(getApplicationContext(), "沒有更多的數據了。", Toast.LENGTH_SHORT).show(); return; } initData(); } break; } } //listview滾動的時候調用的方法 //時時調用。當我們的手指觸摸的屏幕的時候就調用 @Override public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) { // TODO Auto-generated method stub } }); } //對代碼進行優化,繼承自定義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) { //谷歌推出的優化方案 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); //監聽刪除數據按鈕,刪除數據 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; /* //谷歌推出的優化方案 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; */ /*初步的優化 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; */ /*傳統的方式 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應用中分批顯示 ListView數據了,如果剛好有類似的疑惑,不妨參照上述分析進行理解。如果想知道更多相關知識,歡迎關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。