首页 > 系统 > Android > 正文

深入浅出学习Android ListView基础

2020-04-11 10:56:51
字体:
来源:转载
供稿:网友

ListView 简介

ListView 是安卓里常用的控件, 本文介绍一下常用用法,以及优化等方法

1、改写activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  android:layout_width="match_parent"  android:layout_height="match_parent">  <ListView    android:id="@+id/listview"    android:layout_width="match_parent"    android:layout_height="match_parent" /></LinearLayout>

2、新建一个javabean

package com.example.tomchen.testlistview;public class Icon {  private int imageId;  private String name;  public Icon(String name, int imageId) {    this.name = name;    this.imageId = imageId;  }  public String getName() {    return name;  }  public int getImageId() {    return imageId;  }}

3、新建icon_item.xml 用于 listview 每个元素的视图

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  android:layout_width="match_parent"  android:layout_height="match_parent"  android:orientation="horizontal">  <TextView    android:id="@+id/text"    android:layout_width="wrap_content"    android:layout_height="match_parent" />  <ImageView    android:id="@+id/icon"    android:layout_width="wrap_content"    android:layout_height="match_parent" /></LinearLayout>

4、新建一个class IconAdapter 继承自 ArrayAdapter

package com.example.tomchen.testlistview;import android.content.Context;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;public class IconAdapter extends ArrayAdapter<Icon> {  private int resourceId;  public IconAdapter(Context context, int resource, List<Icon> icons) {    super(context, resource, icons);    this.resourceId = resource;  }  @Override  public View getView(int position, View convertView, ViewGroup parent) {    Icon icon = getItem(position);    View view;    if (convertView == null) {      view = LayoutInflater.from(getContext()).inflate(resourceId, null);    } else {      view = convertView;    }    ImageView image = (ImageView) view.findViewById(R.id.icon);    image.setImageResource(icon.getImageId());    TextView text = (TextView) view.findViewById(R.id.text);    text.setText(icon.getName());    return view;  }}

Note
这儿有了一个优化, convertView 将之前的布局进行缓存, 以便之后进行重用.

所以我们判断convertView是否为空. 空的话就使用LayoutInflater去加载布局, 否则的话就直接复用convertView

测试listview

MainActivity里面加入下面代码

package com.example.tomchen.testlistview;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.view.Menu;import android.view.MenuItem;import android.widget.ListView;import java.util.ArrayList;import java.util.List;public class MainActivity extends AppCompatActivity {  private ListView listView;  private List<Icon> icons = new ArrayList<Icon>();  @Override  protected void onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentView(R.layout.activity_main);    listView = (ListView) findViewById(R.id.listview);    icons.add(new Icon("Icon_01", R.drawable.ic_01));    icons.add(new Icon("Icon_02", R.drawable.ic_02));    icons.add(new Icon("Icon_03", R.drawable.ic_03));    icons.add(new Icon("Icon_04", R.drawable.ic_04));    icons.add(new Icon("Icon_05", R.drawable.ic_05));    icons.add(new Icon("Icon_06", R.drawable.ic_06));    icons.add(new Icon("Icon_07", R.drawable.ic_07));    icons.add(new Icon("Icon_08", R.drawable.ic_08));    icons.add(new Icon("Icon_09", R.drawable.ic_09));    IconAdapter adapter = new IconAdapter(this, R.layout.icon_item, icons);    listView.setAdapter(adapter);  }}

效果图如下:

以上就是本文的全部内容,希望对大家的学习Android程序设计有所帮助。

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表