首页 技术 正文
技术 2022年11月16日
0 收藏 796 点赞 4,919 浏览 2435 个字

在应用开发中常常会用到ListView,而且每个Item里面都会有button之类的须要进行事件监听的控件。在给button加入OnClickListener的时候,一開始非常下意识的会想在ListView的adapter的getView方法中找到每个Button并new一个OnClickListener分配给这个button。

可是当数据量非常大的时候,new出来这么多个监听器势必会对内存造成一定的压力。并且每一个Listener的功能全然一样,Listener里面所需知道的,只是是调用者所在的Item的index而已。

怎么样才干更好地利用内存呢?既然每一个Listener的功能一样,那么全然能够用单例模式构造一个Listener。

例如以下:

class MyOnClickListener implements OnClickListener {   private static MyOnClickListener instance = null;   private MyOnClickListener() {
} public static MyOnClickListener getInstance() {
if (instance == null)
instance = new MyOnClickListener() ;
return instance;
} @Override
public void onClick(View view) {
//TODO: do something here
}
}

而在getView方法中,获取到button实例之后。仅仅须要通过button.setOnClickListener(MyOnClickListener.getInstance());对按钮设置监听器了。这种话每个按钮便必定用的是同一个Listener对象。

可是我们的需求并不止于此。非常多时候,我们还须要知道详细是哪个position的button被点击了,我们须要依据position在Listener里面做出不一样的动作。

想要在Listener内部了解外部控件的属性,我们首先想到的是传參。可是因为我们的Listener使用的是单例模式,每一个button往Listner里面传的參数必定会

覆盖前一个button传的參数。于是我们的解决方式仅仅剩下一种,那就是通过onClick函数的參数(View view)来获取该信息。然而,此处的view应该是一个Button,

而Button是不具备position信息的。又于是,自然而然的,解决方式出来了:重载Button类。

class MyButton extends Button {    private int index = -1;    public int getIndex() {
return index;
} public void setIndex(int index) {
this.index = index;
} public MyButton(Context context) {
super(context);
// TODO: do something here if you want
} public MyButton(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO: do something here if you want
} public MyButton(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
// TODO: do something here if you want
}
}

接下来我们须要做的,就是在xml文件里,将item里面的Button的类型改成我们自己定义的MyButton。

即将<Button> </Button>改成<your.package.name.MyButton> </your.package.name.MyButton>,而在adapter的getView函数里面则把findViewById()获得的返回值强制转换成为MyButton。并调用其setIndex函数设置Index值。同一时候MyOnClickListener中重载的的onClick函数也一样将view对象转换成MyButton类型,并通过调用getIndex函数获取position信息,以做对应操作。

Adapter中:

// ....
MyButton button = null;
// ....
@Override
public View getView(int position, View convertView, ViewGroup parentView) {
View view = convertView;
if (convertView == null) {
view = LayoutInflater.from(activity).inflate(R.layout.company_detail_campus_talk_item, null);
} // .... button = (MyButton) view.findViewById(R.id.YOUR_BUTTON_ID);
button.setIndex(position);
button.setOnClickListener(MyOnClickListener.getInstance());
}

MyOnClickListener中:

// ....
@Override
public void onClick(View view) { int index = ((MyButton)view).getIndex(); // ....
}

这样,我们便实现了使用同一个Listener对ListView中不同Item的button进行事件监听处理的业务逻辑。

假设须要在Adapter和Listener之间共享数据的话,能够通过添加Listener的getInstance函数的參数以及Listener类的成员变量实现。

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,088
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,565
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,413
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,186
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,822
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,905