Glide Imgae加载在recyclerview android中花费了太多时间

时间:2017-09-14 07:04:23

标签: android android-recyclerview android-glide image-loading imageurl

在我的应用程序中,我实现了一个recyclerview小部件来显示userimage,名称和公司名称。我已将文本信息存储到Realm DB中,这些信息在启动应用程序后立即显示。但对于图像加载我使用Glide图像库。但问题是,只要我不通过整个Recyclerview,图像就不会在按钮点击后加载。所以,在点击了recyclerview之后,如果我只看到前10行,它就会一个接一个地加载第一张图片。然后当我返回到其他页面并且如果没有互联网连接时,其他图像不会加载。图像字段为空。是否有任何方法可以通过滑行更快地加载图像。

我在适配器类中的代码

public class MyColleaguesAdapter extends RecyclerView.Adapter<MyColleaguesAdapter.ColleagueHolder>{

    public static String TAG = MyColleaguesAdapter.class.getSimpleName();

    protected RequestManager glideManager; //make glideManager keeping
    private List<MyColleagueModel> myColleague;


    private Context context;

    public interface ColleagueListListener {
    }

    public MyColleaguesAdapter(List<MyColleagueModel> colleagues,Context context) {
        this.context=context;
        myColleague = colleagues;
        glideManager = Glide.with(context);
    }


    // create new views (invoked by the layout manager)
    @Override
    public ColleagueHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        View view = LayoutInflater.from(parent.getContext()).inflate( R.layout.colleage_row,parent,false);
        return new ColleagueHolder(view);
    }
    @Override
    public void onBindViewHolder(final ColleagueHolder holder, int position) {
        app_preferences = PreferenceManager.getDefaultSharedPreferences(context);

        final MyColleagueModel currentColleague = myColleague.get(position);

        holder.colleagueName.setText(currentColleague.getName());
        holder.companyName.setText(currentColleague.getCompany());
        holder.jobTitle.setText(currentColleague.getTitle());

        holder.cardView.setCardBackgroundColor(Constant.color);



         Glide.with( holder.itemView.getContext() )
                    .load( Constants.HTTP.PHOTO_URL + currentColleague.getMail() 
  )
                    .thumbnail( 0.5f )
                    .override( 200, 200 )
                    .diskCacheStrategy( DiskCacheStrategy.ALL )
                    .into( holder.colleaguePicture );


        holder.cardView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                Intent i=new Intent(context,DetailMyColleague.class);
                i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                i.putExtra("IMAGE_URL",Constants.HTTP.PHOTO_URL + currentColleague.getMail());

                context.startActivity(i);
            }
        });

    }


    @Override
    public int getItemCount() {
        return myColleague.size();
    }

    public class ColleagueHolder extends RecyclerView.ViewHolder{

        public CardView cardView;
        public ImageView colleaguePicture;
        public TextView colleagueName;
        public TextView  companyName;
        public TextView  jobTitle;

        public ColleagueHolder(View itemView) {
            super(itemView);
            colleaguePicture= itemView.findViewById(R.id.colleague_picture);
            colleagueName= itemView.findViewById(R.id.colleague_name);
            companyName= itemView.findViewById(R.id.company_name);
            jobTitle= itemView.findViewById(R.id.job_title);
            cardView= itemView.findViewById(R.id.cardview_user);

        }
    }

在主要活动中

private RecyclerView recyclerView;
private MyColleaguesAdapter adapter;
private List<MyColleagueModel> myColleagueList = new ArrayList<>();
private ColleagueController mController;
private ColleagueResApiManager mApiManager;

private Realm colleagueRealm;
private LinearLayoutManager layoutManager;

 @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.mycolleagues_layout);
    CookieHandler.setDefault( cookieManager );

     colleagueConfigViews();

}

public void colleagueConfigViews() {
    recyclerView = this.findViewById(R.id.colleagues_recycler);
    recyclerView.setHasFixedSize(true);
    recyclerView.setLayoutManager(new LinearLayoutManager(MyColleaguesPage.this));
    recyclerView.setRecycledViewPool(new RecyclerView.RecycledViewPool());

    colleagueRealm = Realm.getDefaultInstance();

    RealmResults<MyColleagueModel> results = colleagueRealm.where(MyColleagueModel.class).findAll( );

    for (int i = 0; i < results.size(); i++) {

        myColleagueList.add(results.get(i));
    }
    adapter = new MyColleaguesAdapter(myColleagueList,getApplicationContext());

    recyclerView.setAdapter(adapter);
}

3 个答案:

答案 0 :(得分:0)

根据my above comments Glide花时间从网址加载图片,您可以使用占位符在加载时显示图片

    <?xml version="1.0" encoding="utf-8"?>
   <resources>

   <style name="MyCustomTheme" 
 parent="Theme.AppCompat.DayNight.DarkActionBar">
    <item name="android:textColor">#2268ff</item>
    <item name="android:colorPrimary">@color/colorAccent</item>




   </style>

 </resources>

答案 1 :(得分:0)

我不确定,但你可以尝试下面。 我在我的应用中使用了以下代码。没有速度问题..

public class MyRecyclerAdapter extends RecyclerView.Adapter<ColleagueHolder > {    

   protected RequestManager glideManager; //make glideManager keeping

   protected MyRecyclerAdapter(Context context ....Activity or Fragment) {
       mContext = context;
       glideManager = Glide.with((Fragment or Activity) object);
   }

   @Override
   public void onBindViewHolder(final ColleagueHolder holder, int position) {
      holder.colleagueName.setText(currentColleague.getName());
      holder.companyName.setText(currentColleague.getCompany());
      holder.jobTitle.setText(currentColleague.getTitle());
      holder.colleaguePicture.setImageResource( R.drawable.profile_image );

      glideManager
            .load(Constants.HTTP.PHOTO_URL + currentColleague.getMail() )
            .placeholder(R.drawable.myholder)
            .fitCenter()
            .override(200, 200)
            .into(holder.colleaguePicture);

     holder.cardView.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View view) {

              Intent i=new Intent(context,DetailMyColleague.class);
              i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
              i.putExtra("IMAGE_URL",Constants.HTTP.PHOTO_URL + currentColleague.getMail());
              .....
              context.startActivity(i);
    }
});
   }
}

答案 2 :(得分:0)

@Override
public void onViewAttachedToWindow(@NonNull RecyclerView.ViewHolder holder) {
    super.onViewAttachedToWindow(holder);
    Glide.with(context)
         .load(URL)
         .into(holder.imageView);
}