Reputation: 775
By doesn't work, I mean that I can't see it in the ViewHolder. getWidth() returns a value of 0.0.
Edit- The reason that I couldn't see the view was that the original call to get the width was returning 0.
I have a view, which is being used to show how far through a particular class someone is, is set up as follows:
<View
android:id="@+id/timerBar"
android:layout_width="match_parent"
android:layout_height="4dp"
android:background="@color/colorPrimaryDark" />
Within the onBindViewHolder method I am doing the following-
Getting the new width and the current layout
int width = (int) (holder.timerBar.getLayoutParams().width * ((float)time-start)/((float)end-start));
RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) holder.timerBar.getLayoutParams();
Setting the width of the layout, and setting the layout of the view-
holder.timerBar.setLayoutParams(lp);
holder.timerBar.requestLayout();
Calling requestLayout() was something that can apparently cause the view to refresh, however it hasn't worked for me.
Could anyone suggest how to get the view in the recyclerview viewholder to change its width?
Thanks for any help.
Upvotes: 0
Views: 583
Reputation: 34532
Please don't manually set layout parameters unless you know what you are doing. Measuring and layouting of objects within a recyclerview is different, since it is done by a layout manager and you should not interfere with this.
In your case, for displaying a progress you should just use a ProgressBar
and / or use a drawable with clipping or some other means to show progress (there are a lot of different options).
Then you can just call progress.setProgress(50);
and be done without tampering with your layout and views.
Upvotes: 2