2016-03-04 84 views
0

我需要創建一個如下所示的片段。設置窗口小部件的可見性,當兩個窗口小部件高於另一個時

點擊Need Cash TextView將改變含佈局這將有兩個按鈕Cancel & Meet和這些視圖之間切換英寸

但是當我點擊需要現金呼叫toggleVisibility()功能使Need Cash隱形但不顯示Cancel | Meet

我在這個過程中犯了什麼錯誤?

|----------------------|  =>  |----------------------| 
|      |  =>  |      | 
|      |  =>  |      | 
|      |  =>  |      | 
|      |  =>  |      | 
|      |  =>  |      | 
|      |  =>  |      | 
|      |  =>  |      | 
|      |  =>  |      | 
|      |  =>  |      | 
|      |  =>  |      | 
|----------------------|  =>  |----------------------| 
|  Need Cash  |  =>  | Cancel | Meet  | 
|----------------------|  =>  |----------------------| 







@Override 
public void onClick(View v) { 
    switch(v.getId()) { 
     case R.id.tv_need_cash: 
       toggleVisibility(); 
      break; 
     case R.id.tv_need_cash_cancel: 
      toggleVisibility(); 
      Toast.makeText(getActivity(), "tv_need_cash_cancel", Toast.LENGTH_SHORT).show(); 
      break; 

     case R.id.tv_need_cash_lets_meet: 
      toggleVisibility(); 
      Toast.makeText(getActivity(), "tv_need_cash_lets_meet", Toast.LENGTH_SHORT).show(); 
      break; 
    } 
} 

public void toggleVisibility() { 
    if(needCashTv.getVisibility() == View.VISIBLE) { 
     //needCashTv.setEnabled(false); 
     needCashTv.setVisibility(View.GONE); 
     frameForNeedCashStuff.bringToFront(); 

     frameForNeedCashStuff.setVisibility(View.VISIBLE); 
     //cancelNeedCashTv.setEnabled(true); 
     cancelNeedCashTv.setVisibility(View.VISIBLE); 
     //letsMeetneedCashTv.setEnabled(true); 
     letsMeetneedCashTv.setVisibility(View.VISIBLE); 
    } 
    else { 
     //needCashTv.setEnabled(true); 
     needCashTv.setVisibility(View.VISIBLE); 
     needCashTv.bringToFront(); 

     frameForNeedCashStuff.setVisibility(View.GONE); 
     //cancelNeedCashTv.setEnabled(false); 
     cancelNeedCashTv.setVisibility(View.GONE); 
     //letsMeetneedCashTv.setEnabled(false); 
     letsMeetneedCashTv.setVisibility(View.GONE); 
    } 
    thisView.invalidate(); 
} 


.....................OTHER WIDGETS ABOVE...................... 
<RelativeLayout 
    android:layout_width="match_parent" 
    android:layout_height="56dp" 
    android:layout_alignParentBottom="true" 
    android:id="@+id/frameForNeedCashStuff"> 

    <RelativeLayout 
     android:layout_width="match_parent" 
     android:layout_height="56dp" 
     android:visibility="gone"> 
     <TextView 
      android:layout_width="200dp" 
      android:layout_height="@dimen/abc_action_bar_default_height_material" 
      android:layout_alignParentLeft="true" 
      android:background="@color/ColorPrimary" 
      android:gravity="center" 
      android:id="@+id/tv_need_cash_cancel" 
      android:text="@string/button_cancel" 
      android:textSize="@dimen/text_sizes_small" 
      android:textColor="@color/White" 
      android:visibility="gone"/> 

     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="@dimen/abc_action_bar_default_height_material" 
      android:layout_alignParentRight="true" 

      android:background="@color/ColorPrimary" 
      android:gravity="center" 
      android:id="@+id/tv_need_cash_lets_meet" 
      android:text="Let's Meet" 
      android:textSize="@dimen/text_sizes_small" 
      android:textColor="@color/White" 
      android:visibility="gone"/> 
    </RelativeLayout> 

    <TextView 
     android:layout_width="match_parent" 
     android:layout_height="@dimen/abc_action_bar_default_height_material" 
     android:background="@color/ColorPrimary" 
     android:layout_alignParentBottom="true" 
     android:gravity="center" 
     android:id="@+id/tv_need_cash" 
     android:text="@string/need_cash" 
     android:textSize="@dimen/text_sizes_small" 
     android:textColor="@color/White" 
     android:visibility="visible" /> 
</RelativeLayout> 
.....................OTHER WIDGETS BELOW...................... 

EDIT

public View onCreateView(LayoutInflater inflater, ViewGroup container, 
          Bundle savedInstanceState) { 
     // Inflate the layout for this fragment 
     View thisView = inflater.inflate(R.layout.fragment_map, container, false); 
return thisView; 
} 

EDIT點擊之前2

片段視圖,其示出了保持現金的TextView

Fragment view before clicking which is showing the Keep Cash TextView

0點擊後

片段視圖,其沒有顯示 '取消' 和 '滿足' TextViews

Fragment view after clicking which is NOT showing the 'Cancel' and 'Meet' TextViews

**編輯3 ** 指定ID到外RelativeLayout的和無效的它

public void toggleVisibility() { 
     if(needCashTv.getVisibility() == View.VISIBLE) { 
      //needCashTv.setEnabled(false); 
      needCashTv.setVisibility(View.GONE); 

      frameForNeedCashStuff.setVisibility(View.VISIBLE); 
      //cancelNeedCashTv.setEnabled(true); 
      cancelNeedCashTv.setVisibility(View.VISIBLE); 
      //letsMeetneedCashTv.setEnabled(true); 
      letsMeetneedCashTv.setVisibility(View.VISIBLE); 
      frameForNeedCashStuff.bringToFront(); 
     } 
     else { 
      //needCashTv.setEnabled(true); 
      needCashTv.setVisibility(View.VISIBLE); 
      needCashTv.bringToFront(); 

      frameForNeedCashStuff.setVisibility(View.GONE); 
      //cancelNeedCashTv.setEnabled(false); 
      cancelNeedCashTv.setVisibility(View.GONE); 
      //letsMeetneedCashTv.setEnabled(false); 
      letsMeetneedCashTv.setVisibility(View.GONE); 
     } 
     ((ViewGroup)frameForNeedCashStuff2.getParent()).invalidate(); 
     //thisView.invalidate(); 
    } 

修復後的工作代碼 編輯4

public void toggleVisibility() { 
    if(needCashTv.getVisibility() == View.VISIBLE) { 
     needCashTv.setVisibility(View.GONE); 

     frameForNeedCashStuff2.setVisibility(View.VISIBLE); 
    } 
    else { 
     needCashTv.setVisibility(View.VISIBLE); 
     needCashTv.bringToFront(); 

     frameForNeedCashStuff2.setVisibility(View.GONE); 
    } 
    ((ViewGroup)frameForNeedCashStuff.getParent()).invalidate(); 
} 


<FrameLayout 
    android:layout_width="match_parent" 
    android:layout_height="56dp" 
    android:layout_alignParentBottom="true" 
    android:id="@+id/frameForNeedCashStuff"> 

    <RelativeLayout 
    android:layout_width="match_parent" 
    android:layout_height="56dp" 
    android:visibility="gone" 
    android:id="@+id/frameForNeedCashStuff2"> 
    <TextView 
     android:layout_width="200dp" 
     android:layout_height="@dimen/abc_action_bar_default_height_material" 
     android:layout_alignParentLeft="true" 
     android:background="@color/ColorPrimary" 
     android:gravity="center" 
     android:id="@+id/tv_need_cash_cancel" 
     android:text="@string/button_cancel" 
     android:textSize="@dimen/text_sizes_small" 
     android:textColor="@color/White"/> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="@dimen/abc_action_bar_default_height_material" 
     android:layout_alignParentRight="true" 

     android:background="@color/ColorPrimary" 
     android:gravity="center" 
     android:id="@+id/tv_need_cash_lets_meet" 
     android:text="Let's Meet" 
     android:textSize="@dimen/text_sizes_small" 
     android:textColor="@color/White"/> 
    </RelativeLayout> 

    <TextView 
    android:layout_width="match_parent" 
    android:layout_height="@dimen/abc_action_bar_default_height_material" 
    android:background="@color/ColorPrimary" 
    android:layout_alignParentBottom="true" 
    android:gravity="center" 
    android:id="@+id/tv_need_cash" 
    android:text="@string/need_cash" 
    android:textSize="@dimen/text_sizes_small" 
    android:textColor="@color/White" 
    android:visibility="visible" /> 
</FrameLayout> 
+0

你可以嘗試使父視圖無效而不是實際視圖。 ((ViewGroup)getParent())。invalidate(); –

+0

@bastienpinaquy您可以確認其父視圖,佈局或小工具。 – dearvivekkumar

+0

嘗試此 '((ViewGroup)frameForNeedCashStuff.getParent())。invalidate();'但仍面臨同樣的問題。 – dearvivekkumar

回答

0

RelativeLayout包裹TextView S(tv_need_cash_canceltv_need_cash_lets_meet設置爲 '走了'。

如果您只是切換子視圖的可見性,它將不會顯示。您是否確定需要View.invalidate()?如果您只是切換可視性,則沒有必要。

+0

調用thisView.invalidate(); – dearvivekkumar

+0

此視圖實際上是由inflater返回的視圖引用 – dearvivekkumar

+0

您是否檢查過它實際上爲您做了什麼?我真的不認爲這是必要的。你是否按照我的建議設置「RelativeLayout」爲可見狀態? –

相關問題