在RelativeLayout中显示/隐藏ImageView

时间:2014-09-24 14:59:42

标签: android android-layout

我有一个包含两个子节点的相对布局,一个ImageView和一个ProgressBar。当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

帮助?

3 个答案:

答案 0 :(得分:1)

只需更改

imageView.setVisibility(View.GONE);

imageView.setVisibility(View.INVISIBLE);

同时检查this link.

答案 1 :(得分:0)

首先在您的XML中将此行放入ImageView android:visibility =“gone”

然后在你的代码中使用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();
}

我希望有所帮助

答案 2 :(得分: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);