簡體   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