我想製作一個自定義ListView
作爲項目的按鈕。現在,我使用的是OnItemClickListener
併爲每次點擊,敬酒的消息出現,但只來了,如果我按了按鈕,但是在ListView(見圖片)自定義列表視圖與按鈕作爲項目OnItemClickListener無線
如何我能做到嗎,敬酒來了,如果我按下按鈕,而不是他們。
Adaptercode:
public class MainListAdapter extends ArrayAdapter<Games> {
Context context;
int layoutResourceId;
Games data[] = null;
public MainListAdapter(Context context, int layoutResourceId, Games[] data) {
super(context, layoutResourceId, data);
this.layoutResourceId = layoutResourceId;
this.context = context;
this.data = data;
}
@Override
public View getView(int position, View convertView, ViewGroup parent){
View row = convertView;
ListHolder holder = null;
if(row == null){
LayoutInflater inflater = ((Activity)context).getLayoutInflater();
row = inflater.inflate(layoutResourceId, parent, false);
holder = new ListHolder();
holder.btn = (Button)row.findViewById(R.id.listViewButton);
row.setTag(holder);
}else{
holder = (ListHolder)row.getTag();
}
Games games = data[position];
holder.btn.setText(games.name);
return row;
}
static class ListHolder {
Button btn;
}
}
在Acitivty:
MainListAdapter adapter = new MainListAdapter(this, R.layout.listview_item_row, games_data);
listView1 = (ListView)findViewById(R.id.listView1);
final View header = (View)getLayoutInflater().inflate(R.layout.listview_newgame_row, null);
listView1.addHeaderView(header);
listView1.setAdapter(adapter);
listView1.setOnItemClickListener(new OnItemClickListener(){
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
Toast.makeText(getApplicationContext(), "clicked", Toast.LENGTH_LONG).show();
}
});
listview_item_row.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:id="@+id/listViewButton"
android:layout_width="match_parent"
android:layout_height="70dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:focusable="false"
android:background="@drawable/button" />
發表你的代碼你試過的東西 – CoolMonster
**吐司消息出現了,但它只會出現,如果我按下按鈕,但在ListView(見圖片)。**什麼是實際意思是這個 – Piyush
我在我的智能手機上,我沒有這裏的代碼。我會發布它時,我是@home在電腦上 – user3178507