这是一个继承了BaseAdapter的工具类,里面已经实现了几个简单方法。 使用这个工具类可以很更方便的使用BaseAdapter来进行ListView页面数据的适配。
使用这个工具类的步骤:
1.需要重写构造方法传入集合类型的数据或数组类型的数据
2.重写getView的方法实现具体视图的显示
ListView的显示只支持集合数据,这里已经把数组数据进行过转换了,所以才能直接传入数组的数据。
一.工具类的设计
package com.android.utils;
import android.content.Context;
import android.widget.BaseAdapter;
import java.util.ArrayList;
import java.util.List;
/**
* 这是一个简化BaseAdapter适配器的工具类
* 这是使用的是定义一个泛型T,使用时传入什么数据,T就是什么数据
* 实际设计中除了getVIew方法外,其他的方法基本是差不多的
* 所以继承这个工具类后只要重写getView方法,就可以使用BaseAdapter了
*/
public
abstract class ListItemAdapter<T> extends BaseAdapter {
List<T> list =
new ArrayList<T>();
Context context;
public ListItemAdapter(Context context, List<T> list) {
this.context = context;
this.list = list;
}
public ListItemAdapter(Context context, T[] list) {
this.context = context;
for (T t : list) {
this.list.add(t);
}
}
@Override
public int getCount() {
return list ==
null ?
0 : list.size();
}
@Override
public T getItem(int position) {
return list ==
null ?
null : list.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
}
二.调用的示例
这里分别使用数组数据和集合数据结合ListView分别显示多行数据。
(一)创建布局文件
1.主页布局activity_main.xml
<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:orientation="horizontal"
>
<ListView
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent"
android:id="@+id/lv1"
/>
<ListView
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent"
android:id="@+id/lv2"
/>
</LinearLayout>
这里设计两个ListView分别显示两种数据的情况。
2.ListView中每一个条目的布局文件item_list.xml
<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="wrap_content"
android:orientation="horizontal"
android:gravity="center_vertical"
>
<ImageView
android:layout_margin="20dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/item_iv"
android:src="@drawable/ic_launcher"
/>
<TextView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:id="@+id/item_tv"
android:text="文本内容"
/>
</LinearLayout>
这里每一个条目的设计也是比较简单的,左边是一个图片,右边是一个文本。
(二)工具类,上面已经展示!
(三)主方法的调用类
package com.example.utils;
import java.util.ArrayList;
import java.util.List;
import com.android.utils.ListItemAdapter;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Adapter;
import android.widget.ImageView;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;
/**
*Adapter工具类的使用演示
* @param <T>
*
*/
public
class MainActivity extends Activity {
ListView lv1;
ListView lv2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lv1=(ListView) findViewById(R.id.lv1);
lv2=(ListView) findViewById(R.id.lv2);
String[] data1=
new String[
100];
for(int i=
0;i<data1.length;i++){
data1[i]=
"我是条目"+i+
"....";
}
lv1.setAdapter(
new Myadater(
this,data1));
List<String>data2=
new ArrayList<String>();
for(int i=
0;i<data1.length;i++){
data2.add(
"我是行号"+i+
"....");
}
lv2.setAdapter(
new Myadater(
this,data2));
}
public
class Myadater extends ListItemAdapter<String>{
Myadater(Context context, String[] list) {
super(context, list);
}
Myadater(Context context, List<String> list) {
super(context, list);
}
@Override
public View getView(int arg0, View arg1, ViewGroup arg2) {
ViewHolder hodler;
if (arg1==
null) {
arg1=View.inflate(MainActivity.
this,R.layout.item_list,
null);
hodler=
new ViewHolder(arg1);
arg1.setTag(hodler);
}
else {
hodler=(ViewHolder) arg1.getTag();
}
hodler.imageView.setImageResource(R.drawable.ic_launcher);
hodler.textView.setText(getItem(arg0));
return arg1;
}
class ViewHolder{
ImageView imageView;
TextView textView;
public ViewHolder(View contentView) {
imageView=(ImageView) contentView.findViewById(R.id.item_iv);
textView=(TextView) contentView.findViewById(R.id.item_tv);
}
}
}
}
程序运行后显示,左右两个ListView的视图界面,如图所示:
ListView的显示在大部分的程序中都会用到,所以这个工具类收藏起来,在需要使用的时候,就可以很方便的调用了。
转载请注明原文地址: https://ju.6miu.com/read-968772.html