zoukankan      html  css  js  c++  java
  • Android 关于ListView中按钮监听的优化问题(方法二)

    关于ListView中按钮监听的优化问题(方法一)地址:

    http://www.cnblogs.com/steffen/p/3951901.html

    之前的方法一,虽然能够解决position的传递,但是我们一般在监听事件中会经常用到Context和存储数据的集合ArrayList,若每次都要将这么多的数据传递给Button的话,那个Button的数据量必然会增大。

    因此,参考方法一我们可以将Button的监听事件写成Adapter的内部中,方便我们调用Adapter中的所有数据。废话不多说,直接上代码:

     1 // ...
     2 
     3 private ButtonClickListener btnClickListener ;
     4 
     5 public ItemAdapter(Context c, ArrayList<Map<String, Object>> appList, int resource, String[] from, int[] to) {
     6     super(c, appList, resource, from, to);
     7   btnClickListener = new ButtonClickListener();
     8 }
     9 
    10 @Override
    11 public View getView(int position, View converView, ViewGroup parentView) {
    12     Handler handler = null;
    13     View view = convertView;
    14     if(convertView == null) {
    15         view = LayoutInflater.from(activity).inflate(R.layout.item_list, null);
    16         handler= new Handler();
    17         handler.button = view.findViewById(R.id.item_list_btn);
    18      handler.button.setOnclickListener(btnClickListener);
    19 view.setTag(handler); 20 } else { 21 handler = (Handler) view.getTag(); 22 } 23 // ... 24 handler.button.setTag(position); 25   return view; 26 } 27 28 private class ButtonClickListener implements View.OnClickListener { 29 @Override 30 public void onClick(View arg0) { 31 int position = (Integer) ((Button) arg0).getTag(); 32 // ... 33 } 34 } 35 36 private class Handler { 37   Button btn; 38 }
  • 相关阅读:
    图论初步
    分块和块状链表
    线段树入门
    ST表与树状数组
    [luogu P1312]Mayan游戏
    [luoguP4139]上帝与集合的正确用法


    电解质
    无机盐
  • 原文地址:https://www.cnblogs.com/steffen/p/3952090.html
Copyright © 2011-2022 走看看