Android BaseAdapter適配器詳解用法
ListView和GridView
ListView,列表視圖,是Android中最重要的組件之一,幾乎每個(gè)Android應(yīng)用中都會(huì)使用ListView。是一個(gè)以垂直方式在項(xiàng)目中顯示View視圖的列表。
GridView,網(wǎng)格試視圖
一、列數(shù)
android:numColumns=“3”
二、水平的距離
android:verticalSpacing=“10dp”
二、垂直的距離
android:horizontalSpacing=“10dp”
顯示與緩存機(jī)制
ListView、GridView等控件可以展示大量的數(shù)據(jù)信息。如果ListView有100條信息,但是屏幕的尺寸是有限的,一屏幕只能顯示下圖中的10條。當(dāng)向上滑動(dòng)ListView的時(shí)候,條目1被滑出了屏幕區(qū)域,那么系統(tǒng)就會(huì)將條目2回收到Recycler中,即View緩沖池中,而將要顯示的條目11則會(huì)從緩存池中取出布局文件,并重新設(shè)置好條目11需要顯示的數(shù)據(jù),并放入需要顯示的位置。這就是ListView的緩沖機(jī)制,需要時(shí)才顯示,顯示完就被會(huì)收到緩存。
BaseAdapter
BaseAdapter是一種Adapter,在Android中,Adapter為適配器,可以構(gòu)建數(shù)據(jù)源與視圖展示的橋梁,從而讓數(shù)據(jù)源與視圖展示相互關(guān)聯(lián),同時(shí)又解除耦合。
繼承此類來實(shí)現(xiàn)BaseAdapter的四個(gè)方法:
public int getCount(): 適配器中數(shù)據(jù)集的數(shù)據(jù)個(gè)數(shù); public Object getItem(int position): 獲取數(shù)據(jù)集中與索引對應(yīng)的數(shù)據(jù)項(xiàng); public long getItemId(int position): 獲取指定行對應(yīng)的ID; public View getView(int position,View convertView,ViewGroup parent): 獲取沒一行Item的顯示內(nèi)容。
使用演示
實(shí)現(xiàn)簡單的微信主頁面
布局
ListView
<?xml version="1.0" encoding="utf-8"?>
<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"
tools:context="com.qingsu.weixin.WeiXinStart"
android:orientation="vertical"
>
<Toolbar
android:layout_width="match_parent"
android:layout_height="40dp"
>
<ImageView
android:layout_width="24dp"
android:layout_height="24dp"
android:layout_gravity="right"
android:layout_marginRight="15dp"
android:background="@mipmap/jia"
/>
<ImageView
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_marginRight="15dp"
android:layout_gravity="right"
android:background="@mipmap/chaxun"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="24dp"
android:layout_marginLeft="50dp"
android:gravity="center"
android:text="微信(999)"
android:textSize="16sp"
android:textColor="#000"
/>
</Toolbar>
<ListView
android:id="@+id/lsViewWeiXin"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</LinearLayout>
所加條目
<?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"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_marginLeft="10dp"
android:orientation="horizontal"
>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
>
<ImageView
android:id="@+id/imgAvatar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:src="@mipmap/touxiang0" />
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="4"
android:orientation="vertical"
android:gravity="center_vertical"
>
<TextView
android:id="@+id/tvUserName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="這是網(wǎng)名"
android:textColor="#000"
android:textSize="14sp"
/>
<TextView
android:id="@+id/tvMsg"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="3dp"
android:text="最初的消息"
android:textSize="10sp"
/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
</LinearLayout>
創(chuàng)建數(shù)據(jù)源
JavaBean存放數(shù)據(jù)
public class ItemBean {
private String userName;
private String Msg;
private int imgId;
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getMsg() {
return Msg;
}
public void setMsg(String msg) {
Msg = msg;
}
public int getImgId() {
return imgId;
}
public void setImgId(int imgId) {
this.imgId = imgId;
}
public ItemBean(String userName, String msg, int imgId) {
this.userName = userName;
Msg = msg;
this.imgId = imgId;
}
}
初始化數(shù)據(jù)源
ArrayList<ItemBean> list = new ArrayList<ItemBean>();
ListView listView = findViewById(R.id.lsViewWeiXin);
int[] imageNmae = {R.mipmap.touxiang0, R.mipmap.touxiang1, R.mipmap.touxiang2, R.mipmap.touxiang3,
R.mipmap.touxiang4, R.mipmap.touxiang5, R.mipmap.touxiang6, R.mipmap.touxiang7,
R.mipmap.touxiang8, R.mipmap.touxiang9, R.mipmap.touxiang10, R.mipmap.touxiang11,
R.mipmap.touxiang12, R.mipmap.touxiang13, R.mipmap.touxiang14, R.mipmap.touxiang15,
R.mipmap.touxiang16, R.mipmap.touxiang17, R.mipmap.touxiang18, R.mipmap.touxiang19,
};
for (int i = 0; i < 19; i++) {
String userName = "微信好友" + i;
String userMsg = "這是一條很長很長很長的語音消息建議您刪除好友" + i;
list.add(new ItemBean(userName, userMsg, imageNmae[i]));
}
設(shè)置條目的單機(jī)和長按事件
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(WeiXinStart.this, "第" + position + "個(gè)條目被點(diǎn)擊了", Toast.LENGTH_SHORT).show();
}
});
listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(WeiXinStart.this, "第" + position + "個(gè)條目被點(diǎn)長按了", Toast.LENGTH_SHORT).show();
return false;
}
});
創(chuàng)建BaseAdapter及設(shè)置緩存
一般采用內(nèi)部類的形式進(jìn)行適配器的自定義
緩存方式
創(chuàng)建ViewHolder類,創(chuàng)建布局映射關(guān)系;
判斷convertView,為空則創(chuàng)建,并設(shè)置tag,不為空則通過tag取出ViewHolder;
給ViewHolder的控件設(shè)置數(shù)據(jù)
class MyAdapter extends BaseAdapter {
@Override
//ListView需要顯示的數(shù)據(jù)數(shù)量
public int getCount() {
return list.size();
}
@Override
//指定的索引對應(yīng)的數(shù)據(jù)項(xiàng)
public Object getItem(int position) {
return list.get(position);
}
@Override
//指定的索引對應(yīng)的數(shù)據(jù)項(xiàng)ID
public long getItemId(int position) {
return position;
}
@Override
//返回每一項(xiàng)的顯示內(nèi)容
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder = new ViewHolder();
//如果view未被實(shí)例化過,緩存池中沒有對應(yīng)的緩存
if(convertView == null){
//進(jìn)行地址初始化
convertView = LayoutInflater.from(WeiXinStart.this).inflate(R.layout.itemweixin_list,null);
viewHolder.tvName = convertView.findViewById(R.id.tvUserName);
viewHolder.tvMsg = convertView.findViewById(R.id.tvMsg);
viewHolder.imgAvatar = convertView.findViewById(R.id.imgAvatar);
convertView.setTag(viewHolder);//通過setTag將convertView與viewHolder關(guān)聯(lián)
} else {
//如果緩存池中有對應(yīng)的view緩存,則直接通過getTag取出viewHolder
viewHolder = (ViewHolder)convertView.getTag();
}
// 取出bean對象
ItemBean itemBean = list.get(position);
// 設(shè)置控件的數(shù)據(jù)
viewHolder.tvName.setText(itemBean.getUserName());
viewHolder.tvMsg.setText(itemBean.getMsg());
viewHolder.imgAvatar.setImageResource(itemBean.getImgId());
return convertView;
}
//用于緩存
class ViewHolder{
//修改的組件
TextView tvName,tvMsg;
ImageView imgAvatar;
}
}
設(shè)置適配器
//創(chuàng)建適配器對象
MyAdapter myAdapter = new MyAdapter();
//設(shè)置適配器
listView.setAdapter(myAdapter);
完整主代碼
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import com.qingsu.bean.ItemBean;
import com.qingsu.yingguday05.R;
import java.util.ArrayList;
public class WeiXinStart extends AppCompatActivity {
ListView listView;
ArrayList<ItemBean> list = new ArrayList<ItemBean>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_wei_xin_start);
myBaseAdapter();
}
public void myBaseAdapter() {
//初始化數(shù)據(jù)
listView = findViewById(R.id.lsViewWeiXin);
int[] imageNmae = {R.mipmap.touxiang0, R.mipmap.touxiang1, R.mipmap.touxiang2, R.mipmap.touxiang3,
R.mipmap.touxiang4, R.mipmap.touxiang5, R.mipmap.touxiang6, R.mipmap.touxiang7,
R.mipmap.touxiang8, R.mipmap.touxiang9, R.mipmap.touxiang10, R.mipmap.touxiang11,
R.mipmap.touxiang12, R.mipmap.touxiang13, R.mipmap.touxiang14, R.mipmap.touxiang15,
R.mipmap.touxiang16, R.mipmap.touxiang17, R.mipmap.touxiang18, R.mipmap.touxiang19,
};
for (int i = 0; i < 19; i++) {
String userName = "微信好友" + i;
String userMsg = "這是一條很長很長很長的語音消息建議您刪除好友" + i;
list.add(new ItemBean(userName, userMsg, imageNmae[i]));
}
//創(chuàng)建適配器對象
MyAdapter myAdapter = new MyAdapter();
listView.setAdapter(myAdapter);
//單機(jī)事件
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(WeiXinStart.this, "第" + position + "個(gè)條目被點(diǎn)擊了", Toast.LENGTH_SHORT).show();
}
});
//長按事件
listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(WeiXinStart.this, "第" + position + "個(gè)條目被點(diǎn)長按了", Toast.LENGTH_SHORT).show();
return false;
}
});
}
class MyAdapter extends BaseAdapter {
@Override
//ListView需要顯示的數(shù)據(jù)數(shù)量
public int getCount() {
return list.size();
}
@Override
//指定的索引對應(yīng)的數(shù)據(jù)項(xiàng)
public Object getItem(int position) {
return list.get(position);
}
@Override
//指定的索引對應(yīng)的數(shù)據(jù)項(xiàng)ID
public long getItemId(int position) {
return position;
}
@Override
//返回每一項(xiàng)的顯示內(nèi)容
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder = new ViewHolder();
//如果view未被實(shí)例化過,緩存池中沒有對應(yīng)的緩存
if(convertView == null){
//進(jìn)行地址初始化
convertView = LayoutInflater.from(WeiXinStart.this).inflate(R.layout.itemweixin_list,null);
viewHolder.tvName = convertView.findViewById(R.id.tvUserName);
viewHolder.tvMsg = convertView.findViewById(R.id.tvMsg);
viewHolder.imgAvatar = convertView.findViewById(R.id.imgAvatar);
convertView.setTag(viewHolder);//通過setTag將convertView與viewHolder關(guān)聯(lián)
} else {
//如果緩存池中有對應(yīng)的view緩存,則直接通過getTag取出viewHolder
viewHolder = (ViewHolder)convertView.getTag();
}
// 取出bean對象
ItemBean itemBean = list.get(position);
// 設(shè)置控件的數(shù)據(jù)
viewHolder.tvName.setText(itemBean.getUserName());
viewHolder.tvMsg.setText(itemBean.getMsg());
viewHolder.imgAvatar.setImageResource(itemBean.getImgId());
return convertView;
}
//用于緩存
class ViewHolder{
//修改的組件
TextView tvName,tvMsg;
ImageView imgAvatar;
}
}
}
演示

到此這篇關(guān)于Android BaseAdapter適配器詳解用法的文章就介紹到這了,更多相關(guān)Android BaseAdapter內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
android 自定義view實(shí)現(xiàn)彩虹進(jìn)度條功能
實(shí)現(xiàn)一個(gè)彩虹色進(jìn)度條功能,不說明具體用途大家應(yīng)該能猜到,想找別人造的輪子,但是沒有合適的,所以決定自己實(shí)現(xiàn)一個(gè),下面小編通過實(shí)例代碼給大家分享android 自定義view實(shí)現(xiàn)彩虹進(jìn)度條功能,感興趣的朋友一起看看吧2024-06-06
Android Selector獲取焦點(diǎn)后文本背景修改的實(shí)現(xiàn)代碼
這篇文章主要介紹了Android Selector獲取焦點(diǎn)后文本背景修改的實(shí)現(xiàn)代碼,本文通過demo展示和實(shí)現(xiàn)代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-11-11
Android編程實(shí)現(xiàn)將壓縮數(shù)據(jù)庫文件拷貝到安裝目錄的方法
這篇文章主要介紹了Android編程實(shí)現(xiàn)將壓縮數(shù)據(jù)庫文件拷貝到安裝目錄的方法,涉及Android處理壓縮文件的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-10-10
Flutter仿網(wǎng)易實(shí)現(xiàn)廣告卡片3D翻轉(zhuǎn)效果
在逛網(wǎng)易新聞時(shí),發(fā)現(xiàn)列表中的廣告在你滑動(dòng)的時(shí)候會(huì)有一個(gè)3D旋轉(zhuǎn)的交互引你的注意。本文將利用Flutter實(shí)現(xiàn)這一效果,感興趣的可以了解一下2022-04-04
Android中簡單的電話管理與短信管理App編寫實(shí)例
這篇文章主要介紹了Android中簡單的電話管理與短信管理App編寫實(shí)例,包括監(jiān)聽電話的呼叫狀態(tài)以及短信群發(fā)聯(lián)系人選擇等基本功能的實(shí)現(xiàn),代碼突出要點(diǎn),需要的朋友可以參考下2016-04-04
flutter ExpansionTile 層級菜單的實(shí)現(xiàn)
這篇文章主要介紹了flutter ExpansionTile 層級菜單的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-07-07

