向上滚动时StaggeredGridLayout会混乱

时间:2015-08-31 12:59:30

标签: android android-recyclerview recycler-adapter staggered-gridview

我为StaggeredGridLayout制作了此RecyclerViewenter image description here

以下是代码:

 @Override
public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, final int position) {

    Photo photo = mPhotos.get(position);

    TextView titleView = ((CellViewHolder) viewHolder).titleTextView;
        titleView.setText(photo.getTitle());

    TextView subTitleView = ((CellViewHolder) viewHolder).subtitleTextView;
        subTitleView.setText(photo.getName());

    Picasso.with(mContext).load(photo.getPhotoUrl()).into(((CellViewHolder) viewHolder).imageView);

    ImageView userImageOverlay = ((CellViewHolder) viewHolder).userImageOverlay;

    StaggeredGridLayoutManager.LayoutParams layoutParams = (StaggeredGridLayoutManager.LayoutParams) viewHolder.itemView.getLayoutParams();

    if (position == 0 || position % 4 == 0) {
        layoutParams.setFullSpan(true);
        layoutParams.height = Math.round(Utils.convertDpToPixel(202.67f, mContext));
        titleView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 21.67f);
        subTitleView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 12.00f);
        userImageOverlay.setVisibility(View.VISIBLE);
    } else if ((position - 1) % 4 == 0) {
        layoutParams.setFullSpan(false);
        layoutParams.height = Math.round(Utils.convertDpToPixel(360.00f, mContext));
        titleView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 15.00f);
        subTitleView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 10.00f);
        userImageOverlay.setVisibility(View.GONE);
    } else {
        layoutParams.setFullSpan(false);
        layoutParams.height = Math.round(Utils.convertDpToPixel(180.00f, mContext));
        titleView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 15.00f);
        subTitleView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 10.00f);
        userImageOverlay.setVisibility(View.GONE);
    }
}

除非我将它向上滚动,否则一切正常。它按位置按降序绘制单元格,并在网格中留下空格:

enter image description here

任何想法如何保持相同的模式甚至滚动它?

2 个答案:

答案 0 :(得分:3)

试试这个:

StaggeredGridLayoutManager manager = new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL);
manager.setGapStrategy(StaggeredGridLayoutManager.GAP_HANDLING_MOVE_ITEMS_BETWEEN_SPANS);
recyclerView.setLayoutManager(manager);

或:change position when scrolling

或:github project Picasso/Glide-RecyclerView-StaggeredGridLayoutManager

答案 1 :(得分:1)

您需要设置gap strategy

mLayoutMang.setGapStrategy(StaggeredGridLayoutManager.GAP_HANDLING_MOVE_ITEMS_BETWEEN_SPANS);
滚动状态更改为空闲时,

GAP_HANDLING_MOVE_ITEMS_BETWEEN_SPANS将更新layous