UI控件之ListView

时间:2024-05-27 19:34:20

一,一个简单的TextView列表

public class FirstActivity extends Activity {
private String[] data = {"Apple", "Banana", "Orange", "Watermelon",
"Pear", "Grape", "Pineapple", "Strawberry", "Cherry", "Mango","Apple", "Banana", "Orange", "Watermelon","Pear", "Grape", "Pineapple", "Strawberry", "Cherry", "Mango"}; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.first_layout);
ArrayAdapter<String> adapter = new ArrayAdapter<String>( //ArrayAdapter数组适配器
FirstActivity.this, android.R.layout.simple_list_item_1, data);
//第一个参数,上下文对象;第二个参数,一个官方的内置布局的id(这是一个TexView);第三个参数,传入的数据
ListView listView = (ListView) findViewById(R.id.ListView);
listView.setAdapter(adapter);
}
}

二,好玩的东西怎么可以只有Text View呢(来点复杂的)

1.新建一个用于存放水果的类

public class Fruit {
private String name;
private int imageId; //构造器初始化
public Fruit(String name, int imageId) {
this.name = name;
this.imageId = imageId;
} public String getName() {
return name;
} public int getImageId() {
return imageId;
}
}

2.新建一个自定义的布局文件

用于描述布局方式,等待被调用

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"> <ImageView
android:id="@+id/fruit_image"
android:layout_width="50dp"
android:layout_height="50dp" /> <TextView
android:id="@+id/fruit_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>

3.自定义适配器,继承于ArrayAdapter,并重写getView方法

public class FruitAdapter extends ArrayAdapter<Fruit> {      //泛型指定为Fruit类,就像String也是一个类
private int resourceId;
//新建FruitAdapter()方法,参数一:上下文对象;参数二:布局文件的ID;参数三:传入的数据
public FruitAdapter(Context context, int textViewResourceId, List<Fruit> objects){
super(context,textViewResourceId,objects);
resourceId = textViewResourceId;
} @NonNull
@Override
//重写getView() 方法,这个方法在内容被加载之前会执行
public View getView(int position, View convertView, ViewGroup parent) {
Fruit fruit = getItem(position);//得到方法传入的Fruit实例
View view = LayoutInflater.from(getContext()).inflate(resourceId,null);//把构造器获取的id值传入view /*ListView的赋值*/
ImageView fruitImage = (ImageView) view.findViewById(R.id.fruit_image);
TextView fruitName = (TextView) view.findViewById(R.id.fruit_name);
fruitImage.setImageResource(fruit.getImageId());
fruitName.setText(fruit.getName());
return view;
}
}

注:在setText的时候不能用int类型数据。

4.在Activity引用的layout中添加<ListView>

    <ListView
android:id="@+id/ListView"
android:layout_width="match_parent"
android:layout_height="wrap_content"> </ListView>

5.在Activity中初始化数据并引用ListView

public class FirstActivity extends Activity {
private List<Fruit> fruitList = new ArrayList<Fruit>(); //创建数组适配器的对象fruitList,范型为<Fruit类>
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.first_layout);
initFruits(); //调用数据初始化函数
FruitAdapter adapter = new FruitAdapter(FirstActivity.this,R.layout.fruit_item,fruitList);
//对应的三个参数,上下文,id,适配器数据 /*对ListView设置自定义适配器的值*/
ListView listView = (ListView) findViewById(R.id.ListView);
listView.setAdapter(adapter);
} /*列表参数的值的传入和列表的添加*/
private void initFruits(){
Fruit apple = new Fruit("Apple",R.drawable.apple);
fruitList.add(apple);//一定记得要把对象添加进列表中
Fruit banana = new Fruit("banana",R.drawable.banane);
fruitList.add(banana);
Fruit grape = new Fruit("grape",R.drawable.grape);
fruitList.add(grape);
Fruit pear = new Fruit("pear",R.drawable.pear);
fruitList.add(pear);
Fruit watermelon = new Fruit("watermelon",R.drawable.watermelon);
fruitList.add(watermelon);
Fruit orange = new Fruit("orange",R.drawable.orange);
fruitList.add(orange);
}
}

二,ListView之优化

1:控件重用,减少重复加载

public View getView(int position, View convertView, ViewGroup parent) {
Music music = getItem(position);
View view;
if (convertView == null) {
//对应getView中的参数coverView,一个加载缓存布局的参数。如果没有加载好的布局,则使用LayoutInflater进行布局加载
view = LayoutInflater.from(getContext()).inflate(ResourceId, null);
}else {
view = convertView;//存在布局,则重载布局
}

2:虽然解决了加载次数,但仍旧每次加载都findViewById

ViewHolder在As加载中出现问题,暂未解决,后续更新优化问题,再研究研究。

三,添加点击事件,为ListView 添加setOnItemClickListener

listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
MainList APP = mainList.get(position);//确定点击位置,返回值为点击类的的对象
if (APP.getImageId() == R.drawable.list_view_icon) {
Intent intent = new Intent(MainActivity.this,MyListView.class);
startActivity(intent);
}
}
});