Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ProgressBar is not refreshing in RecyclerView Adapter

I am facing an issue with progressBar.setProgress() and progressBar.setMax().

I am having two items and generally need to show the progress of the video as like attached screen shot. For first item only I have the some progress data after set that progress I need to set progress to 0 (zero) for second item. But second item ProgressBar is not reset to back to zero instead of this it is still showing first item's progress length.

I printed the values for Items here:

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

Adapter Code :

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);

}

Adapter row 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>

Screen Shot :

enter image description here

like image 835
Naveen Kumar M Avatar asked Mar 08 '23 20:03

Naveen Kumar M


1 Answers

In onBindViewHolder() perform holder.progressBar.getProgressDrawable().mutate().

like image 76
azizbekian Avatar answered Apr 02 '23 00:04

azizbekian