滚动时,带有RecycleView的StaggeredLayoutManger重新布局项目

时间:2016-06-26 15:04:13

标签: android android-recyclerview

我正在尝试使用StaggeredLayoutManager,但滚动列表时,项目会重新布局。 (图像来自互联网,并在带有ImageLoader的ImageView中显示) 有视频(我不知道为什么我的Android Studio无法录制屏幕) https://www.youtube.com/watch?v=EwwS2bkgx4I

任何人都知道为什么请帮助我。感谢

我的代码。 布局代码。

<com.aspsine.swipetoloadlayout.SwipeToLoadLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/swipeToLoadLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">

<com.beautyapps.beautyasiangirls.ui.views.RefreshHeaderView
    android:id="@id/swipe_refresh_header"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center">

    <com.beautyapps.beautyasiangirls.ui.views.CircularSmile
        android:id="@+id/cs_header_icon"
        android:layout_centerInParent="true"
        android:layout_margin="@dimen/activity_horizontal_margin"
        android:layout_width="@dimen/refresh_header_icon_size"
        android:layout_height="@dimen/refresh_header_icon_size"/>
</com.beautyapps.beautyasiangirls.ui.views.RefreshHeaderView>

<android.support.v7.widget.RecyclerView
    android:id="@+id/swipe_target"
    android:layout_width="match_parent"
    app:layoutManager="android.support.v7.widget.StaggeredGridLayoutManager"
    tools:listitem="@layout/item_image"
    android:layout_height="match_parent"/>

<com.beautyapps.beautyasiangirls.ui.views.LoadMoreFooterView
    android:id="@id/swipe_load_more_footer"
    android:layout_width="match_parent"
    android:layout_height="100dp"
    android:gravity="center"/>

片段代码:

RecyclerView rcImages = (RecyclerView) rootView.findViewById(R.id.swipe_target);

    StaggeredGridLayoutManager gridLayoutManager = new StaggeredGridLayoutManager(3, StaggeredGridLayoutManager.VERTICAL);
    gridLayoutManager.setGapStrategy(StaggeredGridLayoutManager.GAP_HANDLING_MOVE_ITEMS_BETWEEN_SPANS);

    SpacesItemDecoration itemDecoration = new SpacesItemDecoration(getResources().getDimensionPixelOffset(R.dimen.staggered_item_space));

    mRcAdapter = new StaggeredRecycleViewAdapter(getActivity(), mImages);

    rcImages.setLayoutManager(gridLayoutManager);
    rcImages.setAdapter(mRcAdapter);
    rcImages.addItemDecoration(itemDecoration);

适配器代码:

public class StaggeredRecycleViewAdapter extends RecyclerView.Adapter<StaggeredRecycleViewAdapter.ImageHolder> {

private List<BAGImage> mUrls;
private Context mContext;

public StaggeredRecycleViewAdapter(Context context, List<BAGImage> urls) {
    mUrls = urls;
    mContext = context;
}

@Override
public ImageHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View root = LayoutInflater.from(mContext).inflate(R.layout.item_image, parent, false);
    return new ImageHolder(root);
}

@Override
public void onBindViewHolder(ImageHolder holder, int position) {
    holder.bindView(mUrls.get(position));
}

@Override
public int getItemCount() {
    return null == mUrls ? 0 : mUrls.size();
}

public class ImageHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
    private BAGImage mImageUrl;
    public ImageView mImgImage;

    public ImageHolder(final View itemView) {
        super(itemView);
        mImgImage = (ImageView) itemView.findViewById(R.id.imgView);
        itemView.setOnClickListener(this);
    }

    public void bindView(BAGImage image) {
        mImageUrl = image;
        ImageLoader.getInstance().displayImage(image.getThumbnailUrl(), mImgImage);
    }

    @Override
    public void onClick(View view) {
        Intent toView = new Intent(mContext, ShowImageActivity.class);
        toView.putExtra(ShowImageActivity.ARG_IMAGE_PATH, mImageUrl.getHighResolutionUrl());
        toView.putExtra(ShowImageActivity.ARG_IS_FROM_COLLECTION, false);
        mContext.startActivity(toView);
    }
}

}

0 个答案:

没有答案