android - 如何让父 View 处理所有的OnClick事件?

标签 android layout onclicklistener

我有一个布局,有一个显示另一个 View 的按钮,但只显示一次,我的意思是,您单击该按钮,它消失并显示另一个 View ,第二次您应该单击该 View 的父 View 按钮和另一个 View (显示的 View )应该消失,并且按钮应该再次出现。我正在尝试使用 clickable:falsefocusable:false 但它不起作用。我怎样才能实现这一目标?

相关代码

XML

<LinearLayout
        android:id="@+id/item_tournament_header"
        android:background="@drawable/bg_card_tournament"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:weightSum="15">
    <RelativeLayout
        android:clickable="false"
        android:focusable="false"
        android:layout_weight="3"
        android:layout_width="0dp"
        android:layout_height="match_parent">
        <com.github.siyamed.shapeimageview.CircularImageView
            android:id="@+id/item_friend_img_profile_pic"
            android:layout_height="48dp"
            android:layout_width="48dp"
            android:layout_centerInParent="true"
            android:scaleType="centerCrop"
            android:src="@drawable/ic_profile"
            app:siBorderColor="@color/white"/>
    </RelativeLayout>
    <LinearLayout
        android:clickable="false"
        android:focusable="false"
        android:layout_weight="10"
        android:layout_width="0dp"
        android:orientation="vertical"
        android:layout_height="match_parent">
    <TextView
        android:id="@+id/tournament_name"
        android:textSize="@dimen/text_h3"
        android:textStyle="bold"
        android:textColor="@color/white"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
    <TextView
        android:id="@+id/tournament_client"
        android:textSize="@dimen/text_p"
        android:textColor="@color/white"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
    </LinearLayout>
        <RelativeLayout
            android:clickable="false"
            android:focusable="false"
            android:layout_weight="2"
            android:layout_width="0dp"
            android:orientation="vertical"
            android:layout_height="match_parent">

            <ImageView
                android:id="@+id/btn_plus"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerInParent="true"
                android:src="@drawable/ic_plus_tournaments"/>
            </RelativeLayout>
    </LinearLayout>

Java

 btn_plus  = (ImageView) findViewById(R.id.btn_plus);
        TournamentContent =(LinearLayout)findViewById(R.id.item_tournament_content);
        TournamentHeadaer =(LinearLayout)findViewById(R.id.item_tournament_header);

        btn_plus.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                TournamentContent.setVisibility(View.VISIBLE);
                btn_plus.setVisibility(View.GONE);
            }
        });

        TournamentHeadaer.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(btn_plus.getVisibility()==View.VISIBLE)
                {
 // It's not entering here!!!
                    TournamentContent.setVisibility(View.GONE);
                    btn_plus.setVisibility(View.VISIBLE);
                }
            }
        });

最佳答案

 btn_plus  = (ImageView) findViewById(R.id.btn_plus); 
        TournamentContent =(LinearLayout)findViewById(R.id.item_tournament_content); 
        TournamentHeadaer =(LinearLayout)findViewById(R.id.item_tournament_header); 

        btn_plus.setOnClickListener(new View.OnClickListener() {
            @Override 
            public void onClick(View view) {
                TournamentContent.setVisibility(View.VISIBLE);
                btn_plus.setVisibility(View.GONE);
            } 
        }); 

        TournamentHeadaer.setOnClickListener(new View.OnClickListener() {
            @Override 
            public void onClick(View view) {
                if(btn_plus.getVisibility()==View.GONE)
                { 
 // It's not entering here!!! 
                    TournamentContent.setVisibility(View.GONE);
                    btn_plus.setVisibility(View.VISIBLE);
                } 
            } 
        }); 

关于android - 如何让父 View 处理所有的OnClick事件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33480864/

相关文章:

java - 如何根据选择的单选按钮打开特定 Activity ?

android - 我们可以将 gson 模型与 realm 一起使用吗?当我尝试时它显示构建时间错误

android - Retrofit 通过移动服务连接,因为 Wi-Fi 网络没有互联网连接

Android Studio - 下载 Gradle 0.12.2?

android - 加快 iPad 应用程序向 Android 的移植

android - 为什么我总是遇到 R 没有解决的问题?

java - 在 Android Java 中重用带有字符串参数的 onclick 监听器

java - 约束布局是否应该包裹所有其他 View ?

android - Eclipse 相对布局很难工作

java - 创建 OnClickListener 时出现 NullPointerException (Android)