안드로이드

[안드로이드] RecyclerView, ListView에서 item이 클릭이 안될 때 : descendantFocusability

디벨로펄 2022. 10. 3.
반응형

ListView Item 내에 버튼을 다음과 같이 구현했다.

여기서 가족, 고등학교, 동아리 등에 해당하는 것이 버튼이다.

 

구현 원하는 방향

ListViewItem을 클릭했을 때는 상세페이지

그룹명(버튼)을 클릭했을 때는 검색 기능으로 이동하려 한다.

 

 

결론

. ListViewItemClickListner와 버튼 클릭 이벤트를 모두 사용하고 싶다면 ListViewItem Layout의

descendantfocusability를 blocksDescendants로 설정한다.

android:descendantFocusability="blocksDescendants"

버튼이 아닌 곳을 클릭했을 때는 listView Item의 클릭 이벤트(B)가 실행됨..

button 클릭 시 A기능 실행됨.

 

 

또다른 해결책

Button의 Focusable

버튼은 기본적으로 focusable 이 true로 설정되어 있다.

즉, 아래 두 옵션에 대해서, Layout자체는 focus에 대한 옵션이 없으므로,

전부 buttonfocus를 가져가게 된다.(욕심쟁이..)

android:descendantFocusability=“afterDescendants"
android:descendantFocusability=“beforeDescendants"

 

Button의 focusable을 false로 바꾸게 되면 두 가지 옵션 모두에서 원래 의도했던 바와 같이 잘 동작한다.

 

 

안드로이드 공식 Documenation

1. afterDescendants : 하위 객체 focus 이후에 현재 객체가 focus받음.

2. beforeDescendants : 하위 객체가 focus 받기 전에 현재 객체가 focus를 받음.

3. blocksDescendants : 하위 객체로의 focus를 차단함. 

반응형

댓글