When you click ViewGroup, its sub-controls also become pressed. The Cause Analysis and Solution are as follows: viewgrouppressed

Source: Internet
Author: User

When you click ViewGroup, its sub-controls also become pressed. The Cause Analysis and Solution are as follows: viewgrouppressed

This problem should have been analyzed at the beginning when analyzing touch event processing. However, I thought this code was not so close to touch's topic at the time,

I ignored it until I encountered a problem on it. In fact, Android development should have encountered this phenomenon more or less. I am in our own app.

I also found this phenomenon. I was puzzled at the beginning, because according to my own research and analysis, only when a view accepts the touch event pressed will it be transferred to the view

Change the background status by using the setPressed method. The case here obviously does not press this sub-view. The ViewGroup is pressed, so it has been

I didn't think about it, so I didn't think much at the time, so it was so unclear. (now I want to go back and find out where the setPressed method is called in android src,

This problem is solved ). One day, our Review encountered related problems again. Coincidentally, our fix solved the original bug,

Unfortunately, this article is introduced. Again, the problem occurred. At the same time, another of our colleagues showed me the code in section ViewGroup. At that time, I felt very excited.

Surprised. I was excited that the question that had plagued me for a long time was finally answered. I was surprised that I was not quite able to understand why Android would write such a piece of code that seems a little "nosy" to me. Okay, story

The background is almost explained, and the topic is directly entered below.

Everyone who knows about touch events knows that the View. setPressed (true) Call occurs in the View. onTouchEvent method, either DOWN

When an event or a DOWN event is postponed after milliseconds of TAP_TIMEOUT, the event is either handled by the UP event. Let's take a look at the relevant source code:

/*** Sets the pressed state for this view. ** @ see # isClickable () * @ see # setClickable (boolean) ** @ param pressed Pass true to set the View's internal state to "pressed ", or false to reverts * the View's internal state from a previusly set "pressed" state. */public void setPressed (boolean pressed ){
// Check whether the pressed status changes to final boolean needsRefresh = pressed! = (MPrivateFlags & PFLAG_PRESSED) = PFLAG_PRESSED); if (pressed) {// set the flag position mPrivateFlags | = PFLAG_PRESSED;} else {mPrivateFlags & = ~ PFLAG_PRESSED;} if (needsRefresh) {// The status has changed, you need to refresh the drawable state, where the background will become a suitable refreshDrawableState ();} dispatchSetPressed (pressed ); // pass the pressed status to the subview}/*** Dispatch setPressed to all of this view's children. ** @ see # setPressed (boolean) ** @ param pressed The new pressed state */protected void dispatchSetPressed (boolean pressed) {// The default value of view is do nothing, because it does not have children.} // ViewG Oup's heavy load on dispatchSetPressed @ Override protected void dispatchSetPressed (boolean pressed) {// This is the ViewGroup's final View [] children = mChildren; final int count = mChildrenCount; for (int I = 0; I <count; I ++) {final View child = children [I]; // Children that are clickable on their own shocould not // show a pressed state when their parent view does. // Clearing a pressed state always propagates. note Meaning to understand this comment if (! Pressed | (! Child. isClickable ()&&! Child. isLongClickable ())){
// In fact, this if statement mainly involves two tasks. 1. if pressed is false, the setPressed (false) method of child is always called.
// In this case, always pass; 2. If pressed is true, only child cannot respond to the touch event, that is, clickable and longClickable are both false.
// Call the child. setPressed (true) method. To put it bluntly, if the child can handle the pressed event, they will handle it themselves; otherwise, the parent will
// Force the child to process the request. At the same time, note that this call is still recursive and will always be passed to future generations... Child. setPressed (pressed );}}}

In addition, we need to raise 1 point until Android4.1.x starts ViewGroup. dispatchSetPressed. This if judgment is added during the traversal of children, that is, before

Will directly call the child. setPressed (pressed) method. From the above analysis, we can see that the parent "Forces" child to process the pressed event, so in order to avoid

This document does not start with a bug. You can use either of the following methods:

1. Enable a view to handle touch events, that is, set clickable and longClickable to true;

2. If a view does not process touch events by itself, you should not set a stateful drawable for it, or at least do not set a separate state for its pressed state.

It does not matter even if setPressed (true) is called.

In our code, we chose 2nd solutions and removed their background directly from the code. This small article is a supplement to the previous touch event processing and is also in the development process.

Experience and Lessons, hope to help you, enjoy...

Finally, the same topic of the previous csdn article: http://blog.csdn.net/cpyyes/article/details/11144497


Put TextView in Layout. Click Layout to make TextView in the pressed status.

You can set the onTouchListener of layout. When btn. setPressed (true) is pressed, setPressed (false) is lifted, or the Button background is changed directly when it is pressed or lifted without selector.

Net in the gridview group display, click the plus sign "+", and then the sub-group out, the key is that the customer special requirements, turn to the front "-"

Suggestion: You can use JavaScript to dynamically process the generated HTML code, operate the DOM to add a framework page under a Grouped table, and asynchronously obtain the data of the sub-Group, assign a value to this frame page and change the corresponding image "-"

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.