0

我有一個具有兩個子佈局的RelativeLayout。我們稱之爲rel1和rel2。 Rel1在頂部有一個標題。然後在中心的個人資料圖片。和底部的兩個水平放置的按鈕。如何在點擊時隱藏佈局

Rel2有一個listview。 Rel2在父佈局的底部對齊。 Rel2佔用屏幕的50%,最初不可見。 當我點擊rel1中的一個按鈕時,rel2變得可見。 因爲rel2佔據了屏幕的一半,所以rel1的上半部分是可見的,rel1的下半部分被rel2覆蓋。

我的要求是,當我點擊rel1的上半部分(不包括rel2)時,rel2應該隱藏。

我該怎麼做。我應該在rel1佈局上添加onclick監聽器。如果我在rel1上添加onclick,當點擊rel1佈局的子項時會發生什麼。將點擊事件發送給其子女或rel1 ..

請幫忙。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:visibility="visible" > 

     <RelativeLayout 
android:id="@+id/rel1" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:paddingBottom="23dp" 
      android:paddingTop="12dp" > 

      <TextView 
       android:id="@+id/title" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
android:layout_alignParentTop="true" 
       android:ellipsize="end" 
       android:gravity="center_horizontal" 
       android:maxLines="1" 
       android:singleLine="true" 
       android:text="Title" 
       android:textSize="20sp" /> 

     <ImageView 
      android:id="@+id/center_image" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_centerInParent="true" 
      android:background="@drawable/Image" 
      android:contentDescription="center IMage" /> 

      <LinearLayout 
     android:id="@+id/buttonView" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_marginBottom="12dp" 
     android:baselineAligned="false" 
     android:orientation="horizontal" > 

      <ImageButton 
       android:id="@+id/button1" 
       android:layout_width="match_parent" 
       android:layout_height="60dp" 
       android:layout_marginRight="1dp" 
       android:background="@drawable/button1" 
       android:contentDescription="@string/str_avoid_warning"/> 
      <ImageButton 
       android:id="@+id/button2" 
       android:layout_width="match_parent" 
       android:layout_height="60dp" 
       android:layout_marginLeft="1dp" 
       android:background="@drawable/button2" 
       android:contentDescription="@string/str_avoid_warning"/> 
    </LinearLayout> 

 <RelativeLayout 
      android:id="@+id/rel2" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_alignParentBottom="true" 
      android:visibility="gone"> 

      <ListView 
       android:id="@+id/listview" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:cacheColorHint="@android:color/transparent" 
       android:fadingEdgeLength="7dp" 
       android:overScrollMode="never" 
       android:divider="@android:color/transparent" 
       android:dividerHeight="10.0sp" 
       android:requiresFadingEdge="vertical"> 
      </ListView> 
     </RelativeLayout> 

    </RelativeLayout> 
+0

使用它後面的match_parent視圖,並添加點擊監聽到它:) –

+0

我也想加入比賽父透明佈局說REL3,並使它當rel2變得可見時,可見其他明智的消息,並增加了onclick監聽器。只是不確定它是否是一個好主意,我的意思是它只是黑客。我想知道我是否可以找到更好的解決方案 –

+0

有令人毛骨悚然的解決方案,如在所有視圖中添加touchlistener並聽取它。但是看不見的佈局非常整齊。或者,如果您在父視圖和子視圖上都有onclick監聽器,則應在對話框 –

回答

0

使雙方REL1和REL2點擊添加android:clickable="true",然後添加一個OnClickListener到REL1。

+0

但是,如果我在rel1上添加onclick,然後如果在rel1中單擊任何子項(所有子項都是可點擊的),那麼點擊事件將同時發送到rel1和小孩點擊? –

0
<RelativeLayout 
     android:id="@+id/rel1" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:paddingBottom="23dp" 
     android:onClick="hideMe" 
     android:clickable=true 
     android:paddingTop="12dp" > 


public void hideMe(View view){ 
ViewGroup viewgroup = (ViewGroup) getLayoutInflater().inflate(R.layout.rel2, null); 
viewgroup.setVisibility(View,GONE);//CHECK THE OTHER OPTION BELOW 
} 

View.GONE -Everything被刪除,空間可用於其他部件 View.INVISIBLE -Everything將被刪除,但空間不可用於其他wigets

+0

我不明白。你要我隱藏rel1當我點擊rel1? 我想隱藏rel2當我點擊rel1 –

+0

@Pradeep庫馬爾我的壞。我現在編輯了答案 –

0

使第一RelativeLayout的點擊,並添加在第一clicklistener它會隱藏第二

Rel1.setOnClickListener(新View.OnClickListener(){ @覆蓋 公共無效的onClick(視圖v){如果(Rel2.getVisibility()== View.VISIBLE){ Rel2.setVisibility(View.GONE); } } });

1

嘗試這可能是幫助你

private RelativeLayout llRoot; 
private RelativeLayout llContent; 

llRoot = (RelativeLayout) findViewById(R.id.rel1);//in onCreate() 
llContent = (RelativeLayout) findViewById(R.id.rel2);//in onCreate() 

@Override 
public boolean dispatchTouchEvent(MotionEvent ev) { 
    Rect ContentBounds = new Rect(); 
    llRoot.getHitRect(ContentBounds); 

    if (ContentBounds.contains((int) ev.getX(), (int) ev.getY())) { 
     //Do Your stuff here 

    } 

    return super.dispatchTouchEvent(ev); 
}