2014-03-07 30 views
0

我想製作一個自定義ListView作爲項目的按鈕。現在,我使用的是OnItemClickListener併爲每次點擊,敬酒的消息出現,但只來了,如果我按了按鈕,但是在ListView(見圖片)自定義列表視圖與按鈕作爲項目OnItemClickListener無線

enter image description here

如何我能做到嗎,敬酒來了,如果我按下按鈕,而不是他們。

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" /> 

+0

發表你的代碼你試過的東西 – CoolMonster

+0

**吐司消息出現了,但它只會出現,如果我按下按鈕,但在ListView(見圖片)。**什麼是實際意思是這個 – Piyush

+0

我在我的智能手機上,我沒有這裏的代碼。我會發布它時,我是@home在電腦上 – user3178507

回答

0

在你Adupter類作出按鈕兩個對象。

而setonclicklistner用於adupter視圖

例如按鈕

 vi = inflater.inflate(R.layout.list_row_contact, null); 


    LinearLayout ll_adpt_main = (LinearLayout)vi.findViewById(R.id.ll_adpt_main); 
    TextView name = (TextView) vi.findViewById(R.id.name); 
    TextView discription = (TextView) vi.findViewById(R.id.number); 
    ImageView iv = (ImageView)vi.findViewById(R.id.iv); 





    iv.setOnClickListener(new OnClickListener() { 

     @Override 
     public void onClick(View arg0) { 
      // TODO Auto-generated method stub 

     } 
    }); 





    return vi; 
} 
+0

它可能在評論 –

+0

謝謝,我會盡快發佈適配器的代碼 – user3178507

+0

好吧,我明白了,謝謝兄弟。我應該發佈最終代碼嗎?:o – user3178507

0

爲按鈕設置一個單獨的onclicklistener。 OnItemclicklistener用於列表項目。

+0

謝謝,但您可以請張貼一些代碼? – user3178507

0

首先,你必須創建一個這樣的XML佈局。

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

    <Button 
     android:id="@+id/btn" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     /> 
</LinearLayout> 

然後創建一個適配器類:

class MyAdapter extends ArrayAdapter<View.OnClickListener> { 
    public MyAdapter(Context context, int textViewResourceId, View.OnClickListener[] objects) { 
     super(context, textViewResourceId, objects); 
    } 
    public MyAdapter(Context context, int resource, int textViewResourceId, List<View.OnClickListener> objects) { 
     super(context, resource, textViewResourceId, objects); 
    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     convertView = inflater.inflate(R.layout.event_layout, null); 

     Button b = (Button) convertView.findViewById(R.id.btn); 

     View.OnClickListener l = getItem(position); 
     b.setOnClickListener(l); 

     return convertView; 
    } 
} 

活動的佈局應該是這樣的:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/LayoutActivity" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    tools:context=".YourActivityName" > 
     <Button 
      android:id="@+id/btnHeader1" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
     /> 
     <Button 
      android:id="@+id/btnHeader2" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
     /> 

     <ListView 
      android:id="@+id/your_list" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content"> 
     </ListView> 
</LinearLayout> 

我們設置列表項:

ListView list = (ListView)findViewById(R.id.your_list); 

View.OnClickListener[] listeners = ....; 

MyAdapter adapter = new MyAdapter(context, R.layout.your_list_layout_adapter, listeners); 
list.setAdapter(adapter); 

我希望能夠滿足你所有的r equests :)

相關問題