How to trigger selectors from the layout view?

断了今生、忘了曾经 提交于 2019-12-10 23:07:48

问题


The problem

Suppose you have a view inside a layout view, and you wish that each touching event on the layout would affect the view's background selector.

for example, when you touch the layout , the selector of the view will choose the state_pressed state.

What I've tried

I've read about duplicateParentState and addStatesFromChildren , but I think it's the opposite of what I'm searching for.

I've also tried to use splitMotionEvents, but it didn't help.

The question

How do you do this?

Also, what should be done in case the layout is inside a listView (as an item within it) ?


回答1:


Consider you have a spinner inside a linear layout. Make focusable property of view(here that is spinner) to false. And onClick of linear layout call performClick on view(spinner in this case).

EDIT:

In focus Change Listener of linear layout if it has focus, call requestFocus on view. You can follow same method to other states of view

Another method:

do not apply any selector to layout(linearlayout in this case) and add whatever selector you want to view(spinner in this case). For view add this parameter

android:duplicateParentState="true"


来源:https://stackoverflow.com/questions/22588662/how-to-trigger-selectors-from-the-layout-view

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!