每次写listview都要翻以前的代码,好烦。所以记下模板,方便下次的使用。

  xml文件部分代码:

         <ListView
android:id="@+id/listview"
android:scrollingCache="false"
android:cacheColorHint="#00000000"
android:divider="@null"
android:listSelector="@android:color/transparent"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:fadingEdge="none"
android:fadeScrollbars="true">
</ListView>

item的xml布局:(listview_style.xml)

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content"
android:layout_height="wrap_content"> <TextView
android:id="@+id/textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="abcd"
/> </RelativeLayout>

java 代码:

    定义:
   private ListView m_Listview;
private MyAdapter m_listAdapter;
private List<Map<String, String>> m_dataList = new ArrayList<Map<String, String>>();
  
初始化:
   

m_listAdapter = new MyAdapter(this,m_dataList);
       m_Listview = (ListView) findViewById(R.id.listview);
       m_Listview.setAdapter(m_listAdapter);
       m_listAdapter.notifyDataSetChanged();


   自定义适配器:
class MyAdapter extends BaseAdapter{
private Context m_context; //运行上下文
private List<Map<String, String>> m_MapItems ;
private LayoutInflater m_listContainer; //视图容器 public final class ListItemView{ //自定义控件集合
TextView tv;
} public MyAdapter (Context context ,List<Map<String, String>> MapItems) { // TODO Auto-generated constructor stub
this.m_context = context;
m_listContainer = LayoutInflater.from(context); //创建视图容器并设置上下文
this.m_MapItems = MapItems;
} @Override
public int getCount() {
// TODO Auto-generated method stub
return m_MapItems.size();
} @Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return m_MapItems.get(position);
} @Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
} @Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub ListItemView listItemView = null;
if (convertView == null) {
listItemView = new ListItemView();
//获取list_item布局文件的视图
convertView = (View)m_listContainer.inflate(R.layout.listview_style,null);
listItemView.tv = (TextView) convertView.findViewById(R.id.textview);
convertView.setTag(listItemView); }else {
listItemView = (ListItemView)convertView.getTag();
}
Map<String, String> map = m_MapItems.get(position);
return convertView;
} }

Listview模板的更多相关文章

  1. Android --ListView模板

    调整了近一上午的模板 ListView表头 <?xml version="1.0" encoding="utf-8"?> <LinearLay ...

  2. wpf 中 theme 的使用 和 listview 模板的使用.

    theme 文件 <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentatio ...

  3. .NET4.0的listview与DataPager的结合使用时的模板编辑

    1.设置listview模板样式: <asp:ListView ID="ListView1" runat="server" DataSourceID=&q ...

  4. ListView中动态显示和隐藏Header&Footer

    ListView的模板写法 ListView模板写法的完整代码: android代码优化----ListView中自定义adapter的封装(ListView的模板写法) 以后每写一个ListView ...

  5. ListView Web 服务器控件概述(MSDN)

    1: "折叠"图像"展开"图像"复制"图像"复制悬停"图像 全部折叠全部展开 代码:全部 代码:多个 代码:Visual ...

  6. Smobiler控件的使用:ListView的数据绑定及实现多选

    环境 SmobilerDesigner 4.7 Visual Studio 2010以上 正文 listview绑定数据 打开Visual Studio ,新建一个SmobilerApplicatio ...

  7. listview界面显示

    1.布局写listview      2.找到listview           3.封装新闻数据到list集合中 ,目的是为adapter提供数据展示.     4.封装一个Adapter类继承B ...

  8. ASP.NET数据绑定控件简介

    •数据绑定分为数据源和数据绑定控件两部分(①数据绑定控件通过数据源获取和修改数据②数据绑定控件通过数据源隔离数据提供者和数据使用者)数据绑定控件→数据源→数据库•数据源:SqlDataSource(连 ...

  9. Android学习笔记-Adapter基础讲解

    本节引言 从本节开始我们要讲的UI控件都是跟Adapter(适配器)打交道的,了解并学会使用这个Adapter很重要, Adapter是用来帮助填充数据的中间桥梁,简单点说就是:将各种数据以合适的形式 ...

随机推荐

  1. android在listview中放入从sdcard读取的bitmap

    重写viewbinder public class viewbinder_bookmark implements SimpleAdapter.ViewBinder{ @Override public ...

  2. SQL Server死锁总结 [转]

    1. 死锁原理 根据操作系统中的定义:死锁是指在一组进程中的各个进程均占有不会释放的资源,但因互相申请被其他进程所站用不会释放的资源而处于的一种永久等待状态. 死锁的四个必要条件:互斥条件(Mutua ...

  3. 定义SAP Portal Url别名

    Defining URL Aliases Use A URL alias is the part of the portal URL after the section that specifies ...

  4. Ubuntu 16.04下UML建模PowerDesigner的替代ERMaster和MySQL Workbench

    ERMaster是Eclipse的一个插件,小巧,支持连接各种数据库,还能生成代码等.安装参考:http://www.cnblogs.com/EasonJim/p/6170686.html 当然还有一 ...

  5. MyBatis3教程

    MyBatis3教程: http://www.yihaomen.com/article/java/302.htm http://www.mybatis.org/mybatis-3/zh/index.h ...

  6. 【翻译自mos文章】注意: ASMB process exiting due to lack of ASM file activity

    注意: ASMB process exiting due to lack of ASM file activity 參考原文: NOTE: ASMB process exiting due to la ...

  7. Wireshark网络分析实战笔记(一)抓包过滤器

    抓包过滤器和显示过滤器的差别: 1.抓包过滤器配置在抓包之前,wireshark仅仅抓取抓包过滤器过滤的数据 2.显示过滤器配置在抓包后,wireshark已经抓取全部的数据包,显示过滤器让wires ...

  8. Android中不同方向嵌套滑动的解决方式(ListView为样例)

    前言: 就像手机QQ的聊天消息列表.一个纵向滑动的ListView列举全部消息,但每一条消息能够横向滑动. 而默认情况下,仅仅能有一个地方消化处理触摸事件,要么ListView吃掉这个事件.要么子It ...

  9. 练习使用Trim()函数规范名字输入

    Java中的Trim()函数能够去除字符串的空白前缀和空白后缀,可用来规范用户输入的内容,详细这样用: String s="   Hello world  ".trim(); 然后 ...

  10. LeetCode 69. Sqrt(x) (平方根)

    Implement int sqrt(int x). Compute and return the square root of x. x is guaranteed to be a non-nega ...