OnItemClickListener和OnClickListener在ListView上无法正常工作

12

我使用了自定义列表视图,并且正在使用相同的列表视图显示一些数据。

当我单击列表视图项时,onClickListener没有被调用。我无法选择任何列表项。

布局代码:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_marginBottom="16dp"
android:background="@drawable/list_selector"
android:clickable="true"
android:orientation="horizontal"
android:padding="5dip" >

<LinearLayout
    android:id="@+id/imgProperty"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_marginRight="5dip"
    android:padding="3dip" >

    <ImageView
        android:id="@+id/list_image"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:contentDescription="@string/app_name"
        android:src="@drawable/ic_launcher" 
        android:focusable="false"/>
</LinearLayout>

<TextView
    android:id="@+id/tvCity"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerVertical="true"
    android:layout_marginLeft="75dip"
    android:layout_toRightOf="@+id/list_image"
    android:paddingBottom="10dip"
    android:text="property"
    android:textColor="#040404"
    android:textSize="15sp"
    android:textStyle="bold"
    android:typeface="sans" />

<TextView
    android:id="@+id/tvprice"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@+id/imgProperty"
    android:layout_alignLeft="@+id/tvCity"
    android:text="Price" 
    android:focusable="false"/>

</RelativeLayout>

适配器代码:

 public class CustomListAdapter extends BaseAdapter {

 ArrayList<Propety> PropertiesArray;
private LayoutInflater Inflater;


public CustomListAdapter(ArrayList<Propety> PropertiesArray) {  

   this.PropertiesArray=PropertiesArray;

}
@Override
public int getCount() {
    // TODO Auto-generated method stub
    return PropertiesArray.size();
}

@Override
public Object getItem(int position) {

    return PropertiesArray.get(position);
}

@Override
public long getItemId(int position) {

    return position;
}

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

     if (convertView == null) {
            LayoutInflater inflater = LayoutInflater.from(parent.getContext());
            convertView = inflater.inflate(R.layout.customlistview, parent, false);
        }


        final Propety ListArray = PropertiesArray.get(position);

       TextView tvPropertyName = (TextView) convertView.findViewById(R.id.tvCity);
       tvPropertyName.setText(ListArray.getName());

        TextView tvPrice = (TextView) convertView.findViewById(R.id.tvprice);
        tvPrice.setText(ListArray.getPrice());

       ImageView imgProperty = (ImageView) convertView.findViewById(R.id.list_image);
       imgProperty.setImageResource(R.drawable.ic_launcher);


       convertView.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

             Toast.makeText(parent.getContext(), "view clicked: " + ListArray.getName(), Toast.LENGTH_SHORT).show();
        }
    });


    return convertView;
}

}

ListView 代码:

 ListView propertylistview = (ListView) findViewById(R.id.listview);
CustomListAdapter customlistview=new CustomListAdapter(PropertiesArray);
propertylistview.setAdapter(customlistview);

ListView XML:

 <ListView
    android:id="@+id/listview"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/customview"
    android:layout_marginBottom="10dp"
    android:layout_marginLeft="10dp"
    android:layout_marginRight="10dp"
    android:layout_marginTop="24dp"
    android:background="@drawable/list_selector"
    android:textAlignment="center" >

自定义.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >

<SurfaceView
    android:id="@+id/surface"
    android:layout_width="fill_parent"
    android:layout_height="match_parent" />

<TextView
    android:id="@+id/txtangle"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:layout_marginBottom="115dp"
    android:layout_marginLeft="95dp"
    android:text="" />

<ListView
    android:id="@+id/listview"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/customview"
    android:layout_marginBottom="10dp"
    android:layout_marginLeft="10dp"
    android:layout_marginRight="10dp"
    android:layout_marginTop="24dp"
    android:background="@drawable/list_selector"
    android:textAlignment="center" >

</ListView>

<view
    android:id="@+id/customview"
    android:layout_width="110dp"
    android:layout_height="110dp"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    class="com.example.samplebuapp.CustomCompass" />

<view
    android:id="@+id/view1"
    android:layout_width="110dp"
    android:layout_height="110dp"
    android:layout_above="@+id/listview"
    android:layout_alignParentRight="true"
    android:layout_marginRight="18dp"
    class="com.example.samplebuapp.CustomView" />

<LinearLayout
    android:id="@+id/rl1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/listview"
    android:orientation="vertical" 
    android:focusable="false">
</LinearLayout>

</RelativeLayout>

甚至滚动也无法使用。

我无法弄清楚为什么会发生这种情况?我是不是漏掉了什么?

感谢任何帮助解决此问题的支持。


也许这可以帮助:http://www.thepcwizard.in/2012/09/android-creating-custom-listview-for.html - ThePCWizard
似乎触摸事件没有传递到ListView。您的布局文件中是否有任何与ListView一起阻止触摸传播的内容? - Aswin Rajendiran
我正在显示ListView以及其他自定义视图和SurfaceView。请查看custom.xml文件。 - Mahe
6个回答

10
以下内容将在您的情况下完成工作。
ListView propertylistview = (ListView) findViewById(R.id.listview); 
    propertylistview.setOnItemClickListener(  myListViewClicked ):

        OnItemClickListener myListViewClicked = new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Toast.makeText(YourActivity.this, "Clicked at positon = " + position, Toast.LENGTH_SHORT).show();

            }
        };
不要忘记从CustomAdapter中删除以下内容。
  convertView.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {

         Toast.makeText(parent.getContext(), "view clicked: " + ListArray.getName(), Toast.LENGTH_SHORT).show();
    }
});

谢谢@Ninja。那个方法不起作用。我正在SurfaceView上显示ListView。在SurfaceView上显示ListView会引起任何问题吗? - Mahe
1
你在声明 myListViewClicked 之前就使用它了吗? - Duncan Jones
通过首先将OnItemClickListener分配给一个变量(然后传递到setOnItemClickListener中),神奇地解决了我的问题。之后,当我不必分配给变量时,它仍然有效。 - Ken Ratanachai S.

3
如果触摸事件在单元格布局内被拦截,那么在自定义单元格的布局中,在单元格的顶层布局上设置android:descendantFocusability="blocksDescendants"。对于我来说,在每个视图上添加XML属性太麻烦了。
请注意,这也可以在代码中设置:cell.setDescendantFocusability(FOCUS_BLOCK_DESCENDANTS); 我还尝试将此方法用于单元格内的ListView,并且必须重写onTouchEvent才能使其正常工作:
public class NoTouchListView extends ListView {

    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        return false;
    }
}

2

这个简单的代码解决了我的问题,方法view.setOnClickListener需要放在我的自定义适配器里面。

    view.setFocusable(false)

2

请检查您的列表行布局:

布局或任何视图不应该设置为 `android:clickable="true" 和 android:focusable="true"`。

如果存在,请删除并重新运行应用程序。


1

1
即使我也遇到了同样的问题,我有一个复选框,按照以下步骤使itemClickListener工作起来,
将以下属性添加到复选框中,
android:focusable="false"
android:focusableInTouchMode="false"
android:clickable="false"

并且ItemClickListner开始工作。

如果需要详细的示例,您可以访问以下链接:

http://knowledge-cess.com/android-itemclicklistner-with-checkbox-or-radiobutton/

希望它有所帮助,干杯!

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接