在我的應用程序中,我已經實現了一個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);
}
它需要一定的時間來從URL加載圖像,你可以使用** **佔位符顯示圖像而載入 –
嘗試.override(100,100) –
@NileshRathod我怎樣才能在這裏使用佔位符。有什麼好處,使用這個,對不起,我在Android的發展新 – tamrezh21