深入浅出学习Android ListView基础

所属分类: 软件编程 / Android 阅读数: 552
收藏 0 赞 0 分享

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程序设计有所帮助。

更多精彩内容其他人还在看

Retrofit2日志拦截器的使用

这篇文章主要介绍了Retrofit2日志拦截器的使用,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

Android创建外部lib库及自定义View的图文教程

这篇文章主要给大家介绍了关于Android创建外部lib库及自定义View的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

Android分享微信小程序失败的一些事小结

这篇文章主要给大家介绍了关于Android分享微信小程序失败一些事,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

Android分享微信小程序技巧之图片优化

这篇文章主要给大家介绍了关于Android分享微信小程序技巧之图片优化的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

Android Viewpager实现无限循环轮播图

这篇文章主要为大家详细介绍了Android Viewpager实现无限循环轮播图,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android中的Bitmap序列化失败的解决方法

这篇文章主要介绍了Android中的Bitmap序列化失败的解决方法,本文通过实例代码给大家介绍的非常详细,具有一定的参考借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享

Android自定义通用标题栏CustomTitleBar

这篇文章主要为大家详细介绍了Android自定义通用标题栏CustomTitleBar,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android组合控件自定义标题栏

这篇文章主要为大家详细介绍了Android组合控件自定义标题栏,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android自定义复合控件实现通用标题栏

这篇文章主要为大家详细介绍了Android自定义复合控件实现通用标题栏,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

ExpandableListView实现简单二级列表

这篇文章主要为大家详细介绍了ExpandableListView实现简单二级列表,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享
查看更多