繁体   English   中英

在导航抽屉中将OnClickListener设置为LinearLayout

[英]set OnClickListener to LinearLayout in Navigation Drawer

我的活动中有一个导航抽屉,其中包含LinearLayout和ListView。 问题是,当用户单击“线性布局”时,改为触发此视图onClickListener时,我的片段onclickListener被触发,并且发生了错误的操作!

在此处输入图片说明

这是我的导航抽屉XML文件:

<android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <FrameLayout
        android:id="@+id/content_frame"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
    </FrameLayout>

    <LinearLayout
        android:id="@+id/linearDrawer"
        android:layout_width="260dp"
        android:layout_height="match_parent"
        android:layout_gravity="right"
        android:background="#80ff0000"
        android:orientation="vertical" >

        <LinearLayout
            android:id="@+id/userContent"
            android:layout_width="260dp"
            android:layout_height="80dp"
            android:background="@android:color/black"
            android:gravity="center_vertical" >

            <RelativeLayout
                android:id="@+id/userDrawer"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="@color/transparent" >

                <ImageView
                    android:id="@+id/ImgBackground"
                    android:layout_width="260dp"
                    android:layout_height="80dp"
                    android:layout_alignParentLeft="true"
                    android:layout_centerVertical="true"
                    android:contentDescription="@string/app_name"
                    android:scaleType="centerCrop"
                    android:src="@drawable/parsvid_view_video_image_placeholder" />

                <FrameLayout
                    android:id="@+id/avatar_holder"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentRight="true"
                    android:layout_marginRight="13.5dp"
                    android:layout_marginTop="10dp" >

                    <com.meg7.widget.SvgImageView
                        android:id="@+id/avatar_strok"
                        android:layout_width="60.5dp"
                        android:layout_height="60.5dp"
                        android:scaleType="centerCrop"
                        android:src="#B3ffffff"
                        app:svg_raw_resource="@drawable/mask" />

                    <com.meg7.widget.SvgImageView
                        android:id="@+id/user_avatar_logged"
                        android:layout_width="55.5dp"
                        android:layout_height="55.5dp"
                        android:layout_gravity="right"
                        android:layout_marginRight="2.5dp"
                        android:layout_marginTop="2.5dp"
                        android:scaleType="centerCrop"
                        android:src="@drawable/user_avatar"
                        app:svg_raw_resource="@drawable/mask" />
                </FrameLayout>

                <RelativeLayout
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginRight="5dp"
                    android:layout_marginTop="20dp"
                    android:layout_toLeftOf="@+id/avatar_holder" >

                    <TextView
                        android:id="@+id/txt_user_name_drawer"
                        android:layout_width="fill_parent"
                        android:layout_height="wrap_content"
                        android:gravity="right"
                        android:text=""
                        android:textAllCaps="true"
                        android:textAppearance="?android:attr/textAppearanceMedium"
                        android:textColor="@android:color/white" />

                    <TextView
                        android:id="@+id/txt_user_email"
                        android:layout_width="fill_parent"
                        android:layout_height="wrap_content"
                        android:layout_below="@+id/txt_user_name_drawer"
                        android:gravity="right"
                        android:text=""
                        android:textAllCaps="true"
                        android:textColor="@android:color/white"
                        android:textSize="12sp" />
                </RelativeLayout>
            </RelativeLayout>
        </LinearLayout>

        <View
            android:id="@+id/viewSeparator"
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:background="#FFffbb00" />

        <ListView
            android:id="@+id/drawer_list"
            android:layout_width="260dp"
            android:layout_height="match_parent"
            android:layout_gravity="right"
            android:background="#FF002bcc"
            android:cacheColorHint="@color/transparent"
            android:choiceMode="singleChoice"
            android:divider="@android:color/transparent"
            android:dividerHeight="1dp" />
    </LinearLayout>
</android.support.v4.widget.DrawerLayout>

我的Java代码是:

LinearLayout mUserContent;
mUserContent = (LinearLayout) findViewById(R.id.userContent);
mUserContent.setClickable(true);
mUserContent.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {

        Toast.makeText(getApplicationContext(), "Clicked!!", Toast.LENGTH_LONG).show();

        if(LoginSession.isLoggedIn()){
            // do something
        }else{
            // do another thing
        }
    }
});

问题出在哪儿?

android:clickable="true"linearDrawer

<LinearLayout
    android:id="@+id/linearDrawer"
    android:layout_width="260dp"
    android:layout_height="match_parent"
    android:layout_gravity="right"
    android:background="#80ff0000"
    android:orientation="vertical"
    android:clickable="true" >

或以编程方式:

LinearLayout mLinearDrawer = (LinearLayout) findViewById(R.id.linearDrawer);
// method 1
mLinearDrawer.setClickable(true);
// method 2
mLinearDrawer.setOnClickListener(null);

您将clickable设置为较小的视图,因此它没有覆盖所有区域。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM