編輯:關於Android編程
最近交流群裡面有人問到一個問題:如何在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
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持本站。
1、概述Android提供了幾種動畫類型:View Animation 、Drawable Animation 、Property Animation 。View An
開發者可利用SDK提供的接口,使用百度為您提供的基礎地圖數據。目前百度地圖SDK所提供的地圖等級為3-21級,所包含的信息有建築物、道路、河流、學校、公園等內容。 V3.
上個周末,3個小時總體上讀完了《Android群英傳》,本周主要在研究代碼層次的設計和實現。編譯安裝在手機上,玩了幾把,結合代碼,一周時間才掌握了整體的思路。大部分時間,
Intent的匹配過程中有三個步驟,包括Action , category與data 的匹配。如果匹配出了多個結果,系統會顯示一個dialog讓用戶來選