2014-09-24 99 views
0

我有一個相對佈局,它有兩個孩子,一個ImageView和一個ProgressBar。在加載ImageView的圖像時,我想顯示旋轉進度條,一旦圖像完成,我想隱藏進度條並顯示圖像。我永遠無法看到圖像,但進度條顯示得很好。我覈實,圖像被加載,這似乎是一個佈局的問題,但我是無法理解......在RelativeLayout中顯示/隱藏ImageView

這是我的佈局文件

<?xml version="1.0" encoding="utf-8"?> 
    <RelativeLayout 
     xmlns:android="http://schemas.android.com/apk/res/android" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:gravity="center"> 
     <ImageView 
      android:layout_width="110dp" 
      android:layout_height="110dp" 
      android:visibility="visible"/> 
     <ProgressBar 
      android:layout_width="110dp" 
      android:layout_height="110dp"/> 
    </RelativeLayout> 

時加載圖像...

final RelativeLayout relLayout = (RelativeLayout)convertView; 
final ImageView imageView = (ImageView)relLayout.getChildAt(0); 
final ProgressBar bar = (ProgressBar)relLayout.getChildAt(1); 

bar.setIndeterminate(true); 
bar.setVisibility(View.VISIBLE); 
imageView.setVisibility(View.GONE); 

而我ImageLoader的的的onSuccess內部

bar.setVisibility(View.GONE); 
imageView.setVisibility(View.VISIBLE); 
imageView.setImageBitmap(resizedBitmap); 
relLayout.requestLayout(); // also tried invalidate(), and nothing at all 

幫助?

回答

1

只要改變

imageView.setVisibility(View.GONE);

imageView.setVisibility(View.INVISIBLE);

還要檢查this link.

+0

這實際上是正確的答案... 我很驚訝。我的印象是,將其設置爲INVISIBLE仍然會保留佈局空間,這就是爲什麼我去了無效。只要我切換到INVISIBLE,我就可以輕鬆地隱藏/顯示微調或圖像 – user2997084 2014-09-25 16:44:31

+0

我曾經遇到過這個問題。我做到了,解決了問題。我不明白爲什麼這個解決方案降低了投票率。怪異的人... – 2014-09-25 17:46:12

0

首先在你的XML,就把這行你的ImageView 機器人:知名度= 「水漲船高」

然後到你的代碼使用asynkTask展現progressBarr當下載完成隱藏proogressBar並顯示ImageView的

public void getImage(){ 
    new AsyncTask<Void, Void, Boolean>(){ 
     @Override 
     protected Boolean doInBackground(Void... params){ 
     //Code to load image, take care don't change the UI because it can break the app 
     } 
     @Override 
     protected void onProgressUpdate(Integer... values){ 
     //Here you can update your progressBar 
     } 
     @Override 
     public void onPostExecute(Boolean result){ 
     //When the image finish the load here you can hide the progressBar and show the ImageView 
     bar.setVisibility(View.INVISIBLE); 
     imageView.setVisibility(View.VISIBLE); 
     } 
    }.execute(); 
} 

我希望幫助

0
Try to keep id for the views because when the item is being gone, then the position of the view will get changed 
please try like this 

Your Layout: 
<?xml version="1.0" encoding="utf-8"?> 
    <RelativeLayout 
     xmlns:android="http://schemas.android.com/apk/res/android" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:gravity="center"> 
     <ImageView 
      android:id="@+id/img_view" 
      android:layout_width="110dp" 
      android:layout_height="110dp" 
      android:visibility="visible"/> 
     <ProgressBar 
      android:id="@+id/progress_bar" 
      android:layout_width="110dp" 
      android:layout_height="110dp"/> 
    </RelativeLayout> 

In your java class: 

ImageView imgView = (ImageView)findviewbyId(R.id.img_view); 
ProgressBar progressBar = (ProgressBar)findviewById(R.id.progress_bar); 
imgView.setVisiblity(View.INVISIBLE); 
progressBar.setIndeterminate(true); 
In your oncomplete listener 
imgView.setVisibility(View.VISIBLE); 
progressBar.setVisibility(VIEW.INVISBLE);