ListView的常用方式
方式一:加载简单的文本
注意:ListView也是一个控件,所以像button一样加载即可,它不能直接接受数组数据,而是要通过一个适配器Adapter,常用ArrayAdapter
1 public class MainActivity extends Activity { 2 3 @Override 4 protected void onCreate(Bundle savedInstanceState) { 5 super.onCreate(savedInstanceState); 6 setContentView(R.layout.activity_main); 7 8 ListView listView = (ListView) findViewById(R.id.list_view); 9 String[] data = {"1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16","17","18"}; 10 ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, data); 11 listView.setAdapter(adapter); 12 } 13 14 }
activity_main.xml
1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 android:layout_width="match_parent" 3 android:layout_height="match_parent" > 4 5 <ListView 6 android:id="@+id/list_view" 7 android:layout_width="match_parent" 8 android:layout_height="match_parent" > 9 </ListView> 10 11 </LinearLayout>
方式二:自定义承载项,可以加图片文字。主要关注Adapter的写法!
MainActivity
1 public class MainActivity extends Activity { 2 3 @Override 4 protected void onCreate(Bundle savedInstanceState) { 5 super.onCreate(savedInstanceState); 6 requestWindowFeature(Window.FEATURE_NO_TITLE); 7 setContentView(R.layout.activity_main); 8 9 ListView listView = (ListView) findViewById(R.id.list_view); 10 List<Item> data = new ArrayList<Item>(); 11 // 简单设置数据项目 12 for(int i=0;i<18;i++){ 13 data.add(new Item(R.drawable.ic_launcher, i+"")); 14 } 15 MyAdapter adapter = new MyAdapter(this, R.layout.list_view_item, data); 16 listView.setAdapter(adapter); 17 } 18 19 }
Item.java定义图片资源id和文本内容
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:layout_width="match_parent" 4 android:layout_height="wrap_content" 5 android:orientation="horizontal" > 6 7 <ImageView 8 android:id="@+id/item_img" 9 android:layout_width="wrap_content" 10 android:layout_height="wrap_content" /> 11 12 <TextView 13 android:id="@+id/item_text" 14 android:layout_width="0dp" 15 android:layout_height="wrap_content" 16 android:layout_gravity="center_vertical" 17 android:layout_weight="1" /> 18 19 </LinearLayout>
1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 android:layout_width="match_parent" 3 android:layout_height="match_parent" 4 > 5 6 <ListView 7 android:id="@+id/list_view" 8 android:layout_width="match_parent" 9 android:layout_height="match_parent" 10 ></ListView> 11 12 </LinearLayout>
MyAdapter.java自定义适配器,继承自ArrayAdapter
1 public class MyAdapter extends ArrayAdapter<Item> { 2 private int resourceId; 3 public MyAdapter(Context context,int textViewResourceId, 4 List<Item> objects) { 5 super(context,textViewResourceId, objects); 6 // 这里传入承载子项,即R.layout.list_view_item 7 resourceId = textViewResourceId; 8 } 9 // 所有的子项滚入屏幕内都会执行的方法,这里执行加载图片和文本的操作即可 10 @Override 11 public View getView(int position, View convertView, ViewGroup parent) { 12 // 得到Item对象 13 Item item = getItem(position); 14 View view; 15 ViewHolder viewHolder; 16 if(convertView == null){ 17 // 为空才加载布局 18 view = LayoutInflater.from(getContext()).inflate(resourceId, null); 19 viewHolder = new ViewHolder(); 20 // 设置holder标签 21 viewHolder.img = (ImageView) view.findViewById(R.id.item_img); 22 viewHolder.text = (TextView) view.findViewById(R.id.item_text); 23 view.setTag(viewHolder); 24 }else { 25 view = convertView; 26 viewHolder = (ViewHolder) view.getTag(); 27 } 28 29 // 显示图片和文本 30 viewHolder.img.setImageResource(item.getImgSouceId()); 31 viewHolder.text.setText(item.getText()); 32 33 return view; 34 } 35 /** 36 * 定义一个ViewHolder,用来提升ListView的效率,避免大量的解析xml文件,即:findViewById 37 */ 38 private class ViewHolder{ 39 ImageView img; 40 TextView text; 41 } 42 43 }