뒤쪽의 뷰에 클릭 이벤트를 주고 싶습니다.

조회수 6511회

이미지 이름이나 설명을 여기에 넣어주세요.

그림에서처럼 뷰가 배치되어 있고, 저는 파란색의 큰 컨테이너에 클릭이벤트를 주고 싶으면 어떻게 해야하나요?

음... 일단 앞의 빨간색에는 clickable을 false로 두고, 뒤쪽 파란색에 clickable을 true로 두고 onClickListener를 설정해주면 될 것 같은데 제가 코드를 잘못짠 것인지... 안되네요.....

혹시 다른 방법이 있을까요?

  • (•́ ✖ •̀)
    알 수 없는 사용자

2 답변

  • 저는 아래와 같은 코드로 테스트해보니 잘 동작합니다. onClickListener를 뒷 뷰에 준것이 맞는지 확인해보세요.;

    activity_main.xml

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/root"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/colorPrimary"
        android:orientation="vertical"
        android:clickable="true"
        tools:context="com.egithcruz.myapplication.MainActivity">
    
        <TextView
            android:id="@+id/list_item"
            android:clickable="false"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/colorAccent"
            android:text="Hello World!" />
    
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/colorAccent"
            android:text="Hello World!" />
    
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/colorAccent"
            android:text="Hello World!" />
    
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/colorAccent"
            android:text="Hello World!" />
    
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/colorAccent"
            android:text="Hello World!" />
    </LinearLayout>
    
    

    MainActivity.java

    public class MainActivity extends AppCompatActivity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            View view = findViewById(R.id.root);
            view.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Log.d("TEST", "Click");
                }
            });
        }
    }
    
    
    • (•́ ✖ •̀)
      알 수 없는 사용자
    • 답변 감사합니다 ㅠ 그런데 혹시 RecyclerView에는 클릭이벤트를 줄 수 없나요....? 송주연님께서 올려주신 것처럼 해서 RecyclerView에 Clickable속성을 true로 주고 setOnClickListener를 달아주었는데 아무런 반응이 없네요.. ㅠ 알 수 없는 사용자 2016.4.11 17:45
  • 댓글보고 답변 추가합니다. RecyclerView에는 어찌된 영문인지 onClickListener가 먹질 않네요-_-;; API문서에도 분명히 적혀있는데...

    RecyclerView에 setOnTouchListener는 동작하네요. setOnTouchListener 를 등록하는건 어떨까요? 그리고 RecyclerView에 클릭이벤트를 왜 등록하시려는진 모르겠지만...올려주신 UI대로라면 저라면 ViewHolder의 아이템뷰에 파랑색배경이 될 뷰를 하나 더 넣어서 패딩을 주어서 저런 UI를 만들고 Viewholder에 onClickListner를 등록할것 같아요. ;) 조금이라도 도움이 되셨으면 합니다.

    • (•́ ✖ •̀)
      알 수 없는 사용자

답변을 하려면 로그인이 필요합니다.

프로그래머스 커뮤니티는 개발자들을 위한 Q&A 서비스입니다. 로그인해야 답변을 작성하실 수 있습니다.

(ಠ_ಠ)
(ಠ‿ಠ)