当孩子设置为INVISIBLE时,父母不可点击

时间:2013-01-16 06:51:16

标签: android view onclick

我有一个简单的RelativeLayout,其中包含两个TextView和一个ImageView。当我将Views可见性设置为View.INVISIBLE时,单击视图时不会触发父级的onClickListener。当它们可见时,只要我点击它就会调用父级的onClickListener 我想将它们设置为不可见,因为我想根据模型的状态来显示另一个TextView。 我错过了什么?

 public class CheckinCompoundView extends RelativeLayout {

    private static final String TAG = CheckinCompoundView.class.getSimpleName();
    private RelativeLayout mEventContainer;
    @SuppressWarnings("unused")
    private ImageView mEventIcon;
    private TextView mEventName;
    private TextView mEventCategory;
    private RelativeLayout mPlaceContainer;
    private ImageView mPlaceIcon;
    private TextView mPlaceName;
    private TextView mPlaceCategory;
    // private TextView mPlaceEmptyView;
    private TextView mStatText;

    public CheckinCompoundView(Context context) {
        super(context);
        initView();
    }

    private void initView() {
        Context context = getContext();
        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        inflater.inflate(R.layout.view_checkin_compund_view, this, true);

        mEventContainer = (RelativeLayout) findViewById(R.id.event_container);
        mEventIcon = (ImageView) findViewById(R.id.event_image);
        mEventName = (TextView) findViewById(R.id.event_title);
        mEventCategory = (TextView) findViewById(R.id.event_category);
        mPlaceContainer = (RelativeLayout) findViewById(R.id.place_container);
        mPlaceIcon = (ImageView) findViewById(R.id.place_image);
        mPlaceName = (TextView) findViewById(R.id.place_title);
        mPlaceCategory = (TextView) findViewById(R.id.place_category);
        mStatText = (TextView) findViewById(R.id.stat_text);
        // mPlaceEmptyView = (TextView) findViewById(R.id.place_empty);

        mEventCategory.setTypeface(UIUtils.getRobotoLight(context));
        mPlaceCategory.setTypeface(UIUtils.getRobotoLight(context));
        mEventName.setTypeface(UIUtils.getRobotoCondensed(context));
        mPlaceName.setTypeface(UIUtils.getRobotoCondensed(context));
        mStatText.setTypeface(UIUtils.getRobotoCondensed(context));
        // mPlaceEmptyView.setTypeface(UIUtils.getRobotoLight(context));
    }

    private void showEmpty() {
        // mPlaceEmptyView.setVisibility(View.VISIBLE);
        mPlaceIcon.setVisibility(View.INVISIBLE);
        mPlaceName.setVisibility(View.INVISIBLE);
        mPlaceCategory.setVisibility(View.INVISIBLE);
    }

    private void hideEmpty() {
        // mPlaceEmptyView.setVisibility(View.GONE);
         mPlaceIcon.setVisibility(View.VISIBLE);
         mPlaceName.setVisibility(View.VISIBLE);
         mPlaceCategory.setVisibility(View.VISIBLE);
    }

     public void setOnPlaceClickListener(OnClickListener listener) {
        mPlaceCategory.setOnClickListener(listener);
    }
    }

以及相应的布局:

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/light_grey_background_border"
    tools:context=".view.CheckinPlaceCompoundView" >

    <RelativeLayout
        android:id="@+id/event_container"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:minHeight="@dimen/view_chckn_compnd_event_minHeight"
        android:padding="@dimen/view_chckn_compnd_padding" >

        <ImageView
            android:id="@+id/event_image"
            android:layout_width="@dimen/view_chckn_compnd_event_iconHeight"
            android:layout_height="@dimen/view_chckn_compnd_event_iconHeight"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true"
            android:scaleType="centerInside"
            android:src="@drawable/ph_abstract_100_100_2" />

        <TextView
            android:id="@+id/event_title"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignTop="@+id/event_image"
            android:layout_marginLeft="@dimen/view_chckn_compnd_event_padding"
            android:layout_toRightOf="@+id/event_image"
            android:maxLines="1"
            android:text="SZOFTVER LABORATORIUM 4."
            android:textSize="16sp" />

        <TextView
            android:id="@+id/event_category"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignLeft="@+id/event_title"
            android:layout_below="@+id/event_title"
            android:maxLines="1"
            android:text="General"
            android:textColor="@color/text_grey" />
    </RelativeLayout>


    <RelativeLayout
        android:id="@+id/place_container"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/event_container"
        android:layout_below="@+id/event_container"
        android:minHeight="@dimen/view_chckn_compnd_event_minHeight"
        android:padding="@dimen/view_chckn_compnd_padding" >

        <ImageView
            android:id="@+id/place_image"
            android:layout_width="@dimen/view_chckn_compnd_place_iconHeight"
            android:layout_height="@dimen/view_chckn_compnd_place_iconHeight"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true"
            android:scaleType="centerInside"
            android:src="@drawable/ph_abstract_100_100_2" />

        <TextView
            android:id="@+id/place_title"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignTop="@+id/place_image"
            android:layout_marginLeft="@dimen/view_chckn_compnd_event_padding"
            android:layout_toRightOf="@+id/place_image"
            android:maxLines="1"
            android:text="IB028"
            android:textSize="16sp" />

        <TextView
            android:id="@+id/place_category"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignLeft="@+id/place_title"
            android:layout_below="@+id/place_title"
            android:maxLines="1"
            android:text="Labratory"
            android:textColor="@color/text_grey" />
    </RelativeLayout>

    <TextView
        android:id="@+id/stat_text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/place_container"
        android:layout_below="@+id/place_container"
        android:minHeight="@dimen/view_chckn_compnd_event_minHeight"
        android:padding="@dimen/view_chckn_compnd_padding"
        android:text="Your team is dominating" />
    </RelativeLayout>

1 个答案:

答案 0 :(得分:1)

将您的showEmpty()hideEmpty()方法更改为:

private void showEmpty() {
    // mPlaceEmptyView.setVisibility(View.GONE);
    mPlaceIcon.setVisibility(View.GONE);
    mPlaceName.setVisibility(View.GONE);
    mPlaceCategory.setVisibility(View.GONE);
    mPlaceIcon.setClickable( false );
    mPlaceName.setClickable( false );
    mPlaceCategory.setClickable( false );

}


private void hideEmpty() {
    // mPlaceEmptyView.setVisibility(View.GONE);
     mPlaceIcon.setVisibility(View.VISIBLE);
     mPlaceName.setVisibility(View.VISIBLE);
     mPlaceCategory.setVisibility(View.VISIBLE);
     mPlaceIcon.setClickable( true);
     mPlaceName.setClickable( true);
     mPlaceCategory.setClickable( true);
}