最常用也最难用的控件——ListView
1.ListView简单用法
//activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="match_parent"
android:layout_width="match_parent">
<ListView
android:id="@+id/list_view"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
将宽度和高度都设置为match_parent,这样ListView就占满整个布局空间
//MainActivity.java
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<> (MainActivity.this,android.R.layout.simple_list_item_1,data);
ListView listview = (ListView)findViewById(R.id.list_view);
listview.setAdapter(adapter);
}
}
适配器:ArrayAdapter,通过泛型来指定要适配的数据类型,然后在构造函数中把要适配的数据传入,
如上图代码所示,我们选择的构造函数需要传入三个参数,第一个为上下文,第二个为ListView的子项布局id,这是一个Android内置的布局文件,里面只有一个textview,可用于简单的显示一段文本,第三个参数即是数据
然后获取我们的ListView实例,再调用setAdapter()方法,将构建好的适配器传入
2. 定制ListView界面
1.首先定义一个实类,其实一个实类就是listview中的一个项,如图
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;
}
}
2. 在1中我们知道构造适配器时需要传入一个listview的子布局,因为1中数据都是字符串,所以不需要我们来定义子布局,而现在我们的数据是一张图片加一段字符串,所以我们得自己写一个子布局。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<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>
3. 定义新的适配器
public class FruitAdapter extends ArrayAdapter<Fruit> {
private int resourceId;
public FruitAdapter(Context context, int textViewResourceId, List<Fruit> objects){
super(context,textViewResourceId,objects);
resourceId = textViewResourceId;
}
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 = (TextView)view.findViewById(R.id.fruit_name);
fruitImage.setImageResource(fruit.getImageId());
fruitName.setText(fruit.getName());
return view;
}
}
FruitAdapter重写了父类的一组构造函数,用于将上下文、ListView子项布局的id和数据
都传递进来。另外又重写了getView()方法,这个方法在每个子项被滚动到屏幕内的时候会被调
。在getview()方法中,首先通过getItem()方法得到当前项的Fruit实例,然后使用
LayoutInflater来为这个子项加载我们传入的布局。
这里LayoutInflater的inflate()方法接收3个参数,前两个参数我们已经知道是什么。
意思了,第三个参数指定成false,表示只让我们在父布局中声明的layout属性生效,但不会
为这个View添加父布局,因为一旦View有了父布局之后,它就不能再添加到ListView中了。
4. 使用
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++){
Fruit apple = new Fruit("apple",R.mipmap.ic_launcher_round);
fruitlist.add(apple);
Fruit banana = new Fruit("banana",R.mipmap.ic_launcher_round);
fruitlist.add(banana);
Fruit orange = new Fruit("orange",R.mipmap.ic_launcher_round);
fruitlist.add(orange);
Fruit pear = new Fruit("pear",R.mipmap.ic_launcher_round);
fruitlist.add(pear);
Fruit grape = new Fruit("grape",R.mipmap.ic_launcher_round);
fruitlist.add(grape);
Fruit pineapple = new Fruit("pineapple",R.mipmap.ic_launcher_round);
fruitlist.add(pineapple);
Fruit cherry = new Fruit("cherry",R.mipmap.ic_launcher_round);
fruitlist.add(cherry);
Fruit mengo = new Fruit("mengo",R.mipmap.ic_launcher_round);
fruitlist.add(mengo);
}
}
}
这步,和1中差不多。