目录
一、ListView用法
二、定制ListView界面
三、提升ListView的运行效率
四、设置ListView点击事件
一、ListView用法1.新建项目
3.修改MainActivity.java代码
二、定制ListView界面在上面的基础上
4.定义一个实体类Fruit.java,为ListView适配器的适配类型。
5.为ListView的子项指定一个我们自定义的布局。在layout目录下新建fruit_item.xml
6.自定义适配器,继承自ArrayAdapter,并将泛型指定为Fruit类,名为FruitAdapter
7.修改修改MainActivity.java代码,添加intiFruits()方法,来初始化所有水果数据。
一、ListView用法
1.新建项目
2.修改activity——main.xml代码
3.修改MainActivity.java代码
二、定制ListView界面
在上面的基础上
4.定义一个实体类Fruit.java,为ListView适配器的适配类型。
package com.example.listviewtest; 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; } }
5.为ListView的子项指定一个我们自定义的布局。在layout目录下新建fruit_item.xml
6.自定义适配器,继承自ArrayAdapter,并将泛型指定为Fruit类,名为FruitAdapter
public class FruitAdapter extends ArrayAdapter{ private int resourceId; public FruitAdapter(@NonNull Context context, int textViewResourceId, @NonNull List objects) { super(context, textViewResourceId, objects); resourceId = textViewResourceId; } @Override public View getView(int position, View convertView, ViewGroup parent) { Fruit fruit = getItem(position); View view = LayoutInflater.from(getContext()).inflate(resourceId,parent,false); ImageView fruitImage = (ImageView)view.findViewById(R.id.fruit_image); TextView fruitName = view.findViewById(R.id.fruit_name); fruitImage.setImageResource(fruit.getImageId()); fruitName.setText(fruit.getName()); return view; }
7.修改修改MainActivity.java代码,添加intiFruits()方法,来初始化所有水果数据。
public class MainActivity extends AppCompatActivity { private List三、提升ListView的运行效率fruitList = new ArrayList (); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initFruits();//初始化 FruitAdapter adapter = new FruitAdapter(MainActivity.this, R.layout.fruit_item, fruitList); ListView listView = (ListView)findViewById(R.id.list_view); listView.setAdapter(adapter); } private void initFruits() { for (int i = 0; i < 2; i++) { Fruit apple = new Fruit("Apple", R.drawable.apple_pic); fruitList.add(apple); Fruit banana = new Fruit("Banana", R.drawable.banana_pic); fruitList.add(banana); Fruit orange = new Fruit("Orange", R.drawable.orange_pic); fruitList.add(orange); Fruit watermelon = new Fruit("Watermelon", R.drawable.watermelon_pic); fruitList.add(watermelon); Fruit pear = new Fruit("Pear", R.drawable.pear_pic); fruitList.add(pear); Fruit grape = new Fruit("Grape", R.drawable.grape_pic); fruitList.add(grape); 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 cherry = new Fruit("Cherry", R.drawable.cherry_pic); fruitList.add(cherry); Fruit mango = new Fruit("Mango", R.drawable.mango_pic); fruitList.add(mango); } } }
@Override public View getView(int position, View convertView, ViewGroup parent) { Fruit fruit = getItem(position); View view; if(convertView == null){ LayoutInflater.from(getContext()).inflate(resourceId,parent,false); }else{ view = convertView; } ImageView fruitImage = (ImageView)view.findViewById(R.id.fruit_image); TextView fruitName = view.findViewById(R.id.fruit_name); fruitImage.setImageResource(fruit.getImageId()); fruitName.setText(fruit.getName()); return view; }
进一步优化
@Override public View getView(int position, View convertView, ViewGroup parent) { Fruit fruit = getItem(position); View view; ViewHolder viewHolder; if(convertView == null){ view = LayoutInflater.from(getContext()).inflate(resourceId,parent,false); viewHolder = new ViewHolder(); viewHolder.fruitImage = (ImageView) view.findViewById(R.id.fruit_image); viewHolder.fruitName = view.findViewById(R.id.fruit_name); view.setTag(viewHolder); }else{ view = convertView; viewHolder = (ViewHolder)view.getTag(); } viewHolder.fruitImage.setImageResource(fruit.getImageId()); viewHolder.fruitName.setText(fruit.getName()); return view; } class ViewHolder{ ImageView fruitImage; TextView fruitName; }四、设置ListView点击事件
public class MainActivity extends AppCompatActivity { private ListfruitList = new ArrayList (); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initFruits(); // 初始化水果数据 FruitAdapter adapter = new FruitAdapter(MainActivity.this, R.layout.fruit_item, fruitList); ListView listView = (ListView) findViewById(R.id.list_view); listView.setAdapter(adapter); 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_SHORT).show(); } }); }
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)