1. 程式人生 > >ListView的item中內部點選事件的傳遞

ListView的item中內部點選事件的傳遞

在一般的情況下ListView的點選事件只需要的在Activity中設定一個onItemClickListener()方法。但是遇到item裡面的按鈕、圖片、文字、需要點選的時候,就需要把點選事件從Adapter中傳到Activity中 
這裡寫圖片描述

首先建立一個介面

public interface Callback {
    public void click(View view);
}
  • 1
  • 2
  • 3
  • 1
  • 2
  • 3

在Adapter裡呼叫這個藉口,將點選事件傳遞出來


/**
 *這個Adapter需要繼承OnClickListener,然後將item裡面View的點選事件傳遞給Callback介面
 */
public
class CoursePayAdapter extends BaseAdapter implements View.OnClickListener{ private List<CoursePay> mList;//自定義的資料model private LayoutInflater mInflater; private Callback mCallback;//介面 /*** * 構造器 * @param mList 資料 * @param mInflater 得到佈局 * @param mCallback 點選事件的介面 */
public CoursePayAdapter(List<CoursePay> mList, LayoutInflater mInflater,Callback mCallback) { this.mList = mList; this.mInflater = mInflater; this.mCallback=mCallback; } @Override public int getCount() { return mList.size(); } @Override
public Object getItem(int position) { return position; } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { ViewHolder holder=null; if (convertView==null){ convertView=mInflater.inflate(R.layout.listview_item_course,null); holder=new ViewHolder(); holder.textViewTitle= (TextView) convertView.findViewById(R.id.textview_item_course_name); holder.imageViewSub= (ImageView) convertView.findViewById(R.id.imageview_item_course_sub); holder.textViewCount= (TextView) convertView.findViewById(R.id.textview_item_course_count); holder.imageViewAdd= (ImageView) convertView.findViewById(R.id.imageview_item_course_add); holder.textViewOtherName= (TextView) convertView.findViewById(R.id.textview_item_course_other_name); holder.textViewOther= (TextView) convertView.findViewById(R.id.textview_item_course_other); convertView.setTag(holder); }else { holder = (ViewHolder) convertView.getTag(); } CoursePay course=mList.get(position); holder.textViewTitle.setText(course.getName()); holder.imageViewSub.setOnClickListener(this);//將需要設定點選事件的View在這裡設定 holder.imageViewSub.setTag(position);//把點選的位置資訊也要打包 holder.textViewCount.setText(""+course.getNum()); // holder.textViewCount.setTag(position); holder.imageViewAdd.setOnClickListener(this);//將需要設定點選事件的View在這裡設定 holder.imageViewAdd.setTag(position);//把點選的位置資訊也要打包 holder.textViewOtherName.setText("課程註釋:"); holder.textViewOther.setText(course.getOther()); return convertView; } public class ViewHolder{ public TextView textViewTitle; public ImageView imageViewSub; public TextView textViewCount; public ImageView imageViewAdd; private TextView textViewOtherName; public TextView textViewOther; } @Override public void onClick(View v) { //將點選事件傳遞出來 mCallback.click(v); } }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79

然後在Activity中處理點選事件

/**
 * 根據item點選事件來判斷商品價格總數
 */
public class CoursePayTwoActivity extends HSActivity implements AdapterView.OnItemClickListener,Callback{
    private ListView mListView;
    private List<CoursePay> mData;
    private LayoutInflater mInflater;
    private CoursePayAdapter mAdapter;
    private TextView mTextViewPrice;//顯示價格
    private int count=0;//總價格
    private ImageView mImageViewBack;//返回按鈕
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_course_pay_two);
        mListView= (ListView) findViewById(R.id.listview_course);
        mTextViewPrice= (TextView) findViewById(R.id.textview_price);
        mInflater=getLayoutInflater();
        init();
        mAdapter=new CoursePayAdapter(mData, mInflater,this);
        mListView.setAdapter(mAdapter);
        mListView.setOnItemClickListener(this);

        mImageViewBack= (ImageView) findViewById(R.id.imageview_back);
        mImageViewBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

    }

    /***
     * 封裝資料
     */
    private void init() {
        mData=new ArrayList<>();
        CoursePay course1=new CoursePay("塑型課程","1節課時的專業輔導",1999,0);
        CoursePay course2=new CoursePay("減肥課程","7節課時的專業輔導",3999,0);
        mData.add(course1);
        mData.add(course2);
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

    }

    /***
     *  對傳過來的點選事件進行處理
     * @param view
     */
    @Override
    public void click(View view) {
        count=0;//清零
        switch (view.getId()){
            case R.id.imageview_item_course_sub:
                int position=(Integer)view.getTag();//得到點選的位置
                CoursePay coursePaySub=mData.get(position);//得到item的資料
                int countSub=coursePaySub.getNum();//得到當前數量
                if (countSub>=1){
                    countSub--;
                }
                coursePaySub.setNum(countSub);//設定最新的數量
                Toast.makeText(this, "得到的位置是   "+position, Toast.LENGTH_SHORT).show();
                mAdapter.notifyDataSetChanged();
                break;
            case R.id.imageview_item_course_add:
                int positionAdd=(Integer)view.getTag();//得到點選的位置
                CoursePay coursePayAdd=mData.get(positionAdd);//得到item的資料
                int countAdd=coursePayAdd.getNum();//得到當前數量
                if (countAdd>=0){
                    countAdd+=1;
                }
                coursePayAdd.setNum(countAdd);//設定最新的數量
                mAdapter.notifyDataSetChanged();
                Toast.makeText(this, "得到的位置是  "+positionAdd, Toast.LENGTH_SHORT).show();
                break;
        }
        //價格總數
            for (int i=0;i<mData.size();i++){
                count+=mData.get(i).getPrice()*mData.get(i).getNum();
            }
        mTextViewPrice.setText(""+count);
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87

item的XML佈局檔案是

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

        <TextView
            android:id="@+id/textview_item_course_name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:text="塑型課程" />

        <ImageView
            android:id="@+id/imageview_item_course_sub"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:layout_toLeftOf="@+id/textview_item_course_count"
            android:src="@mipmap/u293" />

        <ImageView
            android:id="@+id/imageview_item_course_add"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"
            android:src="@mipmap/u11" />

        <TextView
            android:id="@+id/textview_item_course_count"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_toLeftOf="@+id/imageview_item_course_add"
            android:gravity="center_vertical"
            android:padding="10dp"
            android:text="1"
            android:textColor="@color/gym_title_background"
            android:textSize="20sp" />
    </RelativeLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingLeft="10dp">
        <TextView
            android:id="@+id/textview_item_course_other_name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="課程註釋:"/>
        <TextView
            android:id="@+id/textview_item_course_other"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="1節課時的專業指導" />
    </LinearLayout>

</LinearLayout>

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63