-1

在我的應用程序中,我已經實現了一個recyclerview小部件來顯示userimage,名稱和公司名稱。我已經將文本信息存儲到Realm DB中,並且這些信息在啓動應用程序後立即顯示。但對於圖像加載,我正在使用Glide圖像庫。但問題是,只要點擊按鈕後圖像不加載,只要我不通過整個回收站查看。所以,如果我只看到前10行,點擊回收站後,它會一個接一個地加載第一個圖像。然後,當我回到其他頁面,如果互聯網連接不在那裏,其他圖像不加載。圖像字段爲空。有沒有什麼方法可以更快速地下載圖像並進行滑動。Glide Imgae加載需要花費太多時間在recyclerview android

我的適配器類代碼

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); 
} 
+0

它需要一定的時間來從URL加載圖像,你可以使用** **佔位符顯示圖像而載入 –

+0

嘗試.override(100,100) –

+0

@NileshRathod我怎樣才能在這裏使用佔位符。有什麼好處,使用這個,對不起,我在Android的發展新 – tamrezh21

回答

0

my above comments滑翔需要時間來從URL加載圖像,你可以使用佔位符顯示圖像而載入

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

如何在drawable文件夾中聲明R.drawable.ic_placeholder – tamrezh21

+0

@ tamrezh21只需在** res/drawable **文件夾中添加想要顯示的示例圖像,而不是通過** R.id.yourImageName ** –

+0

未解決問題。仍然有相同的問題 – tamrezh21

0

我不是su但你可以嘗試下面。 我在我的應用程序中使用下面的代碼。沒有速度問題..

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); 
    } 
}); 
    } 
} 
+0

我在哪裏可以寫主要活動中的RequestManager和MyRecyclerAdapater方法。你能解釋更多 – tamrezh21

+0

我已經根據你的更新了我的代碼,我在那裏有活動。你能否請檢查並告訴我,如何在主要活動中調用適配器,還有什麼我應該做的,對不起,我很新,我正在開發 – tamrezh21

+0

我看到了你的更新源代碼。我不認爲使用recyclerView適配器沒有問題。所以我想要你檢查1.數據加載時間2.使數據變小(如果數據行是2000,使數據20)如果數據加載是問題,你必須使你的加載數據異步(如使用線程或AsyncTask或relam異步數據加載) –

相關問題