Android開發(fā)之利用ListView動(dòng)態(tài)刷新某個(gè)Item
前言
本文實(shí)現(xiàn)的是使用ViewHolder來刷新某項(xiàng)數(shù)據(jù),而不用每次都全部刷新數(shù)據(jù)。下面話不多說,來看看詳細(xì)的介紹。
實(shí)現(xiàn)方法
繼承BaseAdapter,新建ViewHolder類。
public class TestListAdapter extends BaseAdapter { private Context mContext; private List<String> strList; public TestListAdapter(Context context, List<String> list) { super(); this.mContext = context; this.strList = list; } @Override public int getCount() { // TODO Auto-generated method stub return strList.size(); } @Override public Object getItem(int position) { // TODO Auto-generated method stub return position; } @Override public long getItemId(int position) { // TODO Auto-generated method stub return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { // TODO Auto-generated method stub ViewHolder holder = null; if (null == convertView) { convertView = LayoutInflater.from(mContext).inflate(R.layout.line, null); holder = new ViewHolder(); holder.iDText = (TextView) convertView.findViewById(R.id.textView_id); holder.strText = (TextView) convertView.findViewById(R.id.textView_str); convertView.setTag(holder); } else { holder = (ViewHolder) convertView.getTag(); } holder.iDText.setText(position + ""); String str = strList.get(position); holder.strText.setText(str); return convertView; } private static class ViewHolder { private TextView iDText; private TextView strText; } public void updataView(int posi, ListView listView) { int visibleFirstPosi = listView.getFirstVisiblePosition(); int visibleLastPosi = listView.getLastVisiblePosition(); if (posi >= visibleFirstPosi && posi <= visibleLastPosi) { View view = listView.getChildAt(posi - visibleFirstPosi); ViewHolder holder = (ViewHolder) view.getTag(); String txt = holder.strText.getText().toString(); txt = txt + "++;"; holder.strText.setText(txt); strList.set(posi, txt); } else { String txt = strList.get(posi); txt = txt + "++;"; strList.set(posi, txt); } } }
在Activity中,調(diào)用updateView()
方法,刷新數(shù)據(jù)。
public class MainActivity extends Activity { private MainActivity mContext; private EditText idEdit; private TextView textView; private List<String> strList = new ArrayList<String>(); private ListView listView; private TestListAdapter ListAdapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mContext = this; for (int i = 0; i < 100; i++) { strList.add("test data"); } idEdit = (EditText) findViewById(R.id.edittext_id); textView = (TextView) findViewById(R.id.textview_modify); listView = (ListView) findViewById(R.id.listview); ListAdapter = new TestListAdapter(mContext, strList); listView.setAdapter(ListAdapter); //動(dòng)態(tài)刷新 textView.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub String idStr = idEdit.getText().toString(); int idInt = Integer.parseInt(idStr); ListAdapter.updataView(idInt, listView);//動(dòng)態(tài)修改 } }); } }
給出布局文件:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#FFFFFF" android:orientation="vertical" > <LinearLayout android:layout_width="match_parent" android:layout_height="40dp" android:orientation="horizontal" > <EditText android:id="@+id/edittext_id" android:layout_width="200dp" android:layout_height="wrap_content" android:hint="put modify id" /> <TextView android:id="@+id/textview_modify" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="動(dòng)態(tài)修改" android:textColor="#123456" /> </LinearLayout> <ListView android:id="@+id/listview" android:layout_width="match_parent" android:layout_height="wrap_content" > </ListView> </LinearLayout>
總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作能帶來一定的幫助,如果有疑問大家可以留言交流。
- Android實(shí)現(xiàn)listview動(dòng)態(tài)加載數(shù)據(jù)分頁的兩種方法
- android ListView內(nèi)數(shù)據(jù)的動(dòng)態(tài)添加與刪除實(shí)例代碼
- android動(dòng)態(tài)布局之動(dòng)態(tài)加入TextView和ListView的方法
- Android實(shí)現(xiàn)Listview異步加載網(wǎng)絡(luò)圖片并動(dòng)態(tài)更新的方法
- Android編程實(shí)現(xiàn)動(dòng)態(tài)更新ListView的方法
- Android listview動(dòng)態(tài)加載列表項(xiàng)實(shí)現(xiàn)代碼
- Android實(shí)現(xiàn)ListView數(shù)據(jù)動(dòng)態(tài)加載的方法
- Android通過Handler與AsyncTask兩種方式動(dòng)態(tài)更新ListView(附源碼)
- Android ListView中動(dòng)態(tài)顯示和隱藏Header&Footer的方法
- Android ListView中headerview的動(dòng)態(tài)顯示和隱藏的實(shí)現(xiàn)方法
- Android開發(fā)中Listview動(dòng)態(tài)加載數(shù)據(jù)的方法示例
相關(guān)文章
Android實(shí)現(xiàn)搜索保存歷史記錄功能
這篇文章主要介紹了Android實(shí)現(xiàn)搜索保存歷史記錄功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-05-05Android開發(fā)之TextView控件用法實(shí)例總結(jié)
這篇文章主要介紹了Android開發(fā)之TextView控件用法,結(jié)合實(shí)例形式總結(jié)分析了TextView控件常用的屬性設(shè)置及使用技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2016-02-02Android自定義控件實(shí)現(xiàn)簡單的輪播圖控件
這篇文章主要介紹了Android自定義控件實(shí)現(xiàn)簡單的輪播圖控件的相關(guān)資料,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-04-04Android應(yīng)用內(nèi)存優(yōu)化指南
內(nèi)存優(yōu)化是提升 Android 應(yīng)用性能和用戶體驗(yàn)的關(guān)鍵環(huán)節(jié),內(nèi)存泄漏、內(nèi)存抖動(dòng)、不合理的數(shù)據(jù)結(jié)構(gòu)或資源占用等問題都可能導(dǎo)致應(yīng)用卡頓、崩潰或后臺(tái)被殺,本文從常見問題場景、優(yōu)化方案、工具使用和最佳實(shí)踐四個(gè)方面深入解析,需要的朋友可以參考下2025-03-03詳解Android中使用OkHttp發(fā)送HTTP的post請求的方法
OkHttp(github.com/square/okhttp)是近來人氣迅速攀升的一款第三方安卓HTTP支持包,這里我們就來詳解Android中使用OkHttp發(fā)送HTTP的post請求的方法2016-07-07