我遇到过这个问题
使用交错的网格视图,它有两列和单列的组合。这样的东西
一切正常,直到我向上和向下滚动几次然后随机改变为这样的东西
更多的卷轴,它最终像这样
我不知道为什么和它非常随机。每次显示页面时我都尝试做一个notifyDataSetChanged,并且似乎什么都不做
这是我的信息中心的代码
public class DashboardStaggered extends Fragment {
RecyclerView mRecyclerView;
View v;
MasonryAdapter adapter;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Log.d("MASONRY", "onCreateView");
super.onCreate(savedInstanceState);
if(v==null) {
Log.d("MASONRY", "v is NULL recreating");
v = inflater.inflate(R.layout.activity_dashboard_staggered, container, false);
setupRecyclerView();
}else{
Log.d("MASONRY", "v is NOT NULL");
}
return v;
}
private void setupRecyclerView(){
mRecyclerView = (RecyclerView) v.findViewById(R.id.masonry_grid);
mRecyclerView.setLayoutManager(new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL));
adapter = new MasonryAdapter(getActivity(),this);
mRecyclerView.setAdapter(adapter);
SpacesItemDecoration decoration = new SpacesItemDecoration(12);
mRecyclerView.addItemDecoration(decoration);
adapter.notifyDataSetChanged();
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
}
@Override
public void onResume() {
super.onResume();
Log.d("MASONRY", "onResume");
if(mRecyclerView==null) {
setupRecyclerView();
}
}
@Override
public void onPause() {
super.onPause();
Log.d("MASONRY", "onPause");
}
}
如果需要,我也可以发布适配器。任何建议都受到高度赞赏。
此处的适配器代码(不想让帖子凌乱)http://pastebin.com/QkZbjjp1
答案 0 :(得分:0)
正如杰森在评论中提到的那样,这是itemgetviewtype中的一个错误,逻辑错误比什么都重要。但经验教训,getItemViewType的更新代码是
@Override
public int getItemViewType(int position) {
// Return type based on position
return tileDefinitions.get(position).getTileType();
}
感谢大家指出。
答案 1 :(得分:0)
这是因为当您向上和向下滚动时,持有者无法识别图像视图的宽度和高度。当您向下滚动时它会清除上部视图,反之亦然。
像这样使用:
@Override
public void onBindViewHolder(ViewHolder viewHolder, int position) {
MyViewHolder vh = (MyViewHolder) viewHolder;
ImageModel item = imageModels.get(position);
RelativeLayout.LayoutParams rlp = (RelativeLayout.LayoutParams) vh.imageView.getLayoutParams();
float ratio = item.getHeight() / item.getWidth();
rlp.height = (int) (rlp.width * ratio);
vh.imageView.setLayoutParams(rlp);
vh.positionTextView.setText("pos: " + position);
vh.imageView.setRatio(item.getRatio());
Picasso.with(mContext).load(item.getUrl()).placeholder(PlaceHolderDrawableHelper.getBackgroundDrawable(position)).into(vh.imageView);
}
清楚看到这个链接: Picasso/Glide-RecyclerView-StaggeredGridLayoutManager