ListView
Android 第一行代码 第二版 第三章ListView
- activity_main.xml文件中写入listView
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
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="wrap_content">
</ListView>
</LinearLayout>
- 定义一个实体类Fruit
public class Fruit {
private String name; //水果名称
private int imageId; //图像id
public Fruit(String name, int imageId) {
this.name = name;
this.imageId = imageId;
}
public String getName() {
return name;
}
public void setImageId(int imageId) {
this.imageId = imageId;
}
}
- 指定子项的自定义布局fruit_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="match_parent">
<ImageView
android:id="@+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginLeft="10dp"/>
<!--文字水平方向上居中显示,距离左侧10dp-->
</LinearLayout>
- 自定义适配器FruitAdapter继承自ArrayAdapter
- 重写构造函数,分别传递上下文,子项布局id和数据
- 重写getView()方法
- 利用convertView来提升运行效率
- 创建ViewHolder类,优化findViewByID()
- 利用getTag(),setTag()保存ViewHolder
public class FruitAdapter extends ArrayAdapter<Fruit> {
private int resourceId;
//1.构造函数重写
public FruitAdapter(Context context, int resource, List<Fruit> objects) {
super(context, resource, objects);
//上下文,子项布局id,数据
resourceId = resource;
}
//ctrl + O重写getView方法,每个子项被滚动到屏幕内调用
//position第几个子项,convertView将之前加载好的布局进行缓存
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
View view;
Fruit fruit = getItem(position); // 获取到当前Fruit实例
ViewHolder viewHolder;
//优化listView的运行效率
if(convertView !=null){
view = convertView;
viewHolder = (ViewHolder) view.getTag();
} else {
//resourceId将要加载的layout布局文件;parent层次结构的父类;false标准写法
view = LayoutInflater.from(getContext()).inflate(resourceId, parent, false);
viewHolder = new ViewHolder();
viewHolder.imageView = (ImageView) view.findViewById(R.id.image);
viewHolder.textView = (TextView) view.findViewById(R.id.text);
view.setTag(viewHolder); //将viewHolder保存到view中
}
viewHolder.imageView.setImageResource(fruit.getImageId()); //设置image图像
viewHolder.textView.setText(fruit.getName());
return view;
}
class ViewHolder{
ImageView imageView;
TextView textView;
}
}
- 修改MainActivity中的方法
- 初始化adapter
- 获取listView
- 装载adapter
- 设置item点击事件
public class MainActivity extends AppCompatActivity {
private List<Fruit> fruitList = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//隐藏标题栏
ActionBar actionBar = getSupportActionBar();
if(actionBar!=null) {
actionBar.hide();
}
initFruit(); // 初始化fruitList
//1.初始化adpater
FruitAdapter fruitAdapter = new FruitAdapter(MainActivity.this,R.layout.fruit_item,
fruitList);
//2.获取listView
ListView listView = (ListView) findViewById(R.id.list_view);
//3.装载adapter
listView.setAdapter(fruitAdapter);
//设置item项点击事件
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Fruit fruit = fruitList.get(position);
Toast.makeText(MainActivity.this,fruit.getName(),Toast.LENGTH_LONG).show();
}
});
}
private void initFruit() {
//利用循环将数据传入2次,以实现ListView将屏幕充满
for( int i = 0 ; i < 2 ; i++){
Fruit apple = new Fruit("apple",R.drawable.apple_pic); //利用构造函数定义apple对象
fruitList.add(apple); //装载apple到fruitList
Fruit banana = new Fruit("banana",R.drawable.banana_pic);
fruitList.add(banana);
Fruit cherry = new Fruit("cherry",R.drawable.cherry_pic);
fruitList.add(cherry);
Fruit grape = new Fruit("grape",R.drawable.grape_pic);
fruitList.add(grape);
Fruit mango = new Fruit("mango",R.drawable.mango_pic);
fruitList.add(mango);
Fruit orange = new Fruit("orange",R.drawable.orange_pic);
fruitList.add(orange);
Fruit pear = new Fruit("pear",R.drawable.pear_pic);
fruitList.add(pear);
Fruit pineapple = new Fruit("pineapple",R.drawable.pineapple_pic);
fruitList.add(pineapple);
Fruit strawberry = new Fruit("strawberry",R.drawable.strawberry_pic);
fruitList.add(strawberry);
Fruit watermelon = new Fruit("watermelon",R.drawable.watermelon_pic);
fruitList.add(watermelon);
}
}
}