In Android, onitemclicklistener does not work. Solution

Source: Internet
Author: User

Recently I wrote a project on which frame layout is used. A listview display contact is placed on the interface and a letter index on the right of the contact is located. When onitemclicklistener is set for the contact listview, it does not work.

The following are layout files and settings.Code

< Framelayoutandroid: ID = "@ + ID/contact_fl" Android: layout_width = "Match_parent" Android: layout_height = "Match_parent"> < Listviewandroid: ID = "@ + ID/contact_lv" Android: layout_width = "Match_parent" Android: layout_height = "Match_parent" Android: layout_weight = "1.0" Android: focusable = "True" Android: focusableintouchmode = "True" Android: dividerheight = "1px" Android: scrollbars = "NONE"/> < Linearlayoutandroid: layout_width = "28dp" Android: layout_height = "Match_parent" Android: layout_gravity = "Right | Center" Android: layout_marginbottom = "6dp" Android: layout_margintop = "10dp" Android: gravity = "Right" Android: Orientation = "Vertical"> < Listviewandroid: ID = "@ + ID/contact_letter" Android: layout_width = "28dp" Android: layout_height = "Match_parent" Android: layout_gravity = "Center_horizontal" Android: focusable = "True" Android: focusableintouchmode = "True" Android: scrollbars = "NONE"/> </linearlayout> </framelayout>

Item layout File

View code

< Imageviewandroid: scaletype = "Centerinside" Android: layout_marginleft = "12dp" Android: layout_margintop = "5dp" Android: layout_marginright = "4dp" Android: layout_marginbottom = "3dp" Android: layout_width = "45dp" Android: layout_height = "45dp" Android: SRC = "@ Drawable/ic_contact_picture" Android: ID = "@ + ID/contact_contactinfoitem_iv_photo"/> < Textview Android: singleline = "True"Android: ellipsize = "Marquee" Android: textstyle = "Bold" Android: marqueerepeatlimit = "Marquee_forever" Android: layout_height = "Wrap_content" Android: layout_width = "Match_parent" Android: layout_weight = "1.0" Android: ID = "@ + ID/contact_contactinfoitem_ TV _name" Android: Text = "XXX"/> < Textview Android: singleline = "True" Android: ellipsize = "Marquee"Android: textstyle = "Bold" Android: marqueerepeatlimit = "Marquee_forever" Android: layout_height = "Wrap_content" Android: layout_width = "Match_parent" Android: layout_weight = "1.0" Android: ID = "@ + ID/contact_contactinfoitem_ TV _phone" Android: Text = "XXX"/>

Set onitemcllicklistener in Activity

Lv_contact.setonitemclicklistener (NewOnitemclicklistener () {@ overridePublic VoidOnitemclick (adapterview <?>Parent, view,IntPosition,LongID) {log. I ("My", "onitemclick clicked");}});

Check and find that in ontouchlistener, the returned result is false, not true, which means that the screen event will continue to be transmitted and will not affect the onitemclicklistener processing.

I checked it online and found that there is a focus attribute in XML that will affect ontouchlistener. I need to change it to false.

Check the XML file again. These two attributes are indeed set in it.

 
Android: focusable = "true"Android: focusableintouchmode= "True"

The names of these two attributes can be understood,

Android: focusable = "true "-------

Set whether to obtain the focus. If requestfocus () is called, the latter takes priority. Note: If you want to set a focal point such as edittext in the form, this setting cannot be used.
You must set the focusable before the edittext to false. You need to set the focus to be obtained in touch mode.
The focusableintouchmode is true.

Android: focusableintouchmode = "true "----
Set whether the view can get the focus in touch mode.
Modify the attribute in XML to "false". Run the project and find that the same onitemcllicklistener does not work.

Because of the rush of time, simply write the onclicklistener of the item in getview In the adapter. The Code is as follows:

 
Contact_fl.setonclicklistener (NewOnclicklistener () {@ overridePublic VoidOnclick (view) {log. I ("My", "onclick clicked");}});

Run the project and click the item. But there are new problems at the same time. In my ontouchlistener type, the onkeydown and onkeyup events disappear ....
Only onkeymove.

After analysis, it is concluded that there must be methods or attributes that conflict with each other.
Since there is no space such as a button in the layout file of item. Check whether there are methods or properties that make the Click Event disappear.

Check the adapter again and find two methods:

@ OverridePublic BooleanAreallitemsenabled (){//All items are SeparatorReturn False;} @ OverridePublic BooleanIsenabled (IntPosition ){//All items are SeparatorReturn False;}

View description:

Returns true if the item at the specified position is not a separator. (A separator is a non-selectable, non-clickable item).
Result is unspecified if position is invalid. An arrayindexoutofboundsexception shocould be thrown in that case for fast failure.

This means that if the specified position is not a separator (the separator is an item that cannot be selected or clicked), true is returned.

So hurry up and change to "true" to run the project and get the results.

 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.