2013-01-16 38 views
0

我有一個簡單的RelativeLayout與兩個TextView s和ImageView。當我將視圖可見性設置爲View.INVISIBLE時,父視圖的onClickListener在點擊視圖時不會被觸發。當它們可見時,只要我點擊它,就會調用父級的onClickListener。
我想將它們設置爲不可見,因爲我想根據模型的狀態推薦另一個TextView。 我錯過了什麼?家長不可點擊時,兒童設置INVISIBLE

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> 
+0

,而不是VIEW.INVISIBLE,嘗試VIEW.GONE :) – AndroidLearner

+0

這沒什麼感覺,而且它也不起作用。 – gmate

回答

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); 
} 
+0

我不希望他們能夠點擊。父親的RelativeLayout是可點擊的,並且應該處理點擊事件。 – gmate