Listview 中未触发 Onitemclick 侦听器

Posted

技术标签:

【中文标题】Listview 中未触发 Onitemclick 侦听器【英文标题】:Onitemclick listener not triggered in Listview 【发布时间】:2013-08-22 01:22:26 【问题描述】:

我有一个列表视图,我正在为该列表视图使用 OnItemClickListener。同时,在我的自定义适配器的getview方法中,我对整个行视图使用onclicklistener。我发现在自定义适配器中使用Onclicklistener时,我的listview的OnItemClickListener没有被触发。我想同时使用这两个监听器。任何人都可以帮我做吗?提前致谢。

我的列表视图监听器,

list_specialists.setOnItemClickListener(new OnItemClickListener() 

            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) 

                HashMap<String, String> trends_data= new HashMap<String, String>();
                trends_data = specobj.get(position);
                specialist.setText(trends_data.get(KEY_SPEC_TITLE));

                new GetTime(BookingActivity.this).execute();


                
            );

还有我在自定义适配器中的 getview 方法,

@Override
public View getView(final int position, View convertView, ViewGroup parent) 
    LayoutInflater inflater = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View rowView = convertView;
    final ViewHolder holder;

    if (rowView == null) 
        rowView = inflater.inflate(R.layout.row_timeslot, parent, false);
        holder = new ViewHolder();
        holder.title = (TextView) rowView.findViewById(R.id.textView2);
        holder.layout = (RelativeLayout) rowView
                .findViewById(R.id.relativeLayout1);
        holder.check = (org.holoeverywhere.widget.CheckBox) rowView
                .findViewById(R.id.checkBox1);

        rowView.setTag(holder);
     else 
        holder = (ViewHolder) rowView.getTag();
    

    HashMap<String, String> trends_data = new HashMap<String, String>();
    trends_data = trendsobj.get(position);

    holder.check.setId(position);

    holder.check.setChecked(checkarry[position]);

    // for managing the state of the boolean
    // array according to the state of the
    // CheckBox

    holder.check.setOnClickListener(new View.OnClickListener() 

        public void onClick(View v) 

            for (int i = 0; i < checkarry.length; i++) 
                if (position == i) 
                    checkarry[i] = true;
                 else 
                    checkarry[i] = false;
                

                RefreshList();
            
        
    );

    holder.layout.setOnClickListener(new View.OnClickListener()  // Here I am using onclicklistener for rows's parent layout.

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




            for (int i = 0; i < checkarry.length; i++) 
                if (position == i) 
                    checkarry[i] = true;
                 else 
                    checkarry[i] = false;
                

                RefreshList();
            



        
    );

    holder.title.setText(trends_data.get(BookingActivity.KEY_TIME_TITLE));



    return rowView;

行布局xml文件...

    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/relativeLayout1"
    android:layout_
    android:layout_
    android:layout_margin="10dp"
    android:focusable="false"
    android:background="@drawable/list_selector"
    android:orientation="vertical"
    android:padding="16dp" >

    <TextView
        android:id="@+id/textView3"
        android:layout_
        android:layout_
        android:layout_centerVertical="true"
        android:layout_marginLeft="10dp"
        android:layout_toRightOf="@+id/activity_googlecards_card_imageview"
        android:text="Rate "
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textColor="#757572" />

    <TextView
        android:id="@+id/TextView01"
        android:layout_
        android:layout_
        android:layout_centerVertical="true"
        android:layout_toRightOf="@+id/textView3"
        android:text="AED 200.00"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textColor="@color/mainservice" />

    <TextView
        android:id="@+id/textView2"
        android:layout_
        android:layout_
        android:layout_alignTop="@+id/activity_googlecards_card_imageview"
        android:layout_marginLeft="5dp"
        android:layout_toRightOf="@+id/activity_googlecards_card_imageview"
        android:gravity="center"
        android:text="Specialist Name"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textColor="#6945B2"
        android:textStyle="bold" />

    <CheckBox
        android:id="@+id/checkBox1"
        android:layout_
        android:layout_
        android:layout_alignBottom="@+id/activity_googlecards_card_imageview"
        android:layout_alignParentRight="true"
        android:focusable="false"
        android:focusableInTouchMode="false" />

    <ImageView
        android:id="@+id/activity_googlecards_card_imageview"
        android:layout_
        android:layout_
        android:layout_alignParentLeft="true"
        android:layout_centerVertical="true"
        android:scaleType="centerCrop"
        android:src="@drawable/list_dummy" />

</RelativeLayout>*

【问题讨论】:

您是否将所有 ListRow 视图设置为 android:Focusable = fale..?? 据我所知,这是不可能的。 @SilentKiller 不工作.. 我是否需要在父级之上添加其他布局,并为该布局设置 onclick 监听器? @BasimSherif Biradar 您不需要将相对(父)布局可聚焦设置为 false。让孩子们集中注意力是错误的。 【参考方案1】:

如果列表行中有任何可点击项,则将执行其 click() 操作,而不是 onItemClickListener。这里行项中有一个checkbox,如果还想使用onItemClickListener,就必须让CheckBox在布局中不可聚焦,如下:

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

【讨论】:

@Basim Sherif,去掉RelativeLayout的focusable:false

以上是关于Listview 中未触发 Onitemclick 侦听器的主要内容,如果未能解决你的问题,请参考以下文章

Android ListView onItemClick Not Work

OnItemClick 永远不会被调用 ListView 中的按钮

ListView:textIsSelectable 和 onItemClick

当ListView有Header时,onItemClick里的position不正确

从片段中ListView的onItemClick开始Activity

Android ListView 在 onItemClick 之后修改