最近交流群里面有人问到一个问题:如何在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,MyClickListener Listener) { mContentList = contentList; mInflater = LayoutInflater.from(context); mListener = Listener; } @OverrIDe public int getCount() { Log.i(TAG,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,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,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
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程小技巧。
总结以上是内存溢出为你收集整理的android中在Activity中响应ListView内部按钮的点击事件的两种方法全部内容,希望文章能够帮你解决android中在Activity中响应ListView内部按钮的点击事件的两种方法所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)