android - Recycler View 在滚动时随机播放图像

标签 android android-recyclerview

我有一个回收站 View ,它正在使用 Universal Image Loader 进行填充。

@Override
public MasonryView onCreateViewHolder(ViewGroup parent, int viewType) {
    View layoutView = LayoutInflater.from(parent.getContext()).inflate(R.layout.grid_item, parent, false);
    MasonryView masonryView = new MasonryView(layoutView);
    //layoutView.setOnClickListener(new MyOnClickListener());
    //imageLoader.destroy();
    imageLoader.init(ImageLoaderConfiguration.createDefault(context));
    options = new DisplayImageOptions.Builder()
            .cacheInMemory(true)
            .resetViewBeforeLoading(true)
            .cacheOnDisk(true)
            .considerExifParams(false)
            .bitmapConfig(Bitmap.Config.RGB_565)
            .build();

    return masonryView;
}

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

    imageLoader
            .displayImage(IMAGE_URLS[position], holder.imageView, options, new SimpleImageLoadingListener() {
                @Override
                public void onLoadingStarted(String imageUri, View view) {
                    holder.donut_progress.setVisibility(View.VISIBLE);
                    holder.donut_progress.setProgress(0);
                }

                @Override
                public void onLoadingFailed(String imageUri, View view, FailReason failReason) {

                    holder.onLoadImage.setVisibility(View.GONE);
                }

                @Override
                public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
                    holder.onLoadImage.setVisibility(View.GONE);
                    holder.donut_progress.setVisibility(View.GONE);
                }
            }, new ImageLoadingProgressListener() {
                @Override
                public void onProgressUpdate(String imageUri, View view, int current, int total) {
                    holder.donut_progress.setProgress(Math.round(100.0f * current / total));
                }
            });


}

如您所见,Imageloader 和 DisplayOptions 是在 CreateView 中定义的。

图像已成功显示,但在滚动时图像会乱序。

以下是我的 custom_recycler_grid_item 的 xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:custom="http://schemas.android.com/apk/res-auto"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#00000000"
android:orientation="vertical">

    <ImageView
        android:id="@+id/img"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:adjustViewBounds="true"/>
    !-- android:scaleType="centerCrop" --!


    <ImageView
        android:id="@+id/onLoadImage"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:src="@drawable/camera_cross"
        android:layout_marginLeft="50dp"
        android:layout_marginTop="50dp"
        android:visibility="gone"/>

    <progressIndicators.DonutProgress
        android:layout_marginLeft="50dp"
        android:layout_marginTop="50dp"
        android:id="@+id/donut_progress"
        android:layout_width="50dp"
        android:layout_height="50dp"
        custom:donut_unfinished_stroke_width="1dp"
        custom:donut_finished_stroke_width="1dp"
        custom:donut_progress="50"/>

</FrameLayout>

我的主要 fragment ,有 recyclerView

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">

<android.support.v7.widget.RecyclerView
    android:id="@+id/masonry_grid"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_weight="0.1"
    app:layout_behavior="@string/appbar_scrolling_view_behavior" />


</LinearLayout>

不过,我也设置了 adapter.setHasStableIds(true);

最佳答案

我完全解决了这个问题。

问题出在我的 RecyclerWidget 高度和宽度上。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">

<android.support.v7.widget.RecyclerView
    android:id="@+id/masonry_grid"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_weight="0.1"
    app:layout_behavior="@string/appbar_scrolling_view_behavior" />


</LinearLayout>

我必须设置我的高度和宽度来包裹内容。

关于android - Recycler View 在滚动时随机播放图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38565688/

相关文章:

android - 主题不适用于 Android 上的 DialogFragment

java - 在 Recyclerview 中滚动后,以前的数据会更新

android - 如何将水平回收 View 居中居中?

android - 我希望 EditText 下的行保持不变

java - 如何在 Android 中使用 RecyclerView?

android - 获取点击的项目及其在 RecyclerView 中的位置

android - RecyclerView 增加高度动画不起作用

android - 2种设置 fragment 参数的方法有什么区别

java - Comparable 不排序对象

android - 当我使用 setAdapter() 时,Spinner onItemSelected 不起作用