Android ListView自定義Adapter實(shí)現(xiàn)仿QQ界面
PS:listview中有一些簡(jiǎn)單使用的適配器,如:SimpleAdapter:構(gòu)造方法SimpleAdapter(Context context,List<Map<String,?>> data,reString [] from,int [] to),但這種適配器過于單調(diào),往往不能達(dá)到用戶想要的效果,想要隨心所欲,就用到了BaseAdapter,自定義適配器。
如圖:
1、首先寫布局文件
activity_layout.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"> <ListView android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/id_lv"> </ListView> </LinearLayout>
沒一個(gè)item的樣式:itemstyle_layout.xml 其中的圖片自己隨便找個(gè)即可。
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:orientation="horizontal" android:layout_height="match_parent"> <ImageView android:layout_width="70dp" android:layout_height="80dp" android:src="@drawable/tou"/> <LinearLayout android:layout_width="0dp" android:layout_weight="0.9" android:layout_height="80dp" android:orientation="vertical"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:textSize="35dp" android:text="11111" android:id="@+id/id_item_tv"/> <TextView android:layout_marginTop="5dp" android:layout_width="match_parent" android:layout_height="wrap_content" android:textSize="20dp" android:text="11111" android:id="@+id/id_item_tv2"/> </LinearLayout> <LinearLayout android:layout_width="0dp" android:layout_height="80dp" android:layout_weight="0.1" android:orientation="vertical"> <ImageView android:layout_width="match_parent" android:layout_height="50dp" android:src="@drawable/xin1"/> </LinearLayout> </LinearLayout>
2、創(chuàng)建適配器MyAdapter.java (已優(yōu)化)
申明一點(diǎn),適配中有優(yōu)化方法,如果不優(yōu)化的話,有時(shí)也可以加載出來,但有時(shí)就會(huì)加載錯(cuò)亂,沒調(diào)用一次就會(huì)多浪費(fèi)創(chuàng)建一個(gè)view對(duì)象,如:
View item = LayoutInflater.from(context).inflate(R.layout.itemstyle_layout, null); // TextView info = (TextView)item.findViewById(R.id.id_item_tv); // info.setText(list.get(position)+"");
優(yōu)化后(加入了Viewholder)
public class MyAdapter extends BaseAdapter{ // public List<Map<String,String>> list; public Context context; public MyAdapter (Context context,List<Map<String,String>> list){ this.context=context; this.list=list; }
@Override public int getCount() { return list.size();//返回個(gè)數(shù) } @Override public Object getItem(int position) { return list.get(position);//返回項(xiàng) } @Override public long getItemId(int position) { return position;//角標(biāo) } @Override public View getView(int position, View convertView, ViewGroup parent) { // View item = LayoutInflater.from(context).inflate(R.layout.itemstyle_layout, null); // TextView info = (TextView)item.findViewById(R.id.id_item_tv); // info.setText(list.get(position)+""); ViewHolder viewHolder; if(convertView ==null){ viewHolder= new ViewHolder(); convertView=LayoutInflater.from(context).inflate(R.layout.itemstyle_layout,null);//加載布局 viewHolder.tv1= (TextView) convertView.findViewById(R.id.id_item_tv); viewHolder.tv2= (TextView) convertView.findViewById(R.id.id_item_tv2); convertView.setTag(viewHolder); }else{ viewHolder= (ViewHolder) convertView.getTag(); } viewHolder.tv1.setText(list.get(position).get("tv1")+""); viewHolder.tv2.setText(list.get(position).get("tv2")+""); return convertView; } static class ViewHolder { ImageView iv; TextView tv1; TextView tv2; }
3、在MainActivity.java中加載listview控件并把list賦值。
public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ListView lv= (ListView) findViewById(R.id.id_lv); List<Map<String,String>> list=new ArrayList<Map<String,String>> (); for(int i=0;i<10;i++){ Map<String,String> map=new HashMap<>(); map.put("tv1","111111"); map.put("tv2","222222"); list.add(map); } MyAdapter ma=new MyAdapter(this,list); lv.setAdapter(ma); } }
完事。
總結(jié):代碼優(yōu)化加入Viewholder的好處:(官方翻譯)
重用緩存convertView傳遞給getView()方法來避免填充不必要的視圖
使用ViewHolder模式來避免沒有必要的調(diào)用findViewById():因?yàn)樘嗟膄indViewById也會(huì)影響性能
ViewHolder模式通過getView()方法返回的視圖的標(biāo)簽(Tag)中存儲(chǔ)一個(gè)數(shù)據(jù)結(jié)構(gòu),這個(gè)數(shù)據(jù)結(jié)構(gòu)包含了指向我們要綁定數(shù)據(jù)的視圖的引用,從而避免每次調(diào)用getView()的時(shí)候調(diào)用findViewById())。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android TelephonyManager詳解及實(shí)現(xiàn)代碼
本文主要介紹Android TelephonyManager, 這里整理了關(guān)于Android TelephoneManager的相關(guān)資料,并附有示例代碼和實(shí)現(xiàn)效果圖,有需要的朋友可以參考下2016-08-08Android 自定義組件成JAR包的實(shí)現(xiàn)方法
這篇文章主要介紹了Android 自定義組件成JAR包的實(shí)現(xiàn)方法的相關(guān)資料,偶爾會(huì)用到這樣的功能,如果你自己自定義的組件很好,需要的朋友可以參考下2016-11-11Android自定義控件實(shí)現(xiàn)帶數(shù)值和動(dòng)畫的圓形進(jìn)度條
這篇文章主要為大家詳細(xì)介紹了Android自定義控件實(shí)現(xiàn)帶數(shù)值和動(dòng)畫的圓形進(jìn)度條,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-12-12Android 仿抖音的評(píng)論列表的UI和效果的實(shí)現(xiàn)代碼
抖音是一款音樂創(chuàng)意短視頻社交軟件,此app已在android各大應(yīng)用商店和app store 上線。下面小編給大家?guī)砹薃ndroid 仿抖音的評(píng)論列表的UI和效果的實(shí)現(xiàn)代碼,感興趣的朋友參考下吧2018-03-03Android:Field can be converted to a local varible.的解決辦法
這篇文章主要介紹了Android:Field can be converted to a local varible.的解決辦法的相關(guān)資料,希望通過本文能幫助到大家,讓大家遇到這樣的問題輕松解決,需要的朋友可以參考下2017-10-10android截圖事件監(jiān)聽的原理與實(shí)現(xiàn)
本篇文章主要介紹了android截圖事件監(jiān)聽的原理與實(shí)現(xiàn),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-07-07Android百度地圖應(yīng)用之MapFragment的使用
這篇文章主要為大家詳細(xì)介紹了Android百度地圖應(yīng)用之MapFragment的使用的相關(guān)資料,需要的朋友可以參考下2016-06-06