欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

android中在Activity中响应ListView内部按钮的点击事件的两种方法

程序员文章站 2024-02-29 14:28:58
最近交流群里面有人问到一个问题:如何在activity中响应listview内部按钮的点击事件,不要在adapter中响应? 对于这个问题,我最初给他的解答是,在adap...

最近交流群里面有人问到一个问题:如何在activity中响应listview内部按钮的点击事件,不要在adapter中响应?

对于这个问题,我最初给他的解答是,在adapter中定义一个回调接口,在activity中实现该接口,从而实现对点击事件的响应。

下班后思考了一下,觉得有两种方式都能比较好的实现:使用接口回调和使用抽象类回调。

正好可以复习一下接口和抽象类的区别,于是写了两个demo:

1.使用接口回调:

adapter类

package com.ivan.adapter; 
 
import java.util.list; 
 
import android.content.context; 
import android.util.log; 
import android.view.layoutinflater; 
import android.view.view; 
import android.view.view.onclicklistener; 
import android.view.viewgroup; 
import android.widget.baseadapter; 
import android.widget.button; 
import android.widget.textview; 
 
import com.ivan.listvieweventcallback.r; 
 
public class contentadapter extends baseadapter implements onclicklistener { 
 
  private static final string tag = "contentadapter"; 
  private list<string> mcontentlist; 
  private layoutinflater minflater; 
  private callback mcallback; 
 
  /** 
   * 自定义接口,用于回调按钮点击事件到activity 
   * @author ivan xu 
   * 2014-11-26 
   */ 
  public interface callback { 
    public void click(view v); 
  } 
 
  public contentadapter(context context, list<string> contentlist, 
      callback callback) { 
    mcontentlist = contentlist; 
    minflater = layoutinflater.from(context); 
    mcallback = callback; 
  } 
 
  @override 
  public int getcount() { 
    log.i(tag, "getcount"); 
    return mcontentlist.size(); 
  } 
 
  @override 
  public object getitem(int position) { 
    log.i(tag, "getitem"); 
    return mcontentlist.get(position); 
  } 
 
  @override 
  public long getitemid(int position) { 
    log.i(tag, "getitemid"); 
    return position; 
  } 
 
  @override 
  public view getview(int position, view convertview, viewgroup parent) { 
    log.i(tag, "getview"); 
    viewholder holder = null; 
    if (convertview == null) { 
      convertview = minflater.inflate(r.layout.list_item, null); 
      holder = new viewholder(); 
      holder.textview = (textview) convertview 
          .findviewbyid(r.id.textview1); 
      holder.button = (button) convertview.findviewbyid(r.id.button1); 
      convertview.settag(holder); 
    } else { 
      holder = (viewholder) convertview.gettag(); 
    } 
    holder.textview.settext(mcontentlist.get(position)); 
 
     
    holder.button.setonclicklistener(this); 
    holder.button.settag(position); 
    return convertview; 
  } 
 
  public class viewholder { 
    public textview textview; 
    public button button; 
  } 
 
  //响应按钮点击事件,调用子定义接口,并传入view 
  @override 
  public void onclick(view v) { 
    mcallback.click(v); 
  } 
} 

activity类:

package com.ivan.listvieweventdemo; 
 
import java.util.arraylist; 
import java.util.list; 
 
import android.app.activity; 
import android.os.bundle; 
import android.view.menu; 
import android.view.view; 
import android.widget.adapterview; 
import android.widget.adapterview.onitemclicklistener; 
import android.widget.listview; 
import android.widget.toast; 
 
import com.ivan.adapter.contentadapter; 
import com.ivan.adapter.contentadapter.callback; 
import com.ivan.listvieweventcallback.r; 
//mainactivity需要实现自定义接口 
public class mainactivity extends activity implements onitemclicklistener, 
    callback { 
 
  // 模拟listview中加载的数据 
  private static final string[] contents = { "北京", "上海", "广州", "深圳", "苏州", 
      "南京", "武汉", "长沙", "杭州" }; 
  private list<string> contentlist; 
  private listview mlistview; 
 
  @override 
  protected void oncreate(bundle savedinstancestate) { 
    super.oncreate(savedinstancestate); 
    setcontentview(r.layout.activity_main); 
 
    init(); 
  } 
 
  private void init() { 
    mlistview = (listview) findviewbyid(r.id.listview); 
    contentlist = new arraylist<string>(); 
    for (int i = 0; i < contents.length; i++) { 
      contentlist.add(contents[i]); 
    } 
    // 
    mlistview.setadapter(new contentadapter(this, contentlist, this)); 
    mlistview.setonitemclicklistener(this); 
  } 
 
  @override 
  public boolean oncreateoptionsmenu(menu menu) { 
    getmenuinflater().inflate(r.menu.main, menu); 
    return true; 
  } 
 
  /** 
   * 响应listview中item的点击事件 
   */ 
  @override 
  public void onitemclick(adapterview<?> arg0, view v, int position, long id) { 
    toast.maketext(this, "listview的item被点击了!,点击的位置是-->" + position, 
        toast.length_short).show(); 
  } 
 
  /** 
   * 接口方法,响应listview按钮点击事件 
   */ 
  @override 
  public void click(view v) { 
    toast.maketext( 
        mainactivity.this, 
        "listview的内部的按钮被点击了!,位置是-->" + (integer) v.gettag() + ",内容是-->" 
            + contentlist.get((integer) v.gettag()), 
        toast.length_short).show(); 
  } 
} 

2.使用抽象类回调

adapter类:

package com.ivan.adapter; 
 
import java.util.list; 
 
import android.content.context; 
import android.util.log; 
import android.view.layoutinflater; 
import android.view.view; 
import android.view.view.onclicklistener; 
import android.view.viewgroup; 
import android.widget.baseadapter; 
import android.widget.button; 
import android.widget.textview; 
 
import com.ivan.listvieweventabstract.r; 
 
public class contentadapter extends baseadapter { 
 
  private static final string tag = "contentadapter"; 
  private list<string> mcontentlist; 
  private layoutinflater minflater; 
  private myclicklistener mlistener; 
 
  public contentadapter(context context, list<string> contentlist, 
      myclicklistener listener) { 
    mcontentlist = contentlist; 
    minflater = layoutinflater.from(context); 
    mlistener = listener; 
  } 
 
  @override 
  public int getcount() { 
    log.i(tag, "getcount"); 
    return mcontentlist.size(); 
  } 
 
  @override 
  public object getitem(int position) { 
    log.i(tag, "getitem"); 
    return mcontentlist.get(position); 
  } 
 
  @override 
  public long getitemid(int position) { 
    log.i(tag, "getitemid"); 
    return position; 
  } 
 
  @override 
  public view getview(int position, view convertview, viewgroup parent) { 
    log.i(tag, "getview"); 
    viewholder holder = null; 
    if (convertview == null) { 
      convertview = minflater.inflate(r.layout.list_item, null); 
      holder = new viewholder(); 
      holder.textview = (textview) convertview 
          .findviewbyid(r.id.textview1); 
      holder.button = (button) convertview.findviewbyid(r.id.button1); 
      convertview.settag(holder); 
    } else { 
      holder = (viewholder) convertview.gettag(); 
    } 
    holder.textview.settext(mcontentlist.get(position)); 
    holder.button.setonclicklistener(mlistener); 
    holder.button.settag(position); 
    return convertview; 
  } 
 
  public class viewholder { 
    public textview textview; 
    public button button; 
  } 
 
  /** 
   * 用于回调的抽象类 
   * @author ivan xu 
   * 2014-11-26 
   */ 
  public static abstract class myclicklistener implements onclicklistener { 
    /** 
     * 基类的onclick方法 
     */ 
    @override 
    public void onclick(view v) { 
      myonclick((integer) v.gettag(), v); 
    } 
    public abstract void myonclick(int position, view v); 
  } 
} 

activity类:

package com.ivan.listvieweventdemo; 
 
import java.util.arraylist; 
import java.util.list; 
 
import android.app.activity; 
import android.os.bundle; 
import android.view.menu; 
import android.view.view; 
import android.widget.adapterview; 
import android.widget.adapterview.onitemclicklistener; 
import android.widget.listview; 
import android.widget.toast; 
 
import com.ivan.adapter.contentadapter; 
import com.ivan.adapter.contentadapter.myclicklistener; 
import com.ivan.listvieweventabstract.r; 
 
public class mainactivity extends activity implements onitemclicklistener { 
 
  // 模拟listview中加载的数据 
  private static final string[] contents = { "北京", "上海", "广州", "深圳", "苏州", 
      "南京", "武汉", "长沙", "杭州" }; 
  private list<string> contentlist; 
  private listview mlistview; 
 
  @override 
  protected void oncreate(bundle savedinstancestate) { 
    super.oncreate(savedinstancestate); 
    setcontentview(r.layout.activity_main); 
 
    init(); 
  } 
 
  private void init() { 
    mlistview = (listview) findviewbyid(r.id.listview); 
    contentlist = new arraylist<string>(); 
    for (int i = 0; i < contents.length; i++) { 
      contentlist.add(contents[i]); 
    } 
    //实例化contentadapter类,并传入实现类 
    mlistview.setadapter(new contentadapter(this, contentlist, mlistener)); 
     
    mlistview.setonitemclicklistener(this); 
  } 
 
  @override 
  public boolean oncreateoptionsmenu(menu menu) { 
    getmenuinflater().inflate(r.menu.main, menu); 
    return true; 
  } 
 
  //响应item点击事件 
  @override 
  public void onitemclick(adapterview<?> arg0, view v, int position, long id) { 
    toast.maketext(this, "listview的item被点击了!,点击的位置是-->" + position, 
        toast.length_short).show(); 
  } 
 
  /** 
   * 实现类,响应按钮点击事件 
   */ 
  private myclicklistener mlistener = new myclicklistener() { 
    @override 
    public void myonclick(int position, view v) { 
      toast.maketext( 
          mainactivity.this, 
          "listview的内部的按钮被点击了!,位置是-->" + position + ",内容是-->" 
              + contentlist.get(position), toast.length_short) 
          .show(); 
    } 
  }; 
} 

两种方式的区别在于,抽象类在activity中实现的时候,只能定义一个成员变量来实现,不能由activity直接实现,因为java不支持多继承。而接口既可以由activity直接实现,也可以由其成员变量来实现。

原文链接:http://blog.csdn.net/u011895534/article/details/50439547

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。