自定义ListView(带有CheckBox)不响应点击 [英] Custom ListView (with CheckBox) not responding to clicks

查看:81
本文介绍了自定义ListView(带有CheckBox)不响应点击的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用自己的ArrayAdapter版本创建了一个自定义ListView.我的问题是它没有响应任何onItemClickListeners.这是我的代码段:

I've created a custom ListView using my own version of ArrayAdapter. My problem is that it is not responding to any onItemClickListeners. Here are my code snippets:

final ArrayAdapter<Agency> aa=new myCustomAdapter();
    lv.setAdapter(aa);

    lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View myView, int pos,
                long id) {
            TextView clickedTV=(TextView)myView.findViewById(R.id.rowtext1);
            Toast.makeText(getBaseContext(), clickedTV.getText().toString(), Toast.LENGTH_LONG).show();


        }
    });

适配器实现:

private class myCustomAdapter extends ArrayAdapter<Agency> {

    public myCustomAdapter() {
        super(getBaseContext(), R.layout.layout_row, AgencyList);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View itemView=convertView;
        if (itemView==null)
            itemView=getLayoutInflater().inflate(R.layout.layout_row, parent, false);
        TextView rowText=(TextView)itemView.findViewById(R.id.rowtext1);
        Agency currentAgency=AgencyList.get(position);
        rowText.setText(currentAgency.getAgency().toString());
        return itemView;
    }   
}

我为每行创建了一个自定义xml布局,然后在我的适配器中进行了膨胀.自定义行布局具有TextView和一个CheckBox:

I've created a custom xml layout for each row then inflated in my adapter. The custom row layout has TextView and a CheckBox:

    <?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="fill_parent" >

    <TextView
        android:id="@+id/rowtext1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="23dp"
        android:layout_marginTop="21dp"
        android:text="Row Item"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <CheckBox
        android:id="@+id/rowcheckBox1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/rowtext1"
        android:layout_alignBottom="@+id/rowtext1"
        android:layout_alignParentRight="true"
        android:layout_marginRight="26dp"
        android:text="Select this" />

</RelativeLayout>

我不能在listView中有一个可点击的行,那里已经有一个可点击的小部件(如复选框)吗?我已经在许多电话中看到了这种实现(例如,Sony的Timescape UI). 请提出解决方法!感谢您的帮助!

Can't I have a clickable row in a listView where there's already a clickable widget (like checkboxes)? I've seen this implementation in many phones (Sony's Timescape UI for example). Please suggest a work around! Thanks for your help!

推荐答案

必须确保每当不触发执行Custom ListView和onItemClick事件时,都必须为自定义布局UI元素设置此属性.因此,为TextView和Checkbox设置

Must be sure whenever you performing with Custom ListView and onItemClick event not be triggered you have to set this properties for your custom layout UI elements. So set this for TextView and Checkbox

android:focusable="false"
android:focusableInTouchMode="false" 

这篇关于自定义ListView(带有CheckBox)不响应点击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆