1. 程式人生 > >Activity中響應ListView內部按鈕的點選事件

Activity中響應ListView內部按鈕的點選事件

最近交流群裡面有人問到一個問題:如何在Activity中響應ListView內部按鈕的點選事件,不要在Adapter中響應?

對於這個問題,我最初給他的解答是,在Adapter中定義一個回撥介面,在Activity中實現該介面,從而實現對點選事件的響應。

下班後思考了一下,覺得有兩種方式都能比較好的實現:使用介面回撥和使用抽象類回撥

正好可以複習一下介面和抽象類的區別,於是寫了兩個Demo:

1.使用介面回撥:

Adapter類

複製程式碼
 1 package com.ivan.adapter;
 2 
 3 import java.util.List;
4 5 import android.content.Context; 6 import android.util.Log; 7 import android.view.LayoutInflater; 8 import android.view.View; 9 import android.view.View.OnClickListener; 10 import android.view.ViewGroup; 11 import android.widget.BaseAdapter; 12 import android.widget.Button; 13 import android.widget.TextView;
14 15 import com.ivan.listvieweventcallback.R; 16 17 public class ContentAdapter extends BaseAdapter implements OnClickListener { 18 19 private static final String TAG = "ContentAdapter"; 20 private List<String> mContentList; 21 private LayoutInflater mInflater; 22 private Callback mCallback;
23 24 /** 25 * 自定義介面,用於回撥按鈕點選事件到Activity 26 * @author Ivan Xu 27 * 2014-11-26 28 */ 29 public interface Callback { 30 public void click(View v); 31 } 32 33 public ContentAdapter(Context context, List<String> contentList, 34 Callback callback) { 35 mContentList = contentList; 36 mInflater = LayoutInflater.from(context); 37 mCallback = callback; 38 } 39 40 @Override 41 public int getCount() { 42 Log.i(TAG, "getCount"); 43 return mContentList.size(); 44 } 45 46 @Override 47 public Object getItem(int position) { 48 Log.i(TAG, "getItem"); 49 return mContentList.get(position); 50 } 51 52 @Override 53 public long getItemId(int position) { 54 Log.i(TAG, "getItemId"); 55 return position; 56 } 57 58 @Override 59 public View getView(int position, View convertView, ViewGroup parent) { 60 Log.i(TAG, "getView"); 61 ViewHolder holder = null; 62 if (convertView == null) { 63 convertView = mInflater.inflate(R.layout.list_item, null); 64 holder = new ViewHolder(); 65 holder.textView = (TextView) convertView 66 .findViewById(R.id.textView1); 67 holder.button = (Button) convertView.findViewById(R.id.button1); 68 convertView.setTag(holder); 69 } else { 70 holder = (ViewHolder) convertView.getTag(); 71 } 72 holder.textView.setText(mContentList.get(position)); 73 74 75 holder.button.setOnClickListener(this); 76 holder.button.setTag(position); 77 return convertView; 78 } 79 80 public class ViewHolder { 81 public TextView textView; 82 public Button button; 83 } 84 85 //響應按鈕點選事件,呼叫子定義介面,並傳入View 86 @Override 87 public void onClick(View v) { 88 mCallback.click(v); 89 } 90 }
複製程式碼

Activity類:

複製程式碼
 1 package com.ivan.listvieweventdemo;
 2 
 3 import java.util.ArrayList;
 4 import java.util.List;
 5 
 6 import android.app.Activity;
 7 import android.os.Bundle;
 8 import android.view.Menu;
 9 import android.view.View;
10 import android.widget.AdapterView;
11 import android.widget.AdapterView.OnItemClickListener;
12 import android.widget.ListView;
13 import android.widget.Toast;
14 
15 import com.ivan.adapter.ContentAdapter;
16 import com.ivan.adapter.ContentAdapter.Callback;
17 import com.ivan.listvieweventcallback.R;
18 //MainActivity需要實現自定義介面
19 public class MainActivity extends Activity implements OnItemClickListener,
20         Callback {
21 
22     // 模擬listview中載入的資料
23     private static final String[] CONTENTS = { "北京", "上海", "廣州", "深圳", "蘇州",
24             "南京", "武漢", "長沙", "杭州" };
25     private List<String> contentList;
26     private ListView mListView;
27 
28     @Override
29     protected void onCreate(Bundle savedInstanceState) {
30         super.onCreate(savedInstanceState);
31         setContentView(R.layout.activity_main);
32 
33         init();
34     }
35 
36     private void init() {
37         mListView = (ListView) findViewById(R.id.listview);
38         contentList = new ArrayList<String>();
39         for (int i = 0; i < CONTENTS.length; i++) {
40             contentList.add(CONTENTS[i]);
41         }
42         //
43         mListView.setAdapter(new ContentAdapter(this, contentList, this));
44         mListView.setOnItemClickListener(this);
45     }
46 
47     @Override
48     public boolean onCreateOptionsMenu(Menu menu) {
49         getMenuInflater().inflate(R.menu.main, menu);
50         return true;
51     }
52 
53     /**
54      * 響應ListView中item的點選事件
55      */
56     @Override
57     public void onItemClick(AdapterView<?> arg0, View v, int position, long id) {
58         Toast.makeText(this, "listview的item被點選了!,點選的位置是-->" + position,
59                 Toast.LENGTH_SHORT).show();
60     }
61 
62     /**
63      * 介面方法,響應ListView按鈕點選事件
64      */
65     @Override
66     public void click(View v) {
67         Toast.makeText(
68                 MainActivity.this,
69                 "listview的內部的按鈕被點選了!,位置是-->" + (Integer) v.getTag() + ",內容是-->"
70                         + contentList.get((Integer) v.getTag()),
71                 Toast.LENGTH_SHORT).show();
72     }
73 }
複製程式碼

2.使用抽象類回撥

Adapter類:

複製程式碼
 1 package com.ivan.adapter;
 2 
 3 import java.util.List;
 4 
 5 import android.content.Context;
 6 import android.util.Log;
 7 import android.view.LayoutInflater;
 8 import android.view.View;
 9 import android.view.View.OnClickListener;
10 import android.view.ViewGroup;
11 import android.widget.BaseAdapter;
12 import android.widget.Button;
13 import android.widget.TextView;
14 
15 import com.ivan.listvieweventabstract.R;
16 
17 public class ContentAdapter extends BaseAdapter {
18 
19     private static final String TAG = "ContentAdapter";
20     private List<String> mContentList;
21     private LayoutInflater mInflater;
22     private MyClickListener mListener;
23 
24     public ContentAdapter(Context context, List<String> contentList,
25             MyClickListener listener) {
26         mContentList = contentList;
27         mInflater = LayoutInflater.from(context);
28         mListener = listener;
29     }
30 
31     @Override
32     public int getCount() {
33         Log.i(TAG, "getCount");
34         return mContentList.size();
35     }
36 
37     @Override
38     public Object getItem(int position) {
39         Log.i(TAG, "getItem");
40         return mContentList.get(position);
41     }
42 
43     @Override
44     public long getItemId(int position) {
45         Log.i(TAG, "getItemId");
46         return position;
47     }
48 
49     @Override
50     public View getView(int position, View convertView, ViewGroup parent) {
51         Log.i(TAG, "getView");
52         ViewHolder holder = null;
53         if (convertView == null) {
54             convertView = mInflater.inflate(R.layout.list_item, null);
55             holder = new ViewHolder();
56             holder.textView = (TextView) convertView
57                     .findViewById(R.id.textView1);
58             holder.button = (Button) convertView.findViewById(R.id.button1);
59             convertView.setTag(holder);
60         } else {
61             holder = (ViewHolder) convertView.getTag();
62         }
63         holder.textView.setText(mContentList.get(position));
64         holder.button.setOnClickListener(mListener);
65         holder.button.setTag(position);
66         return convertView;
67     }
68 
69     public class ViewHolder {
70         public TextView textView;
71         public Button button;
72     }
73 
74     /**
75      * 用於回撥的抽象類
76      * @author Ivan Xu
77      * 2014-11-26
78      */
79     public static abstract class MyClickListener implements OnClickListener {
80         /**
81          * 基類的onClick方法
82          */
83         @Override
84         public void onClick(View v) {
85             myOnClick((Integer) v.getTag(), v);
86         }
87         public abstract void myOnClick(int position, View v);
88     }
89 }
複製程式碼

Activity類:

複製程式碼
 1 package com.ivan.listvieweventdemo;
 2 
 3 import java.util.ArrayList;
 4 import java.util.List;
 5 
 6 import android.app.Activity;
 7 import android.os.Bundle;
 8 import android.view.Menu;
 9 import android.view.View;
10 import android.widget.AdapterView;
11 import android.widget.AdapterView.OnItemClickListener;
12 import android.widget.ListView;
13 import android.widget.Toast;
14 
15 import com.ivan.adapter.ContentAdapter;
16 import com.ivan.adapter.ContentAdapter.MyClickListener;
17 import com.ivan.listvieweventabstract.R;
18 
19 public class MainActivity extends Activity implements OnItemClickListener {
20 
21     // 模擬listview中載入的資料
22     private static final String[] CONTENTS = { "北京", "上海", "廣州", "深圳", "蘇州",
23             "南京", "武漢", "長沙", "杭州" };
24     private List<String> contentList;
25     private ListView mListView;
26 
27     @Override
28     protected void onCreate(Bundle savedInstanceState) {
29         super.onCreate(savedInstanceState);
30         setContentView(R.layout.activity_main);
31 
32         init();
33     }
34 
35     private void init() {
36         mListView = (ListView) findViewById(R.id.listview);
37         contentList = new ArrayList<String>();
38         for (int i = 0; i < CONTENTS.length; i++) {
39             contentList.add(CONTENTS[i]);
40         }
41         //例項化ContentAdapter類,並傳入實現類
42         mListView.setAdapter(new ContentAdapter(this, contentList, mListener));
43         
44         mListView.setOnItemClickListener(this);
45     }
46 
47     @Override
48     public boolean onCreateOptionsMenu(Menu menu) {
49         getMenuInflater().inflate(R.menu.main, menu);
50         return true;
51     }
52 
53     //響應item點選事件
54     @Override
55     public void onItemClick(AdapterView<?> arg0, View v, int position, long id) {
56         Toast.makeText(this, "listview的item被點選了!,點選的位置是-->" + position,
57                 Toast.LENGTH_SHORT).show();
58     }
59 
60     /**
61      * 實現類,響應按鈕點選事件
62      */
63     private MyClickListener mListener = new MyClickListener() {
64         @Override
65         public void myOnClick(int position, View v) {
66             Toast.makeText(
67                     MainActivity.this,
68                     "listview的內部的按鈕被點選了!,位置是-->" + position + ",內容是-->"
69                             + contentList.get(position), Toast.LENGTH_SHORT)
70                     .show();
71         }
72     };
73 }
複製程式碼

 

以下是佈局檔案

複製程式碼
 1 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 2     xmlns:tools="http://schemas.android.com/tools"
 3     android:layout_width="match_parent"
 4     android:layout_height="match_parent"
 5     android:paddingBottom="@dimen/activity_vertical_margin"
 6     android:paddingLeft="@dimen/activity_horizontal_margin"
 7     android:paddingRight="@dimen/activity_horizontal_margin"
 8     android:paddingTop="@dimen/activity_vertical_margin"
 9     tools:context=".MainActivity" >
10 
11     <ListView
12         android:id="@+id/listview"
13         android:layout_width="match_parent"
14         android:layout_height="match_parent" >
15     </ListView>
16 
17 </RelativeLayout>
複製程式碼

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:descendantFocusability="blocksDescendants"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="content"
        android:textColor="#ff0000"
        android:textSize="20sp" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:focusable="false"
        android:text="Button"
        android:textColor="#ff0000"
        android:textSize="20sp" />
按 Ctrl+C 複製程式碼

 

兩種方式的區別在於,抽象類在Activity中實現的時候,只能定義一個成員變數來實現,不能由Activity直接實現,因為Java不支援多繼承。而介面既可以由Activity直接實現,也可以由其成員變數來實現。