In Android, clicking the listview button and clicking the button inside cannot take effect at the same time. Problem Solving

Source: Internet
Author: User

Today I encountered a problem:In Android, clicking listview and clicking button inside cannot take effect at the same time.

The reason is:

When the listview starts to draw, the system first calls the getcount () function to obtain the length of the listview based on its return value (this is why the length of the list is marked with the first graph specific to the START ), call getview () to draw each row one by one based on the length. If the returned value of getcount () is 0, the return 1 is not displayed in the list, and only one row is displayed.

When the system displays the list, an adapter is first instantiated (the custom adapter will be instantiated here ). When manual adaptation is completed, data must be manually mapped. You need to override the getview () method. This method is called when every row in the list is drawn. Getview () has three parameters. Position indicates the row to be displayed, and covertview indicates the layout from the layout file. We use the layoutinflater method to extract the defined vlist2.xml file into a view instance for display. Then, instantiate each component in the XML file (simple findviewbyid () method ). In this way, the data can be mapped to each component. However, to respond to a click event, you need to add a click listener for the button to capture the click event. So far, a custom listview is complete. Now let's look back at this process. The system wants to draw the listview. He first obtains the length of the list to be drawn, and then begins to draw the first line. How can he draw it? Call the getview () function. In this function, first obtain a view (actually a viewgroup), then instance and set each component to display it. Now, we have drawn this line. That
Draw the next line until the painting is complete. In the actual running process, it will be found that each row of the listview has no focus, because the button grabs the focus of the listview, as long as the layout file sets the button to no focus, it is OK.

 

In addition, you cannot add both listview and imageview listeners by using this method. In the web search, some attributes are not added to XML, and the XML root node in list configuration cannot be added.Android: descendantfocusability = "blocksdescendants"And addAndroid: clickable = "true"
Android: focusable = "false"

 

Listview. setonitemclicklistener (New onitemclicklistener (){

Public void onitemclick (adapterview <?> Adapter, view arg1, int position,
Long arg3 ){
Adapter. getitematposition (position); // you can call this operation to obtain the data of the currently clicked item.
}
});

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.