生活随笔
收集整理的這篇文章主要介紹了
为ListView每个Item上面的按钮添加事件
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
1.先看下效果圖:
??????
在這里僅供測試,我把數據都寫死了,根據需要可以自己進行修改,此外實現ListView上面每個Item上的Button的事件有兩種方法:
1.使用final變量擴展局部變量的生命周期范圍主要代碼(本文最后附全部代碼):
[java]?view plaincopy
?? ????????@Override?? ????????public?View?getView(final?int?position,?View?convertView,?ViewGroup?parent)?{?? ?????????????ViewHolder?holder?=?null;?? ????????????if?(convertView?==?null)?{?? ?????????????????? ????????????????holder=new?ViewHolder();???? ?????????????????? ?????????????????? ????????????????convertView?=?mInflater.inflate(R.layout.vlist,?null);?? ????????????????holder.title?=?(TextView)convertView.findViewById(R.id.title);?? ????????????????holder.info?=?(TextView)convertView.findViewById(R.id.info);?? ????????????????holder.viewBtn?=?(Button)convertView.findViewById(R.id.view_btn);?? ????????????????convertView.setTag(holder);??????????????? ????????????}else?{??????????????? ????????????????holder?=?(ViewHolder)convertView.getTag();?? ????????????}????????? ?????????????? ????????????holder.title.setText((String)mData.get(position).get("title"));?? ????????????holder.info.setText((String)mData.get(position).get("info"));?? ????????????holder.viewBtn.setTag(position);?? ?????????????? ????????????holder.viewBtn.setOnClickListener(new?View.OnClickListener()?{?? ?????????????????? ????????????????@Override?? ????????????????public?void?onClick(View?v)?{?? ????????????????????showInfo(position);??????????????????? ????????????????}?? ????????????});?? ?????????????? ?????????????? ?????????????????????? ????????????return?convertView;?? ????????}?? ????}?? ?????? ?????? ????public?final?class?ViewHolder?{?? ????????public?TextView?title;?? ????????public?TextView?info;?? ????????public?Button?viewBtn;?? ????}?? ????public?void?showInfo(int?position){?? ?????????? ????????ImageView?img=new?ImageView(ListViewActivity.this);?? ????????img.setImageResource(R.drawable.b);?? ????????new?AlertDialog.Builder(this).setView(img)?? ????????.setTitle("詳情"+position)?? ????????.setMessage("菜名:"+title[position]+"???價格:"+info[position])?? ????????.setPositiveButton("確定",?new?DialogInterface.OnClickListener()?{?? ????????????@Override?? ????????????public?void?onClick(DialogInterface?dialog,?int?which)?{?? ????????????}?? ????????})?? ????????.show();?? ????}?? ??????
2.使用類記錄每個Button的位置,使每個BUTTON都有自己的Listener主要代碼:
[java]?view plaincopy
?? ?????? ????????public?View?getView(int?position,?View?convertView,?ViewGroup?parent)?{?? ?????????????ViewHolder?holder?=?null;?? ?????????????MyListener?myListener=null;?? ????????????if?(convertView?==?null)?{?? ?????????????????? ????????????????holder=new?ViewHolder();???? ?????????????????? ?????????????????? ?????????????????myListener=new?MyListener(position);?? ????????????????????? ????????????????convertView?=?mInflater.inflate(R.layout.vlist,?null);?? ????????????????holder.title?=?(TextView)convertView.findViewById(R.id.title);?? ????????????????holder.info?=?(TextView)convertView.findViewById(R.id.info);?? ????????????????holder.viewBtn?=?(Button)convertView.findViewById(R.id.view_btn);?? ????????????????convertView.setTag(holder);??????????????? ????????????}else?{??????????????? ????????????????holder?=?(ViewHolder)convertView.getTag();?? ????????????}????????? ?????????????? ????????????holder.title.setText((String)mData.get(position).get("title"));?? ????????????holder.info.setText((String)mData.get(position).get("info"));?? ????????????holder.viewBtn.setTag(position);?? ?????????????? ????????????holder.viewBtn.setOnClickListener(?myListener);?? ?????????????? ?????????????? ?????????????????????? ????????????return?convertView;?? ????????}?? ????}?? ?????? ?????private?class?MyListener?implements?OnClickListener{?? ????????????int?mPosition;?? ????????????public?MyListener(int?inPosition){?? ????????????????mPosition=?inPosition;?? ????????????}?? ????????????@Override?? ????????????public?void?onClick(View?v)?{?? ?????????????????? ????????????????Toast.makeText(ListViewActivity.this,?title[mPosition],?Toast.LENGTH_SHORT).show();?? ????????????}?? ?????????????? ????????}?? ?? ?????? ?????? ?????? ?????? ?????? ?????? ?????? ?????? ?????? ?????? ????public?final?class?ViewHolder?{?? ????????public?TextView?title;?? ????????public?TextView?info;?? ????????public?Button?viewBtn;?? ????}??
3.全部代碼
1.ListViewActivity.java全部代碼:
[java]?view plaincopy
package?ms.ListView;?? ?? import?java.util.ArrayList;?? import?java.util.HashMap;?? import?java.util.List;?? import?java.util.Map;?? ?? import?android.app.Activity;?? import?android.app.AlertDialog;?? import?android.content.Context;?? import?android.content.DialogInterface;?? import?android.os.Bundle;?? import?android.view.LayoutInflater;?? import?android.view.View;?? import?android.view.View.OnClickListener;?? import?android.view.ViewGroup;?? import?android.widget.AdapterView;?? import?android.widget.AdapterView.OnItemSelectedListener;?? import?android.widget.BaseAdapter;?? import?android.widget.Button;?? import?android.widget.ImageView;?? import?android.widget.ListView;?? import?android.widget.TextView;?? import?android.widget.Toast;?? ?? public?class?ListViewActivity?extends?Activity?{?? ?????? ????private?List<Map<String,?Object>>?mData;?? ????private?int?flag;?? ????public?static?String?title[]=new?String[]{"菜名0","菜名1","菜名2","菜名3","菜名4","菜名5","菜名6","菜名7","菜名8","菜名9"};?? ????public?static?String?info[]=new?String[]{?"¥:28","¥:28","¥:28","¥:28","¥:28","¥:28","¥:28","¥:28","¥:28","¥:28",};?? ?????? ?? ????@Override?? ????public?void?onCreate(Bundle?savedInstanceState)?{?? ????????super.onCreate(savedInstanceState);?? ????????setContentView(R.layout.main);?? ????????mData?=?getData();?? ????????ListView?listView?=?(ListView)?findViewById(R.id.listView);?? ????????MyAdapter?adapter?=?new?MyAdapter(this);?? ????????listView.setAdapter(adapter);?? ?????????? ?????????? ????}?? ?? ?? ?? ?????? ????private?List<Map<String,?Object>>?getData()?{?? ????????List<Map<String,?Object>>?list?=?new?ArrayList<Map<String,?Object>>();?? for(int?i=0;i<title.length;i++){?? ????????Map<String,?Object>?map?=?new?HashMap<String,?Object>();?? ????????map.put("title",?title[i]);?? ????????map.put("info",?info[i]);?? ????????list.add(map);?? }?? ?? ????????return?list;?? ????}?? ?? ????public?class?MyAdapter?extends?BaseAdapter?{?? ?? ????????private?LayoutInflater?mInflater;?? ?? ????????public?MyAdapter(Context?context)?{?? ????????????this.mInflater?=?LayoutInflater.from(context);?? ????????}?? ?? ????????@Override?? ????????public?int?getCount()?{?? ?????????????? ????????????return?mData.size();?? ????????}?? ?? ????????@Override?? ????????public?Object?getItem(int?position)?{?? ?????????????? ????????????return?null;?? ????????}?? ?? ????????@Override?? ????????public?long?getItemId(int?position)?{?? ?????????????? ????????????return?0;?? ????????}?? ?????????? ?? ????????@Override?? ????????public?View?getView(final?int?position,?View?convertView,?ViewGroup?parent)?{?? ?????????????ViewHolder?holder?=?null;?? ????????????if?(convertView?==?null)?{?? ?????????????????? ????????????????holder=new?ViewHolder();???? ?????????????????? ?????????????????? ?????????????????? ????????????????convertView?=?mInflater.inflate(R.layout.vlist,?null);?? ????????????????holder.title?=?(TextView)convertView.findViewById(R.id.title);?? ????????????????holder.info?=?(TextView)convertView.findViewById(R.id.info);?? ????????????????holder.viewBtn?=?(Button)convertView.findViewById(R.id.view_btn);?? ????????????????convertView.setTag(holder);??????????????? ????????????}else?{??????????????? ????????????????holder?=?(ViewHolder)convertView.getTag();?? ????????????}????????? ?????????????? ????????????holder.title.setText((String)mData.get(position).get("title"));?? ????????????holder.info.setText((String)mData.get(position).get("info"));?? ????????????holder.viewBtn.setTag(position);?? ?????????????? ????????????holder.viewBtn.setOnClickListener(new?View.OnClickListener()?{?? ?????????????????? ????????????????@Override?? ????????????????public?void?onClick(View?v)?{?? ????????????????????showInfo(position);??????????????????? ????????????????}?? ????????????});?? ?????????????? ?????????????? ?????????????????????? ????????????return?convertView;?? ????????}?? ????}?? ?????????? ?????? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?????? ?????? ?????? ?????? ?????? ?????? ?????? ?????? ?????? ????public?final?class?ViewHolder?{?? ????????public?TextView?title;?? ????????public?TextView?info;?? ????????public?Button?viewBtn;?? ????}?? ????public?void?showInfo(int?position){?? ?????????? ????????ImageView?img=new?ImageView(ListViewActivity.this);?? ????????img.setImageResource(R.drawable.b);?? ????????new?AlertDialog.Builder(this).setView(img)?? ????????.setTitle("詳情"+position)?? ????????.setMessage("菜名:"+title[position]+"???價格:"+info[position])?? ????????.setPositiveButton("確定",?new?DialogInterface.OnClickListener()?{?? ????????????@Override?? ????????????public?void?onClick(DialogInterface?dialog,?int?which)?{?? ????????????}?? ????????})?? ????????.show();?? ????}?? ?????? ?????? }??
2.main.xml
[html]?view plaincopy
<?xml?version="1.0"?encoding="utf-8"?>?? <LinearLayout?xmlns:android="http://schemas.android.com/apk/res/android"?? ????android:layout_width="fill_parent"?? ????android:layout_height="fill_parent"?? ????android:orientation="vertical"?>?? ?? ????<ListView??? ????????android:id="@+id/listView"?? ????????android:layout_width="fill_parent"?? ????????android:layout_height="fill_parent"?? ????????android:divider="@drawable/list_line"?? ????????android:dividerHeight="1dip"?/>?? ?? </LinearLayout>??
3.vlist.xml
[html]?view plaincopy
<?xml?version="1.0"?encoding="utf-8"?>?? <LinearLayout?xmlns:android="http://schemas.android.com/apk/res/android"?? ????android:layout_width="fill_parent"?? ????android:layout_height="fill_parent"?? ????android:orientation="vertical"?>?? ?? ????<ListView??? ????????android:id="@+id/listView"?? ????????android:layout_width="fill_parent"?? ????????android:layout_height="fill_parent"?? ????????android:divider="@drawable/list_line"?? ????????android:dividerHeight="1dip"?/>?? ?? </LinearLayout>??
4.btn_detail_selecter.xml
[html]?view plaincopy
<?xml?version="1.0"?encoding="utf-8"?>?? <selector?xmlns:android="http://schemas.android.com/apk/res/android">?? ?? ????<item?android:drawable="@drawable/btn_detail_normal"?android:state_enabled="true"?android:state_focused="false"?android:state_pressed="false"/>?? ????<item?android:drawable="@drawable/btn_detail_pressed"?android:state_enabled="true"?android:state_pressed="true"/>?? ????<item?android:drawable="@drawable/btn_detail_pressed"?android:state_enabled="true"?android:state_focused="true"/>?? ?? </selector>??
5.item.xml
[html]?view plaincopy
<?xml?version="1.0"?encoding="UTF-8"?>?? <selector?xmlns:android="http://schemas.android.com/apk/res/android">?? ?? ????<item?android:drawable="@drawable/item_higlight"?android:state_focused="true"?android:state_pressed="false"/>?? ????<item?android:drawable="@drawable/item_higlight"?android:state_focused="false"?android:state_pressed="true"/>?? ????<item?android:drawable="@drawable/item_higlight"?android:state_selected="true"/>?? ????<item?android:drawable="@drawable/item_higlight"?android:state_focused="true"/>?? ????<item?android:drawable="@drawable/item_higlight"/>?? ?? </selector>??
6.colors.xml
[html]?view plaincopy
<?xml?version="1.0"?encoding="UTF-8"?>?? <resources>?? ????<color?name="region">#8000ff00</color>?? ????<color?name="listTitle">#ff23323b</color>?? ????<color?name="text">#ff848f9b</color>?? ????<color?name="write">#ffffffff</color>?? </resources>??
7.values.xml
[html]?view plaincopy
<?xml?version="1.0"?encoding="utf-8"?>?? <resources>?? ?? ????<string?name="hello">Hello?World,?ListViewActivity!</string>?? ????<string?name="app_name">ListView</string>?? ?? </resources>??
8.drawables.xml
[html]?view plaincopy
<?xml?version="1.0"?encoding="UTF-8"?>?? <resources>?? ????<item?type="drawable"?name="bg">#80000000</item>?? ????<item?type="drawable"?name="transparent">#00000000</item>?? ????<item?type="drawable"?name="lightblue">#ffcfe1ed</item>?? ????<item?type="drawable"?name="readmenu_btn_bg_f">#30ffffff</item>?? ????<item?type="drawable"?name="readmenu_btn_bg_p">#50ffffff</item>?? ????<item?type="drawable"?name="blackMask">#30000000</item>?? </resources>??
《新程序員》:云原生和全面數字化實踐50位技術專家共同創作,文字、視頻、音頻交互閱讀
總結
以上是生活随笔為你收集整理的为ListView每个Item上面的按钮添加事件的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。