Android UI:ListView - SimpleAdapter实例详解

 更新时间:2016年11月26日 16:58:03   投稿:lqh  
这篇文章主要介绍了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实用的代码片段 常用代码总结

    Android实用的代码片段 常用代码总结

    这篇文章主要介绍了Android实用的代码片段 常用代码总结,需要的朋友可以参考下
    2014-09-09
  • Android实现数据按照时间排序

    Android实现数据按照时间排序

    这篇文章主要为大家详细介绍了Android实现数据按照时间排序的方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2018-09-09
  • Android利用Espresso进行UI自动化测试的方法详解

    Android利用Espresso进行UI自动化测试的方法详解

    因为我是搞android开发的,所以被分到了自动化测试小组,所以了解了一些UI自动化测试。下面这篇文章主要给大家介绍了关于Android利用Espresso进行UI自动化测试的相关资料,需要的朋友可以参考借鉴,下面来一起看看吧。
    2017-12-12
  • Android开发学习实现简单计算器

    Android开发学习实现简单计算器

    这篇文章主要为大家详细介绍了Android实现一个简单计算器,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2020-04-04
  • Android 实现自己的LOG信息

    Android 实现自己的LOG信息

    本文主要讲解Android LOG,这里对如何创建自己的Android LOG信息做了详细的介绍,并附简单代码示例,有需要的小伙伴可以参考下
    2016-08-08
  • Android自定义加载框效果

    Android自定义加载框效果

    这篇文章主要为大家详细介绍了Android自定义加载框效果,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2021-08-08
  • Android实现边录边播功能

    Android实现边录边播功能

    这篇文章主要为大家详细介绍了Android实现边录边播功能,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2021-11-11
  • Android自定义View实现随机数验证码

    Android自定义View实现随机数验证码

    这篇文章主要为大家详细介绍了Android如何利用自定义View实现随机数验证码效果,文中的示例代码讲解详细,感兴趣的小伙伴可以了解一下
    2022-06-06
  • Android 安全加密:消息摘要Message Digest详解

    Android 安全加密:消息摘要Message Digest详解

    本文主要介绍Android安全加密消息摘要Message Digest,这里整理了详细的资料,并说明如何使用Message Digest 和使用注意事项,有需要的小伙伴可以参考下
    2016-09-09
  • Android开发中Listview动态加载数据的方法示例

    Android开发中Listview动态加载数据的方法示例

    这篇文章主要介绍了Android开发中Listview动态加载数据的方法,结合实例形式较为详细的分析了Android操作ListView界面布局与数据动态更新相关操作技巧,需要的朋友可以参考下
    2017-10-10

最新评论