我在我的应用程序中使用RecyclerView来显示来自服务器的图像,我使用交错网格布局以交错格式排列图像,我也使用通用图像加载器来加载图像。我的问题是,位置图像在从服务器加载时交换,例如,第二列中显示的图像将自动更改为第一列,反之亦然。我无法理解其原因,任何人都可以提供帮助吗?提前感谢。
Recycler 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);
}
答案 0 :(得分:1)
正如我在这里怀疑的那样,事情与View Recycling
有关,你可以在SO中找到很多关于这个的帖子,我发布了一些你可以尝试的代码,看看是否有帮助
在类级别声明DisplayImageOptions options
,只在RecyclerViewAdapter()
构建器内部初始化,目前在加载图片时没有使用此options
,因此UIL
将无法加载缓存图像。因此,在加载图像时,您应该这样做:
imageLoader.displayImage(item,recipeImage,options);
另外,如果上述方法不起作用,您也可以尝试以下方法。
在RecyclerViewHolder
课程中添加一个方法,例如:
public void bind(String item){
// Set your image here
imageLoader.displayImage(item,recipeImage,options);
}
并在onBindViewHolder()
中调用此方法,例如&gt;
// imageLoader.displayImage(itemList.get(position), holder.recipeImage);
holder.bind(itemList.get(position))
如果有帮助请告诉我
答案 1 :(得分:0)
我在交错网格视图中遇到了相同的行为,它似乎与在布局阶段如何放置项目有关。
默认策略为GAP_HANDLING_MOVE_ITEMS_BETWEEN_SPANS
,根据documentation,可以在布局内移动项目,以隐藏因项目大小不同而可能出现的差距。
您可以使用setGapStrategy (GAP_HANDLING_NONE)
更改差距策略来禁用此行为。这将停止更改列的项目,但最终可能会在列的开头或结尾处留下更大的间隙。