Hugo
Hugo

Reputation: 1672

Clickable view over a listview element in android

my problem is that i have a ListView, populated from an adapter and with some buttons and ontouchevents inside each element in the list. I want the list to show as "active" only the element in the center of the screen, and for that i show a relativeLayout over the list element which is not in the middle of the screen. This is already working ok.

The problem comes because the view i put in fron of the item is a bit transparent, and buttons behind it (inside the listview item) are still clickable. I want the relativeview on top to consume onclick and ontouch events but everything i read is not working, please i would apreciate your help! :)

I tried with

listViewElement.setEnabled(false);
listViewElement.setClickable(false);

and with

relativeLayoutOverElement.bringToFront();
relativeLayoutOverElement.setClickable(true);
relativeLayoutOverElement.setEnabled(true);
relativeLayoutOverElement.setFocusable(true);
relativeLayoutOverElement.setOnTouchListener(new OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {

                return true;
           }
        });
relativeLayoutOverElement.setOnClickListener(null);

but it didn't work.

this is the format of the xml code of each element in the listview:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:arc="http://schemas.android.com/apk/res-auto"
    android:id="@+id/container"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:paddingBottom="2dp"
    android:paddingTop="2dp" >

    <com.capricorn.ArcMenu
        android:id="@+id/arc_menu"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:visibility="gone"
        arc:childSize="@dimen/menuChildSize"
        arc:fromDegrees="@dimen/menuFromDegrees"
        arc:toDegrees="@dimen/menuToDegrees" />

    <ImageView
        android:id="@+id/imageplay"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_centerInParent="true"
        android:src="@drawable/play"
        android:visibility="gone" />

    <RelativeLayout
        android:id="@+id/transparency"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="@android:color/black"
        android:visibility="gone" >
    </RelativeLayout>

    <LinearLayout
        android:id="@+id/container2"
        android:layout_width="match_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/event_name"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:background="@android:color/transparent"
            android:gravity="center"
            android:padding="6dp"
            android:textColor="@color/green"
            android:textSize="15sp"
            android:visibility="gone" />

        <RelativeLayout
            android:id="@+id/imgvidcontainer"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >

            <RelativeLayout
                android:id="@+id/videocontainer"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:visibility="gone" >

                <VideoView
                    android:id="@+id/video"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_centerInParent="true" />
            </RelativeLayout>

            <ImageView
                android:id="@+id/image"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:scaleType="fitXY" />

        </RelativeLayout>

        <LinearLayout
            android:id="@+id/footer"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:background="@android:color/transparent"
            android:orientation="vertical"
            android:paddingBottom="6dp"
            android:paddingLeft="10dp"
            android:paddingRight="10dp"
            android:paddingTop="6dp" >

            <LinearLayout
                android:id="@+id/footer_location_time"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@android:color/transparent"
                android:orientation="horizontal" >

                <ImageView
                    android:id="@+id/location_icon"
                    android:layout_width="25dp"
                    android:layout_height="25dp"
                    android:src="@drawable/icon_location_green" />

                <TextView
                    android:id="@+id/location"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_vertical"
                    android:layout_marginLeft="6dp"
                    android:background="@android:color/transparent"
                    android:textColor="@color/green" />

            </LinearLayout>

            <RelativeLayout
                android:id="@+id/footer_caption_layout"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" >

                <ImageView
                    android:id="@+id/seemorecaption"
                    android:layout_width="20dp"
                    android:layout_height="20dp"
                    android:layout_alignParentEnd="true"
                    android:layout_alignParentRight="true"
                    android:paddingLeft="5dp"
                    android:paddingRight="5dp"
                    android:src="@drawable/seemorecaption"
                    android:visibility="gone" />

                <TextView
                    android:id="@+id/_caption"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textColor="@color/green" />
            </RelativeLayout>

            <LinearLayout
                android:id="@+id/footer__info"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@android:color/transparent"
                android:orientation="horizontal" >

                <ImageView
                    android:id="@+id/comments"
                    android:layout_width="35dp"
                    android:layout_height="35dp"
                    android:paddingTop="7dp"
                    android:src="@drawable/comments" />

                <TextView
                    android:id="@+id/numcomments"
                    android:layout_width="wrap_content"
                    android:layout_height="match_parent"
                    android:layout_gravity="center_vertical"
                    android:layout_marginLeft="6dp"
                    android:layout_marginRight="20dp"
                    android:background="@android:color/transparent"
                    android:gravity="center_vertical"
                    android:textColor="@color/green" />

                <ImageView
                    android:id="@+id/like"
                    android:layout_width="35dp"
                    android:layout_height="35dp"
                    android:paddingBottom="7dp"
                    android:src="@drawable/like" />

                <TextView
                    android:id="@+id/numlikes"
                    android:layout_width="wrap_content"
                    android:layout_height="match_parent"
                    android:layout_gravity="center_vertical"
                    android:layout_marginLeft="6dp"
                    android:layout_marginRight="20dp"
                    android:background="@android:color/transparent"
                    android:gravity="center_vertical"
                    android:textColor="@color/green" />

                <ImageView
                    android:id="@+id/dislike"
                    android:layout_width="35dp"
                    android:layout_height="35dp"
                    android:paddingTop="7dp"
                    android:src="@drawable/unlike" />

                <TextView
                    android:id="@+id/numdislikes"
                    android:layout_width="wrap_content"
                    android:layout_height="match_parent"
                    android:layout_gravity="center_vertical"
                    android:layout_marginLeft="6dp"
                    android:layout_marginRight="20dp"
                    android:background="@android:color/transparent"
                    android:gravity="center_vertical"
                    android:textColor="@color/green" />
            </LinearLayout>
        </LinearLayout>
    </LinearLayout>

    <TextView
        android:id="@+id/morepoints"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentEnd="true"
        android:layout_alignParentRight="true"
        android:paddingBottom="6dp"
        android:paddingLeft="10dp"
        android:paddingRight="10dp"
        android:paddingTop="6dp"
        android:textColor="@color/green"
        android:textSize="25dp" />

</RelativeLayout>

and this is the xml code of the fragment that contains the listview:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:staggered="http://schemas.android.com/apk/res-auto"
    android:id="@+id/listLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/black"
    android:orientation="vertical" >

    <android.support.v4.widget.SwipeRefreshLayout
        android:id="@+id/swipe"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentTop="true">

        <ListView
            android:id="@+id/listView1"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:clipToPadding="false"
            android:numColumns="1"
            android:paddingTop="50dp" />
    </android.support.v4.widget.SwipeRefreshLayout>

    <LinearLayout
        android:id="@+id/upperBar"
        android:layout_width="fill_parent"
        android:layout_height="50dp"
        android:layout_alignParentTop="true"
        android:background="@android:color/transparent"
        android:orientation="horizontal" >

        <TextView
            android:id="@+id/title"
            android:layout_width="0dp"
            android:layout_height="fill_parent"
            android:layout_weight="0.85"
            android:background="@drawable/greenborder"
            android:gravity="center_vertical|center_horizontal"
            android:textColor="@color/green"
            android:textSize="20dp" />

        <TextView
            android:id="@+id/blackSquare"
            android:layout_width="50dp"
            android:layout_height="fill_parent"
            android:background="@android:color/black"
            android:visibility="gone" />
    </LinearLayout>

    <RelativeLayout
        android:id="@+id/profilePictureLayout"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:background="@layout/rounded_shape"
        android:visibility="gone" >

        <ImageView
            android:id="@+id/profile_image"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@layout/rounded_shape"
            android:padding="4dp" />
    </RelativeLayout>

</RelativeLayout>

I'm trying to disable elements in the listview that are not in the center of the screen from the onscroll in the fragment. I manage to put the "transparency" overlay but everything inside the element remains clickable

Upvotes: 0

Views: 1306

Answers (1)

Swayam
Swayam

Reputation: 16354

To consume the click, simply define an OnClickListener on the view and do nothing.

relativeLayoutOverElement.setOnClickListener(setOnClickListener(new OnClickListener() {
                        @Override
                        public void onClick(View v) {
                          //do nothing
           }
        });

So, clicking on the view will trigger the click listener of the transparency overlay, which won't do anything, but will consume the click so that the underlying elements don't get clicked.

Another way to do this would be to show the transparency overlay on top and make the underlying views not clickable by using setClickable(false) on the underlying view whose click you want to stop. What you tried was making the whole list element not clickable, but that doesn't really remove the onclick listeners of the child view in that list element. So, you have to explicitly disable the click of the child view which has the click listener defined on it.

Upvotes: 1

Related Questions