gpt4 book ai didi

android - 为什么 RecyclerView 中的交错网格布局元素在加载图像时会自动交换其位置?

转载 作者:行者123 更新时间:2023-11-30 01:49:38 24 4
gpt4 key购买 nike

我在我的应用程序中使用 RecyclerView 来显示来自服务器的图像,我使用交错网格布局以交错格式排列图像,我还使用 Universal Image Loader 加载图像。我的问题是,位置从服务器加载时图像会互换,例如,第二列中显示的图像会自动更改为第一列,反之亦然。我不明白这是为什么,有人能帮忙吗?提前致谢。

回收器 View 和 StaggeredGridLayout

     RecyclerView rView = (RecyclerView)view.findViewById(R.id.recycler_view);
rView.setHasFixedSize(true);
rView.setItemAnimator(new DefaultItemAnimator());
StaggeredGridLayoutManager stagGrid = new StaggeredGridLayoutManager(rowColumnValue,StaggeredGridLayoutManager.VERTICAL);
rView.setLayoutManager(stagGrid);

RecyclerView Adapter.java

  public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewHolders> {

private ArrayList<String> itemList;
private ArrayList<String> imageStatusList;
private List<ItemObject> imageList;
private Context context;
String [] imageURLList=null;
FragmentActivity fragmentActivity;
ImageLoader imageLoader;

public RecyclerViewAdapter(FragmentActivity activity, ArrayList<String> itemList,ArrayList<String> imageStatusList,String [] imageURLs) {

this.itemList = itemList;
this.imageStatusList=imageStatusList;

this.context = context;
this.fragmentActivity=activity;
imageLoader = ImageLoader.getInstance();
this.imageURLList=imageURLs;

DisplayImageOptions options = new DisplayImageOptions.Builder().cacheInMemory(true)
.cacheOnDisc(true).resetViewBeforeLoading(true).build();

// .showImageForEmptyUri(fallback)
// .showImageOnFail(fallback)
// .showImageOnLoading(fallback).build();
//imageList = imageLoader.displayImage(itemList,);



}

@Override
public RecyclerViewHolders onCreateViewHolder(ViewGroup parent, int viewType) {

View layoutView = LayoutInflater.from(parent.getContext()).inflate(R.layout.card_view_list, null);
RecyclerViewHolders rcv = new RecyclerViewHolders(layoutView,imageURLList,fragmentActivity);
return rcv;
}

@Override
public void onBindViewHolder(RecyclerViewHolders holder, int position) {


imageLoader.displayImage(itemList.get(position), holder.recipeImage);
imageStatusList.toString();

try {

if (imageStatusList.get(position).equals("approved")) {
holder.rimage.setImageDrawable(fragmentActivity.getResources().getDrawable(R.drawable.varified));
} else if (imageStatusList.get(position).equals("pending")) {
holder.rimage.setImageDrawable(fragmentActivity.getResources().getDrawable(R.drawable.pending));

}

}
catch (Exception e){
e.printStackTrace();
}


}

@Override
public int getItemCount() {

return this.itemList.size();

}
}

RecyclerViewHolder.java

public class RecyclerViewHolders extends RecyclerView.ViewHolder implements View.OnClickListener{


public ImageView recipeImage,rimage;
int likeButtonCount;
String [] imageUrls;
Context mContext;
public RecyclerViewHolders(View itemView,String [] urlList,Context mContext) {

super(itemView);
itemView.setOnClickListener(this);
recipeImage = (ImageView)itemView.findViewById(R.id.recipe_image);
rimage=(ImageView)itemView.findViewById(R.id.image_status_imageview);
imageUrls=urlList;
this.mContext=mContext;

}

@Override
public void onClick(View view) {
int position = getPosition();

ArrayList<String> imageUrlArrayList = new ArrayList<>();
imageUrlArrayList.add(imageUrls[position]);
imagePopupWindow(imageUrlArrayList);
Toast.makeText(view.getContext(), "Clicked = " + getPosition(), Toast.LENGTH_SHORT).show();
}

RecyclerViewAdapter(已编辑)

public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewHolders> {

private ArrayList<String> itemList;
private ArrayList<String> imageStatusList;
private List<ItemObject> imageList;
private Context context;
String [] imageURLList=null;
FragmentActivity fragmentActivity;
ImageLoader imageLoader;
DisplayImageOptions options ;

public RecyclerViewAdapter(FragmentActivity activity, ArrayList<String> itemList,ArrayList<String> imageStatusList,String [] imageURLs) {

this.itemList = itemList;
this.imageStatusList=imageStatusList;

this.context = context;
this.fragmentActivity=activity;
imageLoader = ImageLoader.getInstance();
this.imageURLList=imageURLs;

options = new DisplayImageOptions.Builder().cacheInMemory(true)
.cacheOnDisc(true).resetViewBeforeLoading(true)
.showImageForEmptyUri(fragmentActivity.getResources().getDrawable(R.drawable.wedcake))
.showImageOnFail(fragmentActivity.getResources().getDrawable(R.drawable.wedcake))
.showImageOnLoading(fragmentActivity.getResources().getDrawable(R.drawable.wedcake)).build();


}

onBindViewHolder.java

 @Override
public void onBindViewHolder(RecyclerViewHolders holder, int position) {


// imageLoader.displayImage(itemList.get(position), holder.recipeImage,options);


holder.bind(itemList.get(position));
imageStatusList.toString();

try {

if (imageStatusList.get(position).equals("approved")) {
holder.rimage.setImageDrawable(fragmentActivity.getResources().getDrawable(R.drawable.varified));
} else if (imageStatusList.get(position).equals("pending")) {
holder.rimage.setImageDrawable(fragmentActivity.getResources().getDrawable(R.drawable.pending));

}

}
catch (Exception e){
e.printStackTrace();
}


}

RecyclerViewHolder.java

public class RecyclerViewHolders extends RecyclerView.ViewHolder implements View.OnClickListener{


public ImageView recipeImage,rimage;
int likeButtonCount;
String [] imageUrls;
Context mContext;
ImageLoader imageLoader;
DisplayImageOptions options ;
public RecyclerViewHolders(View itemView,String [] urlList,Context mContext) {

super(itemView);
itemView.setOnClickListener(this);
recipeImage = (ImageView)itemView.findViewById(R.id.recipe_image);
rimage=(ImageView)itemView.findViewById(R.id.image_status_imageview);
imageUrls=urlList;
this.mContext=mContext;
imageLoader = ImageLoader.getInstance();
options = new DisplayImageOptions.Builder().cacheInMemory(true)
.cacheOnDisc(true).resetViewBeforeLoading(true)
.showImageForEmptyUri(mContext.getResources().getDrawable(R.drawable.wedcake))
.showImageOnFail(mContext.getResources().getDrawable(R.drawable.wedcake))
.showImageOnLoading(mContext.getResources().getDrawable(R.drawable.wedcake)).build();



}
@Override
public void onClick(View view) {
int position = getPosition();

ArrayList<String> imageUrlArrayList = new ArrayList<>();
imageUrlArrayList.add(imageUrls[position]);
imagePopupWindow(imageUrlArrayList);
Toast.makeText(view.getContext(), "Clicked = " + getPosition(), Toast.LENGTH_SHORT).show();
}


public void bind(String items){

imageLoader.displayImage(items, recipeImage, options);
}

最佳答案

正如我在这里所怀疑的那样,这件事与 View Recycling 有关,您可以在 SO 本身中找到很多关于此的帖子,我正在发布一些代码,您可以尝试看看是否有帮助

在类级别声明 DisplayImageOptions 选项,并在 RecyclerViewAdapter() 构造函数中初始化,目前您在加载图像时没有使用此 options ,因此 UIL 不会加载缓存的图像。所以在加载图像时你应该这样做:

imageLoader.displayImage(item,recipeImage,options);

或者你也可以尝试下面的方法,如果上面的方法不起作用。

在您的 RecyclerViewHolder 类中添加一个方法,例如:

public void bind(String item){
// Set your image here
imageLoader.displayImage(item,recipeImage,options);
}

并在您的 onBindViewHolder() 中像这样调用此方法 ->

//  imageLoader.displayImage(itemList.get(position), holder.recipeImage);
holder.bind(itemList.get(position))

如果有帮助请告诉我

关于android - 为什么 RecyclerView 中的交错网格布局元素在加载图像时会自动交换其位置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33251765/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com