1

我遇到了progressBar.setProgress()progressBar.setMax()的問題。ProgressBar在RecyclerView適配器中不令人耳目一新

我有兩個項目,通常需要顯示視頻的進展,就像附加的屏幕截圖一樣。對於第一個項目,只有我設置了進度數據後,我需要爲第二個項目設置進度爲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

+0

你在調用['adapter.notifyDataSetChanged()'](https://developer.android.com/reference/android/support/v7/widget/RecyclerView.Adapter.html#notifyDataSetChanged())嗎? – azizbekian

+0

無需調用notifyDataSetChanged,因爲初始項本身包含有效數據,第一項包含一些進度值,第二項包含進度值爲0 –

+1

在'onBindViewHolder()'執行'holder.progressBar.getProgressDrawable()。mutate() '。 – azizbekian

回答

3

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

+0

嘿azizbekian我會在24小時後獎勵我的賞金。 –

相關問題