ProgressBar在RecyclerView适配器中没有刷新

时间:2017-06-14 07:40:26

标签: java android android-recyclerview android-progressbar recycler-adapter

我遇到了progressBar.setProgress()progressBar.setMax()的问题。

我有两个项目,通常需要显示视频的进度,如附加屏幕截图。仅对于第一项,我设置了一些进度数据,我需要将进度设置为第二项的0(零)。但第二项ProgressBar未重置为零而不是此项仍然显示第一项的进度长度。

我在这里打印了项目的值:

// Item1
Media List total Duration : 84063
Media List total ProgressDuration : 19380
// Item2
Media List total Duration : 0
Media List total ProgressDuration : 0

适配器代码:

class ViewHolder extends RecyclerView.ViewHolder {
    TextView fileName;
    ImageView ivThumbNail;
    ProgressBar progressBar;
    View mainContainer;
    View view;

    ViewHolder(View itemView) {
        super(itemView);
        view = itemView;
        mainContainer = itemView.findViewById(R.id.main_container);
        fileName = (TextView) itemView.findViewById(R.id.tv_file_name);
        ivThumbNail = (ImageView) itemView.findViewById(R.id.iv_thumbnail);
        progressBar = (ProgressBar) itemView.findViewById(R.id.progress_bar);
    }
}

@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View itemView = LayoutInflater.from(context).inflate(R.layout.media_row, parent, false);
    return new ViewHolder(itemView);
}

@Override
public void onBindViewHolder(ViewHolder holder, int position) {
    final Media media = mediaList.get(position);

    String videoUrl = media.video_url;

    int totalDuration = VideoPositionVo.getTotalDuration(context, videoUrl, media.course_id);
    int progressDuration = VideoPositionVo.getResumedPosition(context, videoUrl, media.course_id);

    Log.d("Media List total Duration : " , totalDuration);
    Log.d("Media List total ProgressDuration : " , progressDuration);

    holder.progressBar.invalidate();
    holder.progressBar.setMax(totalDuration);
    holder.progressBar.setProgress(progressDuration);


    String sampleThumbNail = "https://i.vimeocdn.com/video/23566238_100x75.webp";
    aQuery.id(holder.ivThumbNail).image(sampleThumbNail, true, true, imageWidth, 0);
    holder.fileName.setText(media.video_label);

}

适配器行xml:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:clickable="true"
    android:elevation="4dp"
    android:foreground="?android:attr/selectableItemBackground"
    android:orientation="vertical"
    android:paddingBottom="4dp"
    android:paddingEnd="4dp"
    app:cardCornerRadius="2dp"
    app:cardElevation="2sp"
    app:cardUseCompatPadding="true">


    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="150dp"
        android:layout_gravity="center"
        android:gravity="center"
        android:orientation="vertical">

        <ImageView
            android:id="@+id/iv_thumbnail"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:clickable="false"
            android:scaleType="centerCrop"
            android:contentDescription="@string/media"
            android:visibility="visible" />

        <RelativeLayout
            android:id="@+id/main_container"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@color/black_transparent_dark">

            <ImageView
                android:id="@+id/iv_file_type"
                android:layout_width="76dp"
                android:layout_height="76dp"
                android:layout_centerInParent="true"
                android:contentDescription="@string/media"
                android:scaleType="centerCrop"
                android:src="@drawable/ic_play" />

            <TextView
                android:id="@+id/tv_file_name"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_margin="5dp"
                android:layout_above="@+id/progress_bar"
                android:ellipsize="end"
                android:gravity="center"
                android:maxLines="2"
                android:text="fileName"
                android:textColor="@color/white"
                android:textStyle="bold" />


            <ProgressBar
                android:id="@+id/progress_bar"
                style="@android:style/Widget.ProgressBar.Horizontal"
                android:layout_width="match_parent"
                android:layout_height="6dp"
                android:indeterminate="false"
                android:layout_alignParentBottom="true"
                android:max="0"
                android:progress="0"
                android:progressDrawable="@drawable/video_progress_bar"
                android:visibility="visible" />
        </RelativeLayout>   

    </RelativeLayout>

</android.support.v7.widget.CardView>

屏幕截图:

enter image description here

1 个答案:

答案 0 :(得分:4)

onBindViewHolder()执行holder.progressBar.getProgressDrawable().mutate()