这篇文章我想来记录一下自己学习android开发中关于ListView布局的介绍以及使用,方便自己以后的翻阅和学习。
对,很简单,这里我把ListView的ID设置为list_view,将宽度和高度都设置为match_parent,让ListView占满整个布局空间
这是一个简单的ListView的Activity,在这个Activity里面需要准备ListView展示的数据,可以是来自数据库等任何地方,这里的数据我就直接简单构造,然后适配器是ArrayAdapter,并指定它的泛型为String,在其构造器中依次传入当前上下文、ListView子项布局的id,以及要适配的数据。
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class StringListViewActivity extends AppCompatActivity {
private String[] data = {"Apple", "Balana", "Orange", "Watermelon", "pear"
, "Grape", "Pineapple", "Strawberry", "Cherry", "Mango", "Pineapple", "Strawbery"
, "Cherry", "Mango"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_stringlistview);
//构造适配器,android.R.layout.simple_list_item_1是Android内置的布局文件,其实就是一个TextView
ArrayAdapter adapter = new ArrayAdapter(StringListView.this, android.R.layout.simple_list_item_1, data);
ListView listView = (ListView) findViewById(R.id.list_view);
//装载适配器
listView.setAdapter(adapter);
}
}
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import java.util.*;
public class SimpleListViewActivity extends AppCompatActivity {
private String[] data = {"Apple", "Balana", "Orange", "Watermelon", "pear"
, "Grape", "Pineapple", "Strawberry", "Cherry", "Mango", "Pineapple", "Strawbery"
, "Cherry", "Mango"};
private String[] creation = {"created in China", "create in America", "create in England", "create in Russia"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_stringlistview);
//构造数据
ArrayList
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;
}
}
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.List;
/**
* Created by apple on 2017/5/1.
*/
public class FruitAdapter extends ArrayAdapter{
private int resourceId;
public FruitAdapter(Context context, int textViewResourceId, List objects){
super(context,textViewResourceId, objects);
resourceId = textViewResourceId;
}
//在每个子项被滚动到屏幕内被调用
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull 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;
// 提高效率
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 = (TextView) 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;
}
}
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private List 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();
}
});
}
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);
}
}
}
《第一行代码——Android》 郭霖