2017-03-23 20 views
0

私は画像とテキストを表示するScrollViewの中にListViewを持っています。テキストは完全に表示されます。しかし、イメージはそうではありません。リストをゆっくりとスクロールすると、すべての画像が確実に完全にロードされますが、もし私が本当に速く動くと、ロードされることはありません。私は方法を呼んでいるところをセットアップする方法だと思いますが、これはAndroidでの最初の1日です。以下は、リソース、レイアウト、およびクラスです。Android ListView in ScrollView画像を読み込むためにGlideを読み込みません。FireBaseは常に読み込まれません。

私は、リスト内のテキストとImageViewの追加方法

<?xml version="1.0" encoding="utf-8"?> 
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 
    <TableRow> 
     <ImageView 
      android:id="@+id/img" 
      android:layout_width="50dp" 
      android:layout_height="50dp"/> 

     <TextView 
      android:id="@+id/txt" 
      android:layout_width="wrap_content" 
      android:layout_height="50dp" /> 

    </TableRow> 

</TableLayout> 

それと一緒に行くためのクラス:

public class someClass extends ArrayAdapter<String>{ 

    private final Activity context; 
    private final ArrayList<String> web; 
    ImageView imageView; 
    FirebaseStorage storage = FirebaseStorage.getInstance(); 
    StorageReference storageRef = storage.getReference(); 
    final ArrayList<Uri> uriList = new ArrayList<>(); 
    public galveon_character_creation_spelllist(Activity context, 
         ArrayList<String> web) { 
     super(context, R.layout.someClass, web); 
     this.context = context; 
     this.web = web; 

    } 
    @Override 
    public View getView(int position, View view, ViewGroup parent) { 
     LayoutInflater inflater = context.getLayoutInflater(); 
     View rowView = inflater.inflate(R.layout.someClass, null, true); 
     TextView txtTitle = (TextView) rowView.findViewById(R.id.txt); 


     imageView = (ImageView) rowView.findViewById(R.id.img); 
     txtTitle.setText(web.get(position)); 
     getImages(position); 

     return rowView; 
    } 

    public void getImages(Integer position) { 
     storageRef.child("FolderRef/" + web.get(position) + ".png").getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() { 
      @Override 
      public void onSuccess(Uri uri) { 
       //imageView.setImageURI(null); 
       //imageView.setImageURI(uri); 
       Glide.with(getContext()) 
         .load(uri) // the uri you got from Firebase 
         .placeholder(R.drawable.unknown) //this would be your default image (like default profile or logo etc). it would be loaded at initial time and it will replace with your loaded image once glide successfully load image using url. 
         .diskCacheStrategy(DiskCacheStrategy.ALL) //using to load into cache then second time it will load fast. 
         .animate(R.anim.fade_in) // when image (url) will be loaded by glide then this face in animation help to replace url image in the place of placeHolder (default) image. 
         .fitCenter()//this method help to fit image into center of your ImageView 
         .into(imageView); //Your imageView variable 

      } 
     }).addOnFailureListener(new OnFailureListener() { 
      @Override 
      public void onFailure(@NonNull Exception exception) { 
       // Handle any errors 
       imageView.setImageResource(R.drawable.unknown); 
      } 
     }); 
    } 
} 

webは絵に並ぶだけのテキストです名前。アクティビティがリストをロードするときにも、表示されているすべての「セル」は読み込まれませんが、ゆっくりと下にスクロールしてバックアップすると(ゆっくりとスクロールすると)画像が表示されます。

もっと情報を投稿する必要がある場合はできます。まだAndroidの仕組みを学ぶ。

+1

ViewHolderパターンについての詳細を読むことができますか? .diskCacheStrategy(DiskCacheStrategy.SOURCE);を試しましたか? – Geek

+0

より良い結果を得るには、ListViewの代わりにRecyclerViewを使用するか、リストビューのみを使用する場合は、リストアダプタクラスのViewHolderを使用してください。 – DkThakur

答えて

1

try、グローバル変数ではなくgetImageにパラメータとしてimageviewを渡します。 例:

// inside getView 
    ImageView imageView = (ImageView) rowView.findViewById(R.id.img); 
    getImages(position, imageView); 

    // GetImage 
    public void getImages(Integer position, ImageView im) { 
     .... 
    } 
+0

これは完全に機能しました! – Torewin

+0

@Torewinハッピーコーディング – UdeshUK

1

まず最初に、あなたのリストにViewHolderパターンを実装するかRecyclerViewを使用することが良いです。あなたの現在の状況では、必要以上にビューを何度も膨らませています。ビューが複雑すぎると、アプリケーションのパフォーマンスが低下する可能性があります。あなたは、リストビューが自分自身をscolling管理する場合、なぜあなたはscrollview内のListViewを置けばいいhere

@Override 
public View getView(int position, View convertView, ViewGroup parent) { 

    ViewHolder holder; 
    if (convertView == null) { 
     //Inflate the view once if the view is null 
     LayoutInflater inflater = context.getLayoutInflater(); 
     convertView = inflater.inflate(R.layout.someClass, null, true); 
     holder = new ViewHolder(); 
     holder.txtTitle = (TextView) convertView.findViewById(R.id.txt) 
     holder.imageView = (ImageView) convertView.findViewById(R.id.img); 
     convertView.setTag(holder); 
    } 
    else { 
     //retrieve the reference of the inflated view 
     holder = (ViewHolder) convertView.getTag(); 
    } 

    holder.txtTitle.setText(web.get(position)); 
    getImages(holder.imageView, position); 
    return convertView; 
} 

public void getImages(ImageView imageView, int position) { 
    storageRef.child("FolderRef/" + web.get(position) + ".png").getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() { 
     @Override 
     public void onSuccess(Uri uri) { 
      //imageView.setImageURI(null); 
      //imageView.setImageURI(uri); 
      Glide.with(getContext()) 
        .load(uri) // the uri you got from Firebase 
        .placeholder(R.drawable.unknown) //this would be your default image (like default profile or logo etc). it would be loaded at initial time and it will replace with your loaded image once glide successfully load image using url. 
        .diskCacheStrategy(DiskCacheStrategy.ALL) //using to load into cache then second time it will load fast. 
        .animate(R.anim.fade_in) // when image (url) will be loaded by glide then this face in animation help to replace url image in the place of placeHolder (default) image. 
        .fitCenter()//this method help to fit image into center of your ImageView 
        .into(imageView); //Your imageView variable 

     } 
    }).addOnFailureListener(new OnFailureListener() { 
     @Override 
     public void onFailure(@NonNull Exception exception) { 
      // Handle any errors 
      imageView.setImageResource(R.drawable.unknown); 
     } 
    }); 
} 

static class ViewHolder { 
    TextView txtTitle; 
    ImageView imageView; 
} 
+0

この情報をお寄せいただきありがとうございます。私はこのメソッドを使用して、すべての情報を書き換えて構造化します。 – Torewin

関連する問題