2013-01-18 175 views
2

尝试为ListView创建点击处理程序,但列表不响应点击。
看了很多当地的答复,但没有帮助我:(onListItemClick不适用于自定义ListView

代码:

public class MainActivity extends SherlockListActivity { 

private ArrayList<Order> listItems; 
private myAdapter myAdapter; 
ListView lv; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

lv = getListView(); 
lv.setTextFilterEnabled(true); 
this.myAdapter = new myAdapter(this, R.layout.list_item, listItems); 
setListAdapter(myAdapter); 
.... 
} 

protected void onListItemClick(ListView l, View v, int position, long id) { 
    super.onListItemClick(l, v, position, id); 

    Toast.makeText(getApplicationContext(), "Clicked " + l.getItemAtPosition(position), Toast.LENGTH_SHORT).show();   
} 

布局
主营:

<ListView 
    android:id="@android:id/list" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:fastScrollEnabled="true"   
    android:listSelector="@drawable/list_selector" 
    android:smoothScrollbar="true" 
    android:textColorHighlight="@android:color/transparent" 
    android:background="#000000" > 
</ListView> 

行:

<ImageView 
    android:id="@+id/image" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:clickable="false" 
    android:focusable="false" 
    android:focusableInTouchMode="false" 
    android:paddingBottom="5dp" 
    android:paddingLeft="5dp" 
    android:paddingRight="5dp" 
    android:paddingTop="5dp" 
    android:src="@drawable/logo" /> 

<TextView 
    android:id="@+id/text" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentTop="true" 
    android:layout_toRightOf="@+id/image" 
    android:clickable="false" 
    android:focusable="false" 
    android:focusableInTouchMode="false" 
    android:paddingBottom="5dp" 
    android:text="Название" 
    android:textColor="@android:color/black" 
    android:textSize="18dp" /> 

<TextView 
    android:id="@+id/text2" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignLeft="@+id/text" 
    android:layout_below="@+id/text" 
    android:clickable="false" 
    android:focusable="false" 
    android:focusableInTouchMode="false" 
    android:paddingBottom="5dp" 
    android:text="Эпизод, время" 
    android:textSize="14dp" /> 

感谢您的帮助,
Regards

+0

你在list_item里面有按钮吗? – Blackbelt

+0

没有按钮,只有2个textview和1个imageview –

+0

是那些可点击或可聚焦的?我记得有类似的问题。尝试把android:focusable =“false” – Blackbelt

回答

2

在适配器中尝试setOnClickListener。由于您拥有ImageView,因此OnListItemClick不起作用。 onListItemClick is not getting called on ListActivity

+0

谢谢,它的工作。我知道'getView()'会工作,但我想在'MainActivity'中实现。但我不知道它阻止了图像。谢谢你提供有用的信息 –

2

如果你想让你的类响应事件,你的类必须实现OnItemClickListener,然后将它分配给你的列表list.setOnItemClickListener(this);

+0

我试了很多次,不行。 –

相关问题