ListView
1.ListView允许用户通过手指上下滑动的方式将屏幕外的数据滚动到屏幕内,同时屏幕上原有的数据会滚动出屏幕
2.在布局中加入ListView控件
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 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>
3.适配
ListView是用来展示大量数据的,这些数据可以从网上下载,也可以从数据库中读取。这里我们用data数组来测试。
数组中的数据无法直接传递给ListView,需要借助适配器,这里使用ArrayAdapter,他可以通过泛型来指定要适配的数据类型,然后在构造函数中把要适配的数据传入。
最后调用ListViewdsetAdapter()方法,将构造好的适配器对象传递过去,建立ListView和数据之间的关系。
public class MainActivity extends AppCompatActivity { 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.activity_main); ArrayAdapter<String> adapter = new ArrayAdapter<String>(MainActivity.this,android.R.layout.simple_list_item_1, data); ListView listView = (ListView) findViewById(R.id.list_view); listView.setAdapter(adapter); } }
4.定制ListView的界面
定制一个实体类作为ListView适配器的适配类型
public class MainActivity extends AppCompatActivity { 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.activity_main); ArrayAdapter<String> adapter = new ArrayAdapter<String>(MainActivity.this,android.R.layout.simple_list_item_1, data); ListView listView = (ListView) findViewById(R.id.list_view); listView.setAdapter(adapter); } }
5.为ListViewd子项指定一个自定义布局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="wrap_content" android:layout_height="wrap_content"> <ImageView android:id="@+id/fruit_image" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:id="@+id/fruit_name" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:layout_marginLeft="10dp"/> </LinearLayout>
6.创建自定义适配器,并将泛型指定为Fruit类
public class FruitAdapter extends ArrayAdapter<Fruit>{ private int resourceId; public FruitAdapter(Context context, int textViewResourceId, List<Fruit> objects){ super (context,textViewResourceId,objects); resourceId = textViewResourceId; } //重写了父类的构造函数,用于将上下文、ListView子项布局的id和数据都穿过来 @NonNull @Override public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) { //该方法使每个子项在滚动到屏幕内时被调用 Fruit fruit = getItem(position); //获取当前项的Fruit实例 View view = LayoutInflater.from(getContext()).inflate(resourceId,parent,false); //为子项加载我们传入的布局 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; } }
7.定制界面
public class MainActivity extends AppCompatActivity { private List<Fruit> 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++){//for循环将所有数据填两变,一遍不足以充满整个屏幕 Fruit apple = new Fruit("Apple",R.drawable.apple);//在构造函数中将水果的名字和对应的图片id传入 fruitList.add(apple); //把创建好的对象添加到水果列表中 Fruit banana = new Fruit("Banana",R.drawable.banana); fruitList.add(banana); Fruit orange = new Fruit("Orange",R.drawable.orange); fruitList.add(orange); Fruit watermelon = new Fruit("Watermelon",R.drawable.watermelon); fruitList.add(watermelon); Fruit pear = new Fruit("Pear",R.drawable.pear); fruitList.add(pear); Fruit grape = new Fruit("Grape",R.drawable.grape); fruitList.add(grape); Fruit pineapple = new Fruit("Pineapple",R.drawable.pineapple); fruitList.add(pineapple); Fruit strawberry = new Fruit("Strawberry",R.drawable.strawberry); fruitList.add(strawberry); Fruit cherry = new Fruit("Cherry",R.drawable.cherry); fruitList.add(cherry); Fruit mango = new Fruit("Mango",R.drawable.mango); fruitList.add(mango); } } }
8.提升ListView的运行效率
(1)在getView()方法中进行了判断,如果convertView为null,则使用LayoutInflater去加载布局,如果不为null则直接对convertView进行重用
public class FruitAdapter extends ArrayAdapter<Fruit>{zeprivate int resourceId; public FruitAdapter(Context context, int textViewResourceId, List<Fruit> objects){ super (context,textViewResourceId,objects); resourceId = textViewResourceId; } //重写了父类的构造函数,用于将上下文、ListView子项布局的id和数据都穿过来 @NonNull @Override public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) { //该方法使每个子项在滚动到屏幕内时被调用 Fruit fruit = getItem(position); //获取当前项的Fruit实例 View view; if (convertView == null){ view = LayoutInflater.from(getContext()).inflate(resourceId,parent,false); }else { view = convertView; } 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; } }
(2)借助viewHolder来进行优化
public class FruitAdapter extends ArrayAdapter<Fruit>{ private int resourceId; public FruitAdapter(Context context, int textViewResourceId, List<Fruit> objects){ super (context,textViewResourceId,objects); resourceId = textViewResourceId; } //重写了父类的构造函数,用于将上下文、ListView子项布局的id和数据都穿过来 @NonNull @Override public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) { //该方法使每个子项在滚动到屏幕内时被调用 Fruit fruit = getItem(position); //获取当前项的Fruit实例 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 = (TextView) view.findViewById(R.id.fruit_name); view.setTag(viewHolder);//将ViewHolder储存在View中 }else { view = convertView; viewHolder = (ViewHolder) view.getTag();//重新获取ViewHolder } viewHolder.fruitImage.setImageResource(fruit.getImageId()); viewHolder.fruitName.setText(fruit.getName()); return view; } class ViewHolder{ //内部类,用于对控件的实例进行缓存 ImageView fruitImage; TextView fruitName; } }
9.ListView的点击事件
public class MainActivity extends AppCompatActivity { private List<Fruit> 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); 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(); } }); }