当前位置: 移动技术网 > IT编程>移动开发>Android > Android UI:ListView - SimpleAdapter实例详解

Android UI:ListView - SimpleAdapter实例详解

2019年07月24日  | 移动技术网IT编程  | 我要评论

鹿鼎记陈小春版演员表,数字家电,清野静流

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;
  }
}

 显示效果

 

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

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网