Android开发中肯定会接触到Adapter,Adapter也有很多种,大部分工作都是使用BaseAdapter,理由很简单,就是可以自定义啦。今天就写下个人对BaseAdapter的理解,以下是Demo:
布局:
布局肯定需要两个,一个是容器的布局,一个是Item的布局,这里采用了ListView,ListView肯定会有很多行,每一行就是一个Item,所以每个Item的布局也必须画出来。
主布局如下,bg_layout是自己写的背景,大家随意
activity_main.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:background="@drawable/bg_layout" android:gravity="center" android:orientation="vertical"> <ListView android:id="@+id/listview_activity_main" android:layout_width="match_parent" android:layout_height="wrap_content" android:divider="#BFFFFFFF" android:dividerHeight="0.5dp" android:scrollbars="none" /> </LinearLayout>
Item布局如下
<?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="50dp" android:layout_gravity="center" android:orientation="horizontal"> <ImageView android:id="@+id/imageview_item_activity_main" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:scaleType="center" android:src="@mipmap/ic_launcher" /> <TextView android:id="@+id/textview_item_activity_main" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:gravity="center" android:singleLine="true" android:text="测试" android:textColor="@android:color/white" android:textSize="16dp" /> </LinearLayout>
代码:
使用BaseAdapter呢肯定要继承BaseAdapter,然后Item也必须有个实体类,BaseAdapter中进行数据的绑定,主类中传递数据到BaseAdapter中。实体类的作用就是存储数据。
Item代码如下
DemoItem:
/** * Item * * @author yuzhentao */ public class DemoItem {
private int demoItemImage; private int demoItemText; public int getDemoItemImage() {
return demoItemImage; }
public void setDemoItemImage(int demoItemImage) {
this.demoItemImage = demoItemImage; }
public int getDemoItemText() {
return demoItemText; }
public void setDemoItemText(int demoItemText) {
this.demoItemText = demoItemText; }
}
BaseAdapter有很多实现方式,普遍做法就是采用ViewHolder
DemoAdapter代码如下
DemoAdapter:
/** * Adapter * * @author yuzhentao */ public class DemoAdapter extends BaseAdapter {
private Context context; private ArrayList<DemoItem> demoItemList; private LayoutInflater layoutInflater; public DemoAdapter(Context context, ArrayList<DemoItem> demoItemList) {
this.context = context; this.demoItemList = demoItemList; layoutInflater = LayoutInflater.from(context); }
@Override public int getCount() {
return demoItemList.size(); }
@Override public Object getItem(int position) {
return demoItemList.get(position); }
@Override public long getItemId(int position) {
return position; }
@Override public View getView(int position, View convertView, ViewGroup parent) {
DemoItemViewHolder holder; if (convertView == null) {
convertView = layoutInflater.inflate(R.layout.item_activity_main, parent, false); holder = new DemoItemViewHolder(convertView); convertView.setTag(holder); } else {
holder = (DemoItemViewHolder) convertView.getTag(); }
DemoItem demoItem = demoItemList.get(position); holder.iv.setImageResource(demoItem.getDemoItemImage()); holder.tv.setText(demoItem.getDemoItemText()); return convertView; }
private static class DemoItemViewHolder {
ImageView iv; TextView tv; private DemoItemViewHolder(View convertView) {
iv = (ImageView) convertView.findViewById(R.id.imageview_item_activity_main); tv = (TextView) convertView.findViewById(R.id.textview_item_activity_main); }
}
}
最后当然是主界面啦
MainActivity代码如下
MainActivity:
/** * 主界面 * * @author yuzhentao */ public class MainActivity extends Activity {
private Context context; private int[] demoItemImages = {R.drawable.ic_activity_main_1, R.drawable.ic_activity_main_2, R.drawable.ic_activity_main_3, R.drawable.ic_activity_main_4, R.drawable.ic_activity_main_5}; private int[] demoItemTexts = {R.string.activity_main_1, R.string.activity_main_2, R.string.activity_main_3, R.string.activity_main_4, R.string.activity_main_5}; private ArrayList<DemoItem> demoItemList = new ArrayList<>(); @Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); setContentView(); initActivity(); initView(); }
private void setContentView() {
setContentView(R.layout.activity_main); }
private void initActivity() {
context = this; }
private void initView() {
ListView lv = (ListView) findViewById(R.id.listview_activity_main); for (int i = 0; i < 5; i++) {
DemoItem demoItem = new DemoItem(); demoItem.setDemoItemImage(demoItemImages[i]); demoItem.setDemoItemText(demoItemTexts[i]); demoItemList.add(demoItem); }
DemoAdapter demoAdapter = new DemoAdapter(context, demoItemList); lv.setAdapter(demoAdapter); }
}
两个数组里放的是图片和文字的ID,这里不写出来了。
最后是效果,希望对大家有点帮助: