本文介绍了差异listview.setOnItemClickListener和row.setOnClickListener的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建一个自定义阵列适配器,我现在想实现它处理一下视图的功能。我有考虑两种选择,但我不知道是否有性能/运行速度或什么区别吗?

选项1,在一个ArrayAdapter本身:

  row.setOnClickListener(新OnClickListener(){            @覆盖
            公共无效的onClick(视图v){
                // TODO自动生成方法存根
            }
        });

选项2,从主要活动:

  listView.setAdapter(适配器);
        listView.setOnItemClickListener(新OnItemClickListener(){            @覆盖
            公共无效onItemClick(适配器视图<>为arg0,ARG1观,诠释ARG2,
                    长ARG3){
                // TODO自动生成方法存根            }
        });

或者,他们完全一样的?


解决方案

In first approach you will create for each row own listener e.q. you have 100 rows so you'll have 100 listeners that is not good at all. In second approach you will create one listener for whole ListView.

android.widget.AdapterView.OnItemClickListener
android.view.View.OnClickListener

How you can see, first is more comfortable and directly designated for dealing with adapter widgets like ListView is. Also this approach is generally recommended and used.

You have connection with each row via parameters of onItemClick() method and code is more human-readable and it's clearer.

这篇关于差异listview.setOnItemClickListener和row.setOnClickListener的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-04 21:56