众所周知,ListView是安卓最为频繁使用的控件,但是,随着人们审美观的提高,一些初级的ListView已经满足不了需求了,于是,我们必须为自己定制一套专属的ListView,这就需要用到适配器,ArrayAdapter可以满足大部分需求,在自己定制适配器的过程中,我遇到了许许多多的问题,希望把我的心得交给大家,免得大家再重蹈覆辙,下面进入实战。

我们的终极界面是这样的:

此图是静态截面图

此图是点击ID为1的整行信息后的效果图

此图是点击ID为1中的按钮后的效果图

下面开始步入正题

完成这整个项目需要完成以下条目:

1.为每个ListView中的元素编写一个通用界面。

2.为每个元素写一个通用适配器。

3.在主界面中添加一个ListView。

4.实例化元素并与ArrayAdapter绑定,并添加全局和内部点击事件。

1.开始编写元素布局(这里叫Node)

<?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="120px"
android:orientation="horizontal"
android:descendantFocusability="blocksDescendants">            //如果不写这句代码,会使最外层元素点击无效 <TextView
android:id="@+id/order"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:layout_gravity="center"
android:textSize="30dp"
android:textColor="@color/theme"
android:gravity="center"
/> <ImageView
android:id="@+id/logo"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:layout_gravity="center"
/> <LinearLayout
android:id="@+id/info"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center_vertical"
android:layout_gravity="center"
android:orientation="vertical"> <TextView
android:id="@+id/car_order"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/theme"
/> <TextView
android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/theme"
/> </LinearLayout> <TextView
android:id="@+id/money"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center_vertical"
android:textColor="@color/black"
android:textSize="20dp"
/> <CheckBox
android:id="@+id/jud"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:layout_gravity="center"
/> <Button
android:id="@+id/delta"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:layout_gravity="center"
android:text="冲值"/> </LinearLayout>

需要在values的布局下的colors布局界面中添加如下代码:

<color name="black">#080000</color>
<color name="theme">#282626</color>

还需要在drawble目录下放一张50 * 50(推荐大小)的名为Logo.png的图片

2.为元素写一个管理类(Node类)。

public class Node {

    private int order;
private int logo;
private String car_order;
private String name;
private int money;
private CheckBox jud;
private Button delta; public Node(int order,int logo,String name,int money){ this.order = order;
this.logo = logo;
this.car_order = "辽A1000" + order;
this.name = name;
this.money = money; } public int getorder(){ return order;
} public String getcar_order(){ return car_order;
} public int getlogoID(){ return logo;
} public String getname(){ return name;
} public int getmoney(){ return money;
} }

2.为元素写一个适配器.

public class NodeAdapter extends ArrayAdapter<Node>{

    private int resourceId;

    public NodeAdapter(Context context, int resourceId, List<Node> objects){

        super(context,resourceId,objects);
this.resourceId = resourceId;
} @Override
public View getView(final int position, View convertView, ViewGroup parent) { Node node = getItem(position);
//View view = LayoutInflater.from(getContext()).inflate(resourceId, null);
View view;
ViewHolder holder; if(convertView == null){ view = LayoutInflater.from(getContext()).inflate(resourceId,parent,false);
holder = new ViewHolder();
holder.order = (TextView)view.findViewById(R.id.order);
holder.logo = (ImageView)view.findViewById(R.id.logo);
holder.car_order = (TextView)view.findViewById(R.id.car_order);
holder.name = (TextView)view.findViewById(R.id.name);
holder.money = (TextView)view.findViewById(R.id.money);
holder.jud = (CheckBox)view.findViewById(R.id.jud);
holder.delta = (Button)view.findViewById(R.id.delta); view.setTag(holder);
}else{ view = convertView;
holder = (ViewHolder)view.getTag();
} holder.order.setText(node.getorder() + "");
holder.logo.setImageResource(node.getlogoID());
holder.car_order.setText(node.getcar_order());
holder.name.setText(node.getname() + "");
holder.money.setText(node.getmoney() + ""); holder.jud.setTag(position);
holder.delta.setTag(position); holder.delta.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) { itemSelect.seleId(position);
}
}); return view;
} public interface onItemSelect{                //回调接口 void seleId(int i);
} private onItemSelect itemSelect; public void setOnItemSelectListener(onItemSelect listener){ this.itemSelect = listener;
} class ViewHolder{ TextView order;
ImageView logo;
TextView car_order;
TextView name;
TextView money;
CheckBox jud;
Button delta; } }

3,写主界面布局代码:

<?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"> <ListView
android:id="@+id/nodeview"
android:layout_width="match_parent"
android:layout_height="wrap_content" /> </LinearLayout>

4.为主界面完成代码。

public class MainActivity extends AppCompatActivity {

    private List<Node> nodeList = new ArrayList<Node>();
private ListView nodeview; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); initNode(); NodeAdapter adapter = new NodeAdapter(getApplicationContext(),R.layout.node,nodeList);
nodeview.setAdapter(adapter); nodeview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) { Node node = nodeList.get(position);
Toast.makeText(getApplicationContext(),"您要查看" + node.getorder() + "号车主的全部信息",Toast.LENGTH_SHORT).show();
}
}); adapter.setOnItemSelectListener(new NodeAdapter.onItemSelect() {
@Override
public void seleId(int i) { int order = i + 1;
Toast.makeText(getApplicationContext(),"您正在为" + order + "号车主充值",Toast.LENGTH_SHORT).show();
}
});
} private void initNode(){ nodeview = (ListView)findViewById(R.id.nodeview);
Node node1 = new Node(1,R.drawable.logo,"胡海",100);
Node node2 = new Node(2,R.drawable.logo,"蓝月",200);
nodeList.add(node1);
nodeList.add(node2);
}
}

5.整个项目框架如下:

到此,这个项目就完成了.效果上面已经有图了。

ListView + ArrayAdapter + 接口回调的更多相关文章

  1. Android接口回调的理解

    1.各种理解 <1>说白了,就是拿到对象引用,调其方法 <2>实际上就是利用多态的方式调用而已 <3>其实很容易理解的,定义接口,然后提供一个外部的接口设置进去,然 ...

  2. Android中的接口回调技术

    Android中的接口回调技术有很多应用的场景,最常见的:Activity(人机交互的端口)的UI界面中定义了Button,点击该Button时,执行某个逻辑. 下面参见上述执行的模型,讲述James ...

  3. Android系列之Fragment(三)----Fragment和Activity之间的通信(含接口回调)

    ​[声明] 欢迎转载,但请保留文章原始出处→_→ 生命壹号:http://www.cnblogs.com/smyhvae/ 文章来源:http://www.cnblogs.com/smyhvae/p/ ...

  4. Android开发之自定义组件和接口回调

    说到自定义控件不得不提的就是接口回调,在Android开发中接口回调用的还是蛮多的.在这篇博客开始的时候呢,我想聊一下iOS的自定义控件.在iOS中自定义控件的思路是继承自UIView, 在UIVie ...

  5. Android接口回调机制

    开发中,接口回调是我们经常用到的. 接口回调的意思即,注册之后并不立马执行,而在某个时机触发执行. 举个例子: A有一个问题不会,他去问B,B暂时解决不出来,B说,等我(B)解决了再告诉你(A)此时A ...

  6. java中的接口回调

    [接口回调]接口回调是多态的另一种体现.接口回调是指:可以把使用某一个接口的类创建的对象的引用赋给该接口声明的接口变量中,那么该接口变量就可以调用被类实现的接口中的方法.当接口变量调用被类实现的接口中 ...

  7. (转)Android之接口回调机制

    开发中,接口回调是我们经常用到的. 接口回调的意思即,注册之后并不立马执行,而在某个时机触发执行. 举个例子: A有一个问题不会,他去问B,B暂时解决不出来,B说,等我(B)解决了再告诉你(A)此时A ...

  8. Android 中的接口回调

    http://blog.csdn.net/wangjinyu501/article/details/22052187   在Android中到处可见接口回调机制,尤其是UI事件处理方面.举一个最常见的 ...

  9. 弄明白Android 接口回调机制

    以前对于这个机制理解不够深刻,现在重新整理下思路. 一.建模 我理解的接口回调就是,我这个类实现了一个接口里的方法doSomething,然后注册到你这里,然后我就去做别的事情去了,你在某个触发的时机 ...

随机推荐

  1. 原生ajax动态添加数据

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  2. CSS样式命名

    CSS样式命名    说明网页公共命名#wrapper    页面外围控制整体布局宽度#container或#content    容器,用于最外层#layout    布局#head, #heade ...

  3. (转)ARM GNU常用汇编语言介绍

    ARM GNU常用汇编语言介绍 原文地址:http://zqwt.012.blog.163.com/blog/static/120446842010445441611/ ARM汇编语言源程序语句,一般 ...

  4. [BUG]Uncaught TypeError: Illegal invocation at HTMLDivElement.

    <div id="clickMe" key="1">点我</div> clickMe.onclick = function (event ...

  5. 【JAVA进阶架构师指南】之二:JVM篇

    前言   谈到JAVA,就不得不提JVM---JAVA程序员绕不开的话题.也许有童鞋会说,我不懂JVM,但是我一样可以写出JAVA代码,我相信说这种话的童鞋,往往是只有1-3年的初级开发人员,对JAV ...

  6. 创建Sphinx + GitHub + ReadtheDocs托管文档

    最新博客链接 "Tsanfer's Blog" 创建Sphinx + GitHub + ReadtheDocs托管文档 Readthedocs在线电子书链接

  7. ERP系统功能及部署方式

    ERP是对企业经营过程中的一些事项进行系统化管理的一种软件,所以ERP软件包含企业经营过程中需要用到的多种功能管理模块,并且ERP软件有两种部署方式.下面一起来了解一下相关的知识吧! ERP系统的功能 ...

  8. shell脚本基础-四种启动方式

    1.当前路径启动 ./test.sh 2.绝对路径启动 pwd /data/beijing 'pwd'/test.sh 3.指定解释器执行 sh test.sh bash test.sh 4.shel ...

  9. 【分布式锁】06-Zookeeper实现分布式锁:可重入锁源码分析

    前言 前面已经讲解了Redis的客户端Redission是怎么实现分布式锁的,大多都深入到源码级别. 在分布式系统中,常见的分布式锁实现方案还有Zookeeper,接下来会深入研究Zookeeper是 ...

  10. C#面向对象--索引器

    一.索引器(Indexer)允许类和结构的实例像数组一样通过索引取值,可以看做是对[]运算符的重载,索引器实际上就是有参数的属性,也被称为有参属性或索引化属性,其声明形式与属性相似,不同之处在于索引器 ...