欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android UI:ListView - SimpleAdapter实例详解

程序员文章站 2024-02-28 09:12:40
android ui:listview -- simpleadapter simpleadapter是扩展性最好的适配器,可以定义各种你想要的布局,而且使用很方便。...

android ui:listview -- simpleadapter

simpleadapter是扩展性最好的适配器,可以定义各种你想要的布局,而且使用很方便。

layout :

<?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">
    <listview
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:divider="#7f00"    //分割线
      android:dividerheight="2dp"
      android:id="@+id/listview_sample"/>
</linearlayout>

header layout:

<?xml version="1.0" encoding="utf-8"?>
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<imageview
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:src="@mipmap/ic_launcher"/>
</linearlayout>

自定义布局  item:

<?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">
  <imageview
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_margin="3px"
    android:id="@+id/img"/>
  <linearlayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">
    <textview
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:textsize="16sp"
      android:id="@+id/title"/>
    <textview
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:id="@+id/info"
      android:textsize="16sp"/>
  </linearlayout>

</linearlayout>

java 代码:

public class sampleadapteractivity extends activity {

  private listview mlistview;
  @override
  protected void oncreate(bundle savedinstancestate) {
    super.oncreate(savedinstancestate);
    setcontentview(r.layout.sampleadapter_layout);
    mlistview = (listview) findviewbyid(r.id.listview_sample);
    simpleadapter adapter = new simpleadapter(this,
        getdata(), //数据来源
        r.layout.item_listview, //对应item view
        new string[]{"img","title","info"}, //data 中对应值
        new int[]{r.id.img,r.id.title,r.id.info}); //填充layout位置
    mlistview.setheaderdividersenabled(true);   //是否显示头view 的分割线
    view header = view.inflate(this,r.layout.listview_header,null);
    view footer = view.inflate(this,r.layout.listview_header,null);
    mlistview.addheaderview(header);  //添加头部view
    mlistview.addfooterview(footer);   //添加底部view
    mlistview.setadapter(adapter);
  }

  @override
  protected void onresume() {
    super.onresume();
  }
  private list<? extends map<string,?>> getdata() {
    list<map<string,object>> items = new arraylist<map<string, object>>();
    for (int i = 0; i < 5; i++) {
      map<string,object> item = new hashmap<string,object>();
      item.put("img",r.mipmap.ic_launcher);
      item.put("title","title -- " + i );
      item.put("info","info -- " + i );
      items.add(item);
    }
    return items;
  }
}

 显示效果

 Android UI:ListView - SimpleAdapter实例详解

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!