我有一个显示YouTube视频缩略图的RecyclerView
。我希望在某个位置调用notifyItemChanged()
方法时,不要刷新这些缩略图。
这是我的适配器:
public class VideosAdapter extends RecyclerView.Adapter<VideosAdapter.ViewHolder> {
private List<Video> videos;
private Animation loadingRotation;
public VideosAdapter(Context context, List<Video> videos) {
this.videos= videos;
loadingRotation = AnimationUtils.loadAnimation(context, R.anim.refresh_btn);
loadingRotation.setRepeatCount(Animation.INFINITE);
}
@NonNull
@Override
public VideosAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
if (parent instanceof RecyclerView) {
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
VideoItemLayoutBinding binding = DataBindingUtil.inflate(inflater, R.layout.video_item_layout,
parent, false);
ViewHolder holder = new ViewHolder(binding);
return holder;
} else {
throw new RuntimeException("Something is wrong with the RecyclerView");
}
}
@Override
public void onBindViewHolder(@NonNull VideosAdapter.ViewHolder holder, int position) {
//views that would update
//I want to exclude this:
holder.b.thumbnail.startAnimation(loadingRotation);
holder.b.thumbnail.initialize(YOUTUBE_API_KEY, new YouTubeThumbnailView.OnInitializedListener() {
@Override
public void onInitializationSuccess(YouTubeThumbnailView youTubeThumbnailView,
YouTubeThumbnailLoader youTubeThumbnailLoader) {
youTubeThumbnailLoader.setVideo(videos.get(position));
youTubeThumbnailLoader.setOnThumbnailLoadedListener(new YouTubeThumbnailLoader.OnThumbnailLoadedListener() {
@Override
public void onThumbnailLoaded(YouTubeThumbnailView youTubeThumbnailView, String s) {
youTubeThumbnailView.clearAnimation();
youTubeThumbnailView.setBackground(null);
youTubeThumbnailLoader.release();
}
@Override
public void onThumbnailError(YouTubeThumbnailView youTubeThumbnailView,
YouTubeThumbnailLoader.ErrorReason errorReason) {
youTubeThumbnailView.clearAnimation();
youTubeThumbnailLoader.release();
}
});
}
@Override
public void onInitializationFailure(YouTubeThumbnailView youTubeThumbnailView,
YouTubeInitializationResult youTubeInitializationResult) {
}
});
}
@Override
public int getItemCount() {
return ads.size();
}
class ViewHolder extends RecyclerView.ViewHolder {
VideoItemLayoutBinding b;
ViewHolder(@NotNull VideoItemLayoutBinding binding) {
super(binding.getRoot());
this.b = binding;
}
}
}
video_item_layout.xml
中的缩略图声明 <com.google.android.youtube.player.YouTubeThumbnailView
android:id="@+id/thumbnail"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_marginBottom="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:background="@drawable/loading_thumbnail"
android:contentDescription="@string/video_thumbnail_description"/>
我在加载缩略图时使用了动画(旋转进度条),并在缩略图准备就绪时停止。
我尝试将thumbnail.initialize()
放在onCreateViewHolder()
和ViewHolder()
构造函数中,但仍然没有,当我调用notifyItemChanged()
时,缩略图总是被重新加载。
提前谢谢。
答案 0 :(得分:0)
您可以选择其中一种回收视图方法
notifyItemChanged(int position, Object payload)
notifyItemChanged(int position)
notifyItemRangeChanged(int positionStart, int itemCount, Object payload)
notifyItemRangeChanged(int positionStart, int itemCount)
根据您的要求,它不会刷新整个回收视图项目,而是刷新所选项目。有关详细信息,请参阅official docs
关于有效负载,从文档中可以清楚地看出
客户端可以选择传递有效负载以进行部分更改。这些 有效负载将被合并,并可以传递给适配器 onBindViewHolder(ViewHolder,int,List),如果该项已经存在 由ViewHolder代表,它将反弹到同一个 ViewHolder。具有null有效负载的notifyItemRangeChanged()将清除 该项目上的所有现有有效负载,并防止将来的有效负载 调用onBindViewHolder(ViewHolder,int,List)。适配器不应该 假设有效负载将始终传递给onBindViewHolder(), 例如当没有附加视图时,有效载荷将是简单的 丢弃。
答案 1 :(得分:0)
您可以覆盖notifyItemChanged()方法,如下所示
@Override
public void notifyItemChanged(int position){
if(position!=THE_POSITION_WHERE_YOU_DONT_WANTED_TO_REFRESH){
super.notifyItemChanged(position);
}
}