创建一个新工程。要使用自定义ListView,需要自定义一个ListView item 的布局xml,还需要自定义一个Adapter。
- 先在activity_main.xml中创建一个listview:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:id="@+id/list_view"
android:layout_width="match_parent"
android:layout_height="match_parent"></ListView>
</LinearLayout>
这里让listview全部充满整个屏幕。
- 自定义listview item的布局文件。新建一个fruit_item.xml布局文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/fruit_image"
android:layout_width="50dp"
android:layout_height="50dp"/>
<TextView
android:id="@+id/furit_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:textSize="15sp"
android:layout_marginLeft="10dp"/>
</LinearLayout>
这里只用来显示一张图片和一个textview文字说明.
3.先声明一个Fruit类:
package com.example.listview;
/**
* Created by archerlj on 2017/9/17.
*/
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;
}
}
- 自定义适配器,新建一个FruitAdapter类,继承自ArrayAdapter,泛型类型穿入Fruit:
package com.example.listview;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.List;
/**
* Created by archerlj on 2017/9/17.
*/
public class FruitAdapter extends ArrayAdapter<Fruit> {
private int resourceId;
public FruitAdapter(Context context, int resource, List<Fruit> objects) {
super(context, resource, objects);
this.resourceId = resource;
}
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
View view;
ViewHolder viewHolder;
if (convertView == null) {
view = LayoutInflater.from(getContext()).inflate(resourceId, parent, false);
viewHolder = new ViewHolder();
viewHolder.fruitImage = view.findViewById(R.id.fruit_image);
viewHolder.fruitName = view.findViewById(R.id.furit_name);
view.setTag(viewHolder);
} else {
view = convertView;
viewHolder = (ViewHolder) view.getTag();
}
Fruit fruit = getItem(position);
viewHolder.fruitImage.setImageResource(fruit.getImageId());
viewHolder.fruitName.setText(fruit.getName());
return view;
}
class ViewHolder {
ImageView fruitImage;
TextView fruitName;
}
}
这里添加一个属性变量resourceId用来存储初始化方法中穿入的listview item的布局文件的id。添加一个初始化方法。重写getView方法。这里为了优化listview的性能,添加了一个ViewHolder内部类,用来保存listview item上的控件,这样就不需要每次都通过findviewbyid去获取了,并通过setTag方法把viewholder赋值给每个listview item的view。这里convertView缓存了listview item的view。如果缓存为空,我们再根据resourceId去创建,否则就直接使用即可。
android中的listview 的内部优化机制同iOS中的UITableView对cell的优化机制是一样的,都使用了cell重用机制。
- 在MainActivity中获取listview,并设置适配器:
package com.example.listview;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private List<Fruit> fruits = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initFruits();
FruitAdapter adapter = new FruitAdapter(this, R.layout.fruit_item, fruits);
ListView listView = (ListView) findViewById(R.id.list_view);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
Fruit fruit = fruits.get(position);
Toast.makeText(MainActivity.this, fruit.getName(), Toast.LENGTH_SHORT).show();
}
});
}
private void initFruits() {
for (int i = 0; i < 2; i++) {
Fruit apple = new Fruit("Apple", R.mipmap.ic_launcher);
fruits.add(apple);
Fruit apple1 = new Fruit("Apple", R.mipmap.ic_launcher);
fruits.add(apple1);
Fruit apple2 = new Fruit("Apple", R.mipmap.ic_launcher);
fruits.add(apple2);
Fruit apple3 = new Fruit("Apple", R.mipmap.ic_launcher);
fruits.add(apple3);
Fruit apple4 = new Fruit("Apple", R.mipmap.ic_launcher);
fruits.add(apple4);
Fruit apple5 = new Fruit("Apple", R.mipmap.ic_launcher);
fruits.add(apple5);
Fruit apple6 = new Fruit("Apple", R.mipmap.ic_launcher);
fruits.add(apple6);
}
}
}
运行:
Screenshot_2017-09-17-14-46-42-886_ListView.png这样,一个简单的自定义ListView item 就实现了。