2012-03-13 34 views
0

我有Android的列表視圖[TextView TextView ImageButton]。我通過使用ArrayAdapter實現列表視圖。根據我的要求,我需要單擊和長按事件列表視圖,以便我使用registerForContextMenu(getListView())長按和單按我使用onListItemClick這工作正常。之後,另外我需要列表視圖上的imageButton onClick事件,併爲imageButton實現onclick。單擊和長按不工作後在listView中實現imageButton的onclick監聽器在Android

現在我的問題是單擊新聞事件和長按事件不工作後onClickView.But ImageButton實現onClick監聽器onClick監聽器工作正常。

我添加屬性機器人:可調焦=「假」到ImageButton的,但它並不能幫到我。

如何使用onclick偵聽器爲ImageButton獲取單次和長按事件?請求幫助我。

group.xml:

...... 
<ListView 
    android:id="@id/android:list" 
    android:focusable="true" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" > 
</ListView> 
..... 

group_row.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" > 
    <TextView 
     ...... 
     android:focusable="false" /> 
    <ImageButton 
     android:id="@+id/group_row_gadd" 
     ..... 
     android:focusable="false" 
     android:background="@drawable/add" /> 
    <TextView 
     android:id="@+id/group_row_grow" 
     .... 
     android:focusable="false" /> 
</RelativeLayout> 

我一個ArrayAdapter類:

public class GroupAdapter extends ArrayAdapter<String> implements OnClickListener{ 
    ....  

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 

     LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     View rowView = inflater.inflate(R.layout.group_row, parent, false); 

     name = (TextView) rowView.findViewById(R.id.group_row_gname); 
     number = (TextView) rowView.findViewById(R.id.group_row_grow); 
     add = (ImageButton) rowView.findViewById(R.id.group_row_gadd); 
     add.setTag(position); 
     add.setOnClickListener(this); 

     return rowView; 
    } 

    public void onClick(View v) { 
     Log.e("onclick", "onclick"); 
     Integer position = (Integer) v.getTag(); 
     switch (v.getId()) { 
     case R.id.group_row_gadd: 

      break; 
     } 

    } 

} 

mainclass:

public class Group extends ListActivity{ 
    ..... 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.group); 
     registerForContextMenu(getListView()); 
     ..... 
    }// OnCreate End 

    @Override 
    protected void onListItemClick(ListView l, View v, int position, long id) { 
     ..... 
    } 

    @Override 
    public void onCreateContextMenu(ContextMenu menu, View v,ContextMenuInfo menuInfo) { 
     super.onCreateContextMenu(menu, v, menuInfo); 
     MenuInflater inflater = getMenuInflater(); 
     inflater.inflate(R.menu.group_edit_remove, menu); 
    } 

    @Override 
    public boolean onContextItemSelected(MenuItem item) { 
     AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo(); 
     ..... 
     return false; 
    } 

} 

回答

0

我猜你還需要設置android:focusableInTouchMode="false"到您的視圖元素group_row.xml

+0

我試過你的答案,但不工作請幫我 – 2012-03-13 08:09:37

0

因爲ImageButton的不能獲得焦點。在ListView上設置OnItemClickListener,ListView的項目將成爲獲得焦點的第一優先級。所以當您單擊ImageButton時,它將無法工作。您可以修改您的GroupAdapter。

+0

請解釋我。什麼修改適配器類? – 2012-03-13 07:54:42

+0

我有onItemClickListener。請參閱編輯的問題。請幫幫我。 – 2012-03-13 08:07:13

相關問題