如何将Click事件转移到Gridview下面的按钮?

时间:2017-06-09 16:14:46

标签: android button gridview onclicklistener ontouchlistener

我已经尝试了一切,但我不明白该怎么做?

我有一个与按钮重叠的gridview,当我点击按钮时,click事件没有转移到按钮,因为gridview与它重叠。我不能把按钮放在gridview上面因为它会破坏设计是否有任何方法可以实现它。这个图表可以更好地解释。 enter image description here

这是我的xml代码我不认为共享java代码会有意义。

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                xmlns:tools="http://schemas.android.com/tools"
                android:id="@+id/browser_sub_drawer_parent"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                >

    <android.support.v4.widget.DrawerLayout
        android:id="@+id/browser_sub_drawer_root"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <RelativeLayout
                android:id="@+id/browser_sub_header_layout"
                android:layout_width="match_parent"
                android:layout_height="340dp"
                android:layout_alignParentTop="true"
                android:visibility="invisible">

                <ImageView
                    android:id="@+id/browser_sub_header_image"
                    android:layout_width="match_parent"
                    android:layout_height="340dp"
                    android:adjustViewBounds="true"
                    android:background="@drawable/black_to_transparent_gradient"
                    android:scaleType="centerCrop"/>

                <RelativeLayout
                    android:id="@+id/browser_sub_header_text_layout"
                    android:layout_width="match_parent"
                    android:layout_height="80dp"
                    android:layout_alignParentBottom="true"
                    android:layout_marginBottom="60dp"
                    android:background="@drawable/black_to_transparent_gradient">

                    <TextView
                        android:id="@+id/browser_sub_header_text"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_alignParentTop="true"
                        android:layout_marginRight="120dp"
                        android:layout_marginTop="15dp"
                        android:ellipsize="marquee"
                        android:marqueeRepeatLimit="marquee_forever"
                        android:paddingLeft="12dp"
                        android:paddingRight="12dp"
                        android:scrollHorizontally="true"
                        android:shadowColor="#FF454545"
                        android:shadowDx="0"
                        android:shadowDy="0"
                        android:shadowRadius="5"
                        android:singleLine="true"
                        android:text="Header Text"
                        android:textColor="#FFFFFF"
                        android:textSize="24sp"/>

                    <TextView
                        android:id="@+id/browser_sub_header_sub_text"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_below="@+id/browser_sub_header_text"
                        android:layout_marginRight="120dp"
                        android:ellipsize="marquee"
                        android:marqueeRepeatLimit="marquee_forever"
                        android:paddingLeft="12dp"
                        android:paddingRight="12dp"
                        android:scrollHorizontally="true"
                        android:shadowColor="#FF454545"
                        android:shadowDx="0"
                        android:shadowDy="0"
                        android:shadowRadius="5"
                        android:singleLine="true"
                        android:text="Subtext"
                        android:textColor="#FF999999"
                        android:textSize="16sp"/>

                    <TextView
                        android:id="@+id/browser_sub_play_all"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_alignParentRight="true"
                        android:layout_centerVertical="true"
                        android:layout_marginRight="25dp"
                        android:background="?android:selectableItemBackground"
                        android:padding="5dp"
                        android:text="@string/play_all_caps"
                        android:textColor="#FFFFFF"
                        android:textSize="12sp"/>

                </RelativeLayout>

                <RelativeLayout
                    android:layout_width="match_parent"
                    android:layout_height="60dp"
                    android:layout_alignParentBottom="true"
                    android:background="#BB000000"/>

            </RelativeLayout>

            <GridView
                android:id="@+id/browser_sub_grid_view"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:layout_alignParentStart="true"
                android:layout_alignParentTop="true"
                android:clickable="false"
                android:drawSelectorOnTop="true"
                android:focusable="false"
                android:horizontalSpacing="3.5dp"
                android:paddingBottom="6dp"
                android:paddingLeft="6dp"
                android:paddingRight="6dp"
                android:paddingTop="280dp"
                android:verticalSpacing="3.5dp"/>

        </RelativeLayout>


    </android.support.v4.widget.DrawerLayout>

</RelativeLayout>

请注意gridview涵盖了我给出顶部填充的整个屏幕。 提前谢谢。

2 个答案:

答案 0 :(得分:0)

像这样更新您的代码

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/browser_sub_drawer_parent"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >

    <android.support.v4.widget.DrawerLayout
        android:id="@+id/browser_sub_drawer_root"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <RelativeLayout
                android:id="@+id/browser_sub_header_layout"
                android:layout_width="match_parent"
                android:layout_height="340dp"
                android:layout_alignParentTop="true"
                android:visibility="invisible">

                <ImageView
                    android:id="@+id/browser_sub_header_image"
                    android:layout_width="match_parent"
                    android:layout_height="340dp"
                    android:adjustViewBounds="true"
                    android:background="@drawable/black_to_transparent_gradient"
                    android:scaleType="centerCrop"/>

                <RelativeLayout
                    android:id="@+id/browser_sub_header_text_layout"
                    android:layout_width="match_parent"
                    android:layout_height="80dp"
                    android:layout_alignParentBottom="true"
                    android:layout_marginBottom="60dp"
                    android:background="@drawable/black_to_transparent_gradient">

                    <TextView
                        android:id="@+id/browser_sub_header_text"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_alignParentTop="true"
                        android:layout_marginRight="120dp"
                        android:layout_marginTop="15dp"
                        android:ellipsize="marquee"
                        android:marqueeRepeatLimit="marquee_forever"
                        android:paddingLeft="12dp"
                        android:paddingRight="12dp"
                        android:scrollHorizontally="true"
                        android:shadowColor="#FF454545"
                        android:shadowDx="0"
                        android:shadowDy="0"
                        android:shadowRadius="5"
                        android:singleLine="true"
                        android:text="Header Text"
                        android:textColor="#FFFFFF"
                        android:textSize="24sp"/>

                    <TextView
                        android:id="@+id/browser_sub_header_sub_text"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_below="@+id/browser_sub_header_text"
                        android:layout_marginRight="120dp"
                        android:ellipsize="marquee"
                        android:marqueeRepeatLimit="marquee_forever"
                        android:paddingLeft="12dp"
                        android:paddingRight="12dp"
                        android:scrollHorizontally="true"
                        android:shadowColor="#FF454545"
                        android:shadowDx="0"
                        android:shadowDy="0"
                        android:shadowRadius="5"
                        android:singleLine="true"
                        android:text="Subtext"
                        android:textColor="#FF999999"
                        android:textSize="16sp"/>

                    <TextView
                        android:id="@+id/browser_sub_play_all"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_alignParentRight="true"
                        android:layout_centerVertical="true"
                        android:layout_marginRight="25dp"
                        android:background="?android:selectableItemBackground"
                        android:padding="5dp"
                        android:text="@string/play_all_caps"
                        android:textColor="#FFFFFF"
                        android:textSize="12sp"/>

                </RelativeLayout>

                <RelativeLayout
                    android:layout_width="match_parent"
                    android:layout_height="60dp"
                    android:layout_alignParentBottom="true"
                    android:background="#BB000000"/>

            </RelativeLayout>

            <GridView
                android:id="@+id/browser_sub_grid_view"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:layout_alignParentStart="true"
                android:layout_alignParentTop="true"
                android:clickable="false"
                android:drawSelectorOnTop="true"
                android:focusable="false"
                android:horizontalSpacing="3.5dp"
                android:paddingBottom="6dp"
                android:paddingLeft="6dp"
                android:paddingRight="6dp"
                android:layout_marginTop="280dp"
                android:verticalSpacing="3.5dp"/>

        </RelativeLayout>


    </android.support.v4.widget.DrawerLayout>

</RelativeLayout>

答案 1 :(得分:0)

事实证明这很简单,虽然我花了很多时间弄清楚代码。

mGridView.setOnTouchListener((v, event) -> {
            MotionEvent e = MotionEvent.obtain(event);
            //view to which you want to dispatch the motionevent.
            mHeaderLayout.dispatchTouchEvent(e);
            e.recycle();
            return false;
        });