2015-12-01 19 views
0

我是新來的android和嘗試構建一個android應用程序。Android圖像視圖顯示setVisibility不見了

我將兩個圖像視圖一起堆疊。我想要做的是當用戶點擊頂部圖像時,最下面的圖像出現。當用戶點擊底部圖像時,出現頂部圖像。

問題是我點擊頂部圖像後,底部圖像顯示了一秒,然後頂部圖像再次出現。

下面是代碼:

View view = inflater.inflate(R.layout.list_item, null); 
ImageView top = (ImageView) view.findViewById(R.id.top); 
top.setOnClickListener(new View.OnClickListener() { 
     public void onClick(View v) { 
       View parent = (View)v.getParent(); 
       ImageView top =    
          (ImageView)parent.findViewById(R.id.top); 
       top.setVisibility(View.GONE); 

       ImageView bottom =  
          (ImageView)parent.findViewById(R.id.bottom); 
       bottom.setVisibility(View.VISIBLE); 
     } 
    }); 

    ImageView bottom = (ImageView) view.findViewById(R.id.bottom); 
    bottom.setOnClickListener(new View.OnClickListener() { 
     public void onClick(View v) { 
       View parent = (View)v.getParent(); 
       ImageView top = 
         (ImageView)parent.findViewById(R.id.top); 
       top.setVisibility(View.VISIBLE); 

       ImageView bottom =   
         (ImageView)parent.findViewById(R.id.bottom); 
       bottom.setVisibility(View.GONE); 
     } 
    }); 

這裏是佈局文件

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="horizontal" android:layout_width="match_parent" 
    android:layout_height="match_parent"> 
    <ImageView 
      android:id="@+id/bottom" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:layout_alignParentRight="true" 
      android:src="@drawable/stop" /> 

     <ImageView 
      android:id="@+id/top" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:layout_alignParentRight="true" 
      android:src="@drawable/ok" /> 
</RelativeLayout> 
+0

首先這是什麼觀點!!! ImageView iv =(ImageView)view.findViewById(R.id.expand); –

+0

我修改了我發佈的代碼。 ImageView iv是頂部的圖像。 – cui

回答

0
import android.app.Activity; 
import android.os.Bundle; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.view.View; 
import android.widget.ImageView; 

public class MainActivity extends Activity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     ImageView top = (ImageView) findViewById(R.id.top); 
     top.setOnClickListener(new View.OnClickListener() { 
      public void onClick(View v) { 
       View parent = (View) v.getParent(); 
       ImageView top = (ImageView) parent.findViewById(R.id.top); 
       top.setVisibility(View.GONE); 

       ImageView bottom = (ImageView) parent.findViewById(R.id.bottom); 
       bottom.setVisibility(View.VISIBLE); 
      } 
     }); 

     ImageView bottom = (ImageView) findViewById(R.id.bottom); 
     bottom.setOnClickListener(new View.OnClickListener() { 
      public void onClick(View v) { 
       View parent = (View) v.getParent(); 
       ImageView top = (ImageView) parent.findViewById(R.id.top); 
       top.setVisibility(View.VISIBLE); 

       ImageView bottom = (ImageView) parent.findViewById(R.id.bottom); 
       bottom.setVisibility(View.GONE); 
      } 
     }); 
    } 

} 

我想上面的代碼和它的工作,它不改變形象。

+0

上面的代碼每次用戶點擊都可以更改圖像嗎? – cui

+0

是的,它每次用戶點擊都會改變圖像 –