每次写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. codevs——1517 求一次函数解析式

    1517 求一次函数解析式  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 白银 Silver 题解       题目描述 Description 相信大家都做过练习册上的这种 ...

  2. Codeforces 300E(数学)

    题意:给定k个数字,求最小的正整数n,使得“n的阶乘”是“这k个数字的阶乘的积”的倍数.1<=k<=1e6,数字ai满足1<=ai<=1e7 分析:如果我们能对着k个数字的阶乘 ...

  3. [CH#56]过河(贪心)

    题目:http://ch.ezoj.tk/contest/CH%20Round%20%2356%20-%20%E5%9B%BD%E5%BA%86%E8%8A%82%E6%AC%A2%E4%B9%90% ...

  4. Ubuntu下非常规方法安装绿色软件(压缩包)

    继上一篇http://www.cnblogs.com/EasonJim/p/7117567.html文章中说的常规方式安装的软件,都会自动在命令行及Dash Home中体现. 但是如果是使用压缩包进行 ...

  5. 【sql技巧】mysql修改时,动态指定要修改的字段 update `table` set (case when ....) = 1 where id = xx

    如果你点进了这篇帖子,那么你一定遇到了跟我一样的问题.别看题目的set case when...,我一开始也是第一反应是用case when但是发现并不好使. 问题呢,说得高大上一点:动态指定要修改的 ...

  6. PHP array_keys()

    定义和用法 array_keys() 函数返回包含数组中所有键名的一个新数组. 如果提供了第二个参数,则只返回键值为该值的键名. 如果 strict 参数指定为 true,则 PHP 会使用全等比较 ...

  7. Unix/Linux环境C编程新手教程(40) 初识文件操作

     1.函数介绍 close(关闭文件) 相关函数 open,fcntl,shutdown.unlink,fclose 表头文件 #include<unistd.h> 定义函数 int ...

  8. ios11--UIButton

    // // ViewController.m // 02-UIButton(在代码中使用) // #import "ViewController.h" @interface Vie ...

  9. Versioning with the Override and New Keywords (C# Programming Guide)

    The C# language is designed so that versioning between base and derived classes in different librari ...

  10. [NOIP2003普及组]麦森数(快速幂+高精度)

    [NOIP2003普及组]麦森数(快速幂+高精度) Description 形如2^P-1的素数称为麦森数,这时P一定也是个素数.但反过来不一定,即如果P是个素数,2^P-1不一定也是素数.到1998 ...