枕上从妨一夜睡,灯前读尽十年诗。这篇文章主要讲述Android学习——控件ListView的使用相关的知识,希望能为你提供帮助。
一、ListView的简单用法
首先新建一个ListViewTest项目,并让android Studio自动创建好活动。然后修改activity_main.xml中的代码,如下:
1 < ?xml version="1.0" encoding="utf-8"?> 2 < LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3android:layout_width="match_parent" 4android:layout_height="match_parent"> 5 6< ListView 7android:id="@+id/list_view" 8android:layout_width="match_parent" 9android:layout_height="match_parent"> 10< /ListView> 11 < /LinearLayout>
【Android学习——控件ListView的使用】接下来修改MainActivity中的代码:
1 public class MainActivity extends AppCompatActivity { 2 3private String[] data=https://www.songbingjia.com/android/{"Apple","Banana","Orange","Watermelon","Pear","Grape","Pineapple","Strawberry","Cherry","Mango","Apple","Banana","Orange","Watermelon","Pear","Grape","Pineapple","Strawberry","Cherry","Mango"}; 4@Override 5protected void onCreate(Bundle savedInstanceState) { 6super.onCreate(savedInstanceState); 7setContentView(R.layout.activity_main); 8 9ArrayAdapter< String> adapter=new ArrayAdapter< String> (MainActivity.this,android.R.layout.simple_list_item_1,data); 10ListView listview=(ListView)findViewById(R.id.list_view); 11listview.setAdapter(adapter); 12} 13 }
数组中的数据无法直接传递给ListView,需要借助适配器来实现。
ArrayAdapter的构造函数中依次传入当前上下文,ListView子项布局的id,以及要适配的数据;
调用ListView的setAdapter()方法,将构建好的适配器对象传递进去,这样ListView和数据之间的关联就建立完成。
运行程序,可以通过滚动的方式来查看屏幕外的数据。
文章图片
二、定制ListView的界面
首先准备一组图片,分别对应上面提供的每一种水果(注意图片大小尽量一致),放在drawable目录下,注意命名不能出现大写字母(比如Apple不合法);
在com.example.administrator.listviewtest下新建.java文件,定义一个实体类Fruit,作为ListView适配器的适配类型。
1 public class Fruit{ 2private String name; 3private int imageId; 4public Fruit(String name,int imageId){ 5this.name=name; 6this.imageId=imageId; 7} 8public String getName(){ 9return name; 10} 11public int getImageId(){ 12return imageId; 13} 14 }
然后为ListView的子项指定一个自定义布局,在layout目录下新建fruit_item.xml。
1 < ?xml version="1.0" encoding="utf-8"?> 2 < LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3android:layout_width="match_parent" 4android:layout_height="wrap_content"> 5 6< ImageView 7android:id="@+id/fruit_image" 8android:layout_width="wrap_content" 9android:layout_height="wrap_content" /> 10 11< TextView 12android:id="@+id/fruit_name" 13android:layout_width="wrap_content" 14android:layout_height="wrap_content" 15android:layout_gravity="center_vertical" 16android:layout_marginLeft="10dp" 17/> 18 < /LinearLayout>
接下来创建一个自定义的适配器,同样在com.example.administrator.listviewtest下新建.java文件,重写了父类的一组构造函数,用于将上下文、ListView子项布局的id和数据都传递进来,命名为FruitAdapter,代码如下:
1 public class FruitAdapter extends ArrayAdapter< Fruit> { 2private int resourceId; 3public FruitAdapter(Context context, int textViewResourceId, List< Fruit> objects){ 4super(context,textViewResourceId,objects); 5resourceId=textViewResourceId; 6} 7@Override 8public View getView(int position,View convertView,ViewGroup parent){ 9Fruit fruit=getItem(position); //获取当前项的实例 10View view= LayoutInflater.from(getContext()).inflate(resourceId,parent,false); 11ImageView fruitImage=(ImageView)view.findViewById(R.id.fruit_image); 12TextView fruitName=(TextView) view.findViewById(R.id.fruit_name); 13fruitImage.setImageResource(fruit.getImageId()); 14fruitName.setText(fruit.getName()); 15return view; 16} 17 }
下面修改MainActivity中的代码:
1 package com.example.administrator.listviewtest; 2 3 import android.support.v7.app.AppCompatActivity; 4 import android.os.Bundle; 5 import android.widget.ArrayAdapter; 6 import android.widget.ListView; 7 8 import java.util.ArrayList; 9 import java.util.List; 10 11 public class MainActivity extends AppCompatActivity { 12private List< Fruit> fruitList=new ArrayList< > (); 13 14 //private String[] data=https://www.songbingjia.com/android/{"Apple","Banana","Orange","Watermelon","Pear","Grape","Pineapple","Strawberry","Cherry","Mango","Apple","Banana","Orange","Watermelon","Pear","Grape","Pineapple","Strawberry","Cherry","Mango"}; 15@Override 16protected void onCreate(Bundle savedInstanceState) { 17super.onCreate(savedInstanceState); 18setContentView(R.layout.activity_main); 19initFruits(); //初始化水果数据 20FruitAdapter adapter=new FruitAdapter(MainActivity.this,R.layout.fruit_item,fruitList); 21 22 //ArrayAdapter< String> adapter = new ArrayAdapter< String> (MainActivity.this, android.R.layout.simple_list_item_1, data); 23ListView listview = (ListView) findViewById(R.id.list_view); 24listview.setAdapter(adapter); 25} 26 27private void initFruits(){ 28for(int i=0; i< 2; i++){ 29Fruit apple=new Fruit("Apple",R.drawable.apple); 30fruitList.add(apple); 31Fruit orange=new Fruit("Orange",R.drawable.orange); 32fruitList.add(orange); 33Fruit banana=new Fruit("Banana",R.drawable.banana); 34fruitList.add(banana); 35Fruit waterlenmo=new Fruit("Waterlemon",R.drawable.waterlemon); 36fruitList.add(waterlenmo); 37Fruit pear=new Fruit("Pear",R.drawable.pear); 38fruitList.add(pear); 39Fruit grape=new Fruit("Grape",R.drawable.grape); 40fruitList.add(grape); 41Fruit pineapple=new Fruit("Pineapple",R.drawable.pineapple); 42fruitList.add(pineapple); 43Fruit strawberry=new Fruit("Strawberry",R.drawable.straw); 44fruitList.add(strawberry); 45Fruit cherry=new Fruit("Cherry",R.drawable.cherry); 46fruitList.add(cherry); 47Fruit mango=new Fruit("mango",R.drawable.mango); 48fruitList.add(mango); 49} 50} 51 }
运行程序后,效果图如下(图片大小后来才意识到,懒得换了……):
文章图片
推荐阅读
- android动画具体解释六 XML中定义动画
- 关于Android滑动冲突的解决方法
- android 自己定义水平和圆形progressbar 仅仅定义一些style就能够
- mongodb的cappedCollection集合
- 手把手教你_怎么找android应用的包名和启动activity
- Android 5.0 源代码结构
- 圣熙女鞋APP原型图纸
- 自己动手写Android数据库框架
- aws实例部署flask报错script-timed-out-before-returning-headers-application-py