为啥这些随机标记出现在每个 recyclerview 和 viewpager 项目中?
Posted
技术标签:
【中文标题】为啥这些随机标记出现在每个 recyclerview 和 viewpager 项目中?【英文标题】:Why these random marks are appearing in every recyclerview and viewpager items?为什么这些随机标记出现在每个 recyclerview 和 viewpager 项目中? 【发布时间】:2020-11-28 11:05:49 【问题描述】:我在片段中有 2 个回收器视图和一个视图寻呼机。 其他一切正常,但两个回收站视图项目都在每个项目的左上角显示一个随机标记。视图寻呼机项目也发生了同样的事情。
此片段在片段活动中,并且视图分页器正在使用分页器适配器。我正在使用 Picasso 库在项目上加载图像,并使用网络策略将图像缓存在磁盘中。
recycler view items appearing like this
view pager items appearing like this
Recycler View 适配器
public class CourseListAdapter extends RecyclerView.Adapter<CourseListAdapter.CourseListViewHolder>
public static final int HOME_PAGE = 1;
public static final int DISPLAY_COURSE = 2;
private Picasso mPicasso;
private List<DisplayCourse> courseList;
private static final String TAG = "CourseListAdapter";
private OnItemClickListener mListener;
public interface OnItemClickListener
void onItemClick(int position, View view);
private String mSender;
public void setOnItemClickListener(OnItemClickListener listener)
mListener = listener;
public CourseListAdapter(List<DisplayCourse> courseList, String sender)
this.courseList = courseList;
this.mSender = sender;
mPicasso = Picasso.get();
@NonNull
@Override
public CourseListViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType)
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
switch (viewType)
case 1:
View view1 = inflater.inflate(R.layout.recom_course_home_layout, parent, false);
return new CourseListViewHolder(view1, mListener);
case 2:
View view2 = inflater.inflate(R.layout.display_course_layout, parent, false);
return new CourseListViewHolder(view2, mListener);
default:
View view3 = inflater.inflate(R.layout.section_video_item_layout, parent, false);
return new CourseListViewHolder(view3, mListener);
@Override
public void onBindViewHolder(@NonNull CourseListViewHolder holder, int position)
holder.title.setText(courseList.get(position).getCourseTitle());
mPicasso.load(courseList.get(position).getThumbnailURL())
.networkPolicy(NetworkPolicy.OFFLINE)
.into(holder.thumbnail, new Callback()
@Override
public void onSuccess()
@Override
public void onError(Exception e)
mPicasso.load(courseList.get(position).getThumbnailURL())
.error(R.drawable.ofklogo)
.into(holder.thumbnail, new Callback()
@Override
public void onSuccess()
@Override
public void onError(Exception e)
);
);
@Override
public int getItemCount()
return courseList.size();
public static class CourseListViewHolder extends RecyclerView.ViewHolder
ImageView thumbnail;
TextView title;
public CourseListViewHolder(@NonNull View itemView, final OnItemClickListener listener)
super(itemView);
title = itemView.findViewById(R.id.courseTitle);
thumbnail = itemView.findViewById(R.id.courseThumbNailImageView);
itemView.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View view)
int position = getAdapterPosition();
if (listener != null)
if (position != RecyclerView.NO_POSITION)
listener.onItemClick(position, view);
);
@Override
public int getItemViewType(int position)
if (mSender.equals("home_page"))
return HOME_PAGE;
else if (mSender.equals("displayCourse"))
return DISPLAY_COURSE;
return -1;
查看寻呼机适配器
public class VideoSliderAdapter extends PagerAdapter
private boolean doNotifyDataSetChangedOnce = false;
private static final String TAG = "VideoSliderAdapter";
private YouTubePlayerView youTubePlayerView;
private View gradientView;
private ImageView thumbNail;
private LinearLayout layout;
private Picasso picasso;
private List<Video> videoList;
private Context mContext;
private Lifecycle mLifeCycle;
public VideoSliderAdapter(List<Video> videoList, Context context, Lifecycle lifecycle)
this.videoList = videoList;
this.mContext = context;
this.mLifeCycle = lifecycle;
doNotifyDataSetChangedOnce = true;
picasso = Picasso.get();
@Override
public int getCount()
if (doNotifyDataSetChangedOnce)
doNotifyDataSetChangedOnce = false;
notifyDataSetChanged();
return videoList.size();
@Override
public int getItemPosition(@NonNull Object object)
return POSITION_NONE;
@Override
public boolean isViewFromObject(@NonNull View view, @NonNull Object object)
return (view == (CardView) object);
@NonNull
@Override
public Object instantiateItem(@NonNull ViewGroup container, int position)
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.video_paly_layout, container, false);
TextView title = view.findViewById(R.id.videoTitle);
title.setText(videoList.get(position).getVideoTitle());
gradientView = view.findViewById(R.id.gradientView);
thumbNail = view.findViewById(R.id.videoThumbNail);
picasso.load(videoList.get(position).getVideoThumbNail())
.networkPolicy(NetworkPolicy.OFFLINE)
.into(thumbNail, new Callback()
@Override
public void onSuccess()
@Override
public void onError(Exception e)
picasso.load(videoList.get(position).getVideoThumbNail())
.error(R.drawable.ofklogo)
.into(thumbNail, new Callback()
@Override
public void onSuccess()
@Override
public void onError(Exception e)
);
);
layout = view.findViewById(R.id.videoPlayLayout);
youTubePlayerView = view.findViewById(R.id.youtube_player_view);
mLifeCycle.addObserver(youTubePlayerView);
new AddListener(youTubePlayerView, position).execute();
container.addView(view);
return view;
@Override
public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object)
container.removeView((CardView) object);
回收站视图持有者布局
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_
android:layout_
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
android:clickable="true"
android:focusable="true"
android:foreground="?android:attr/selectableItemBackground"
app:cardCornerRadius="8dp">
<RelativeLayout
android:layout_
android:layout_>
<ImageView
android:id="@+id/courseThumbNailImageView"
android:layout_
android:layout_
android:layout_alignParentTop="true"
android:scaleType="fitXY"
android:src="@drawable/art_thumb" />
<RelativeLayout
android:layout_
android:layout_
android:layout_below="@id/courseThumbNailImageView">
<TextView
android:id="@+id/courseTitle"
android:layout_
android:layout_
android:layout_centerInParent="true"
android:maxLines="2"
android:text="Course title"
android:textAlignment="center"
android:textColor="@android:color/black"
android:textStyle="bold" />
</RelativeLayout>
</RelativeLayout>
</androidx.cardview.widget.CardView>
查看页面布局
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_
android:layout_
app:cardCornerRadius="8dp">
<RelativeLayout
android:layout_
android:layout_>
<com.pierfrancescosoffritti.androidyoutubeplayer.core.player.views.YouTubePlayerView
android:id="@+id/youtube_player_view"
android:layout_
android:layout_
app:autoPlay="false" />
<ImageView
android:id="@+id/videoThumbNail"
android:layout_
android:layout_
android:scaleType="fitXY" />
<View
android:id="@+id/gradientView"
android:layout_
android:layout_
android:background="@drawable/gradient_drawable" />
<LinearLayout
android:id="@+id/videoPlayLayout"
android:layout_
android:layout_
android:layout_alignParentBottom="true"
android:background="?attr/selectableItemBackground"
android:clickable="true"
android:focusable="true"
android:gravity="center_vertical"
android:orientation="horizontal"
android:padding="16dp">
<ImageView
android:layout_
android:layout_
android:padding="8dp"
android:src="@drawable/play_button" />
<TextView
android:id="@+id/videoTitle"
android:layout_
android:layout_
android:ellipsize="end"
android:maxLines="2"
android:text="Course title"
android:textColor="@android:color/white"
android:textStyle="bold" />
</LinearLayout>
</RelativeLayout>
</androidx.cardview.widget.CardView>
【问题讨论】:
发布一些关于您的 RecyclerView 和 ViewPager 的代码 你是否为每个项目使用了卡片视图? 是的,我正在为每个项目使用卡片视图 检查您的卡片视图背景颜色和边距。我认为这些来自那个 XML。请在此处添加您的 xml 以供参考。 【参考方案1】:使用 Picasso 加载图像时,使用:setIndicatorsEnabled(false)
picasso.load(videoList.get(position).getVideoThumbNail())
.networkPolicy(NetworkPolicy.OFFLINE)
.setIndicatorsEnabled(false)
.into(...);
颜色表示:
绿色:从内存中提取图像 蓝色:图像是从磁盘获取的 红色:图片来自网络【讨论】:
以上是关于为啥这些随机标记出现在每个 recyclerview 和 viewpager 项目中?的主要内容,如果未能解决你的问题,请参考以下文章
为啥尝试在 GoogleMap 上放置标记时出现 NullPointerException?