I want to give a click event to the view at the back.

Asked 2 years ago, Updated 2 years ago, 34 views

The view is arranged as shown in the picture, and what should I do if I want to give a click event to a large blue container?

Well... I think you can set the onClickListener with clickable as false in the red in the front and clickable as true in the blue in the back, but I don't know if I made the wrong code... It doesn't work

Is there any other way?

android

2022-09-22 22:05

2 Answers

I tested it with the code below and it works well. Make sure that you gave the onClickListener to the back view.;

;;

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");
            }
        });
    }
}


2022-09-22 22:05

Read the comments and add an answer. For some reason, on ClickListener doesn't work on RecyclerView-_-;; It's clearly written in the API document...

SetOnTouchListener works in RecyclerView. How about registering setOnTouchListener? And I don't know why you want to register a click event with RecyclerView, but...According to the UI you uploaded, I would add another blue background view to the Viewholder's item view and give it a padded jacket to make that UI and register on ClickListner in Viewholder. I hope I can be of any help.


2022-09-22 22:05

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.