问题
I am using Recycler View for creating a list of items and I am getting a duplicate items in the list. I have passed a list of 30 size into the Recycler View Adapter. The created list has 30 items but there are only 3 unique items, all other are repetition of 3 unique items. I am not able to find the bug.
public class CollectionAdapter extends RecyclerView.Adapter<CollectionAdapter.CollectionViewHolder> {
private List<CollectionDataTypeModel> mDataSet = new ArrayList<CollectionDataTypeModel>();
private Activity mActivity;
private String mType;
public CollectionAdapter(List<CollectionDataTypeModel> mDataSet, Activity activity, String type) {
this.mActivity = activity;
this.mDataSet = mDataSet;
this.mType = type;
}
@Override
public CollectionViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.collection_cardview_layout, parent, false);
CollectionViewHolder vh = new CollectionViewHolder(v);
return vh;
}
@Override
public void onBindViewHolder(final CollectionViewHolder holder, int position) {
final CollectionDataTypeModel collectionData = mDataSet.get(position);
Log.d("Colelction Adapter","name : "+collectionData.getCollectionName());
holder.titleText.setText(collectionData.getCollectionName()+"");
holder.secondaryText.setText(collectionData.getPoiCount()+" attractions");
if (mType.equalsIgnoreCase("Collection")) {
Glide.with(mActivity).load(R.drawable.aman).asBitmap().centerCrop().into(new BitmapImageViewTarget(holder.profileImage) {
@Override
protected void setResource(Bitmap resource) {
RoundedBitmapDrawable circularBitmapDrawable = RoundedBitmapDrawableFactory.create(mActivity.getResources(), resource);
circularBitmapDrawable.setCircular(true);
holder.profileImage.setImageDrawable(circularBitmapDrawable);
}
});
} else if (mType.equalsIgnoreCase("Destination")) {
holder.profileImageRipple.setVisibility(View.GONE);
} else if (mType.equalsIgnoreCase("Download")) {
holder.profileImageRipple.setVisibility(View.GONE);
holder.viewIcon.setImageResource(R.drawable.clear_icon);
holder.viewCount.setVisibility(View.INVISIBLE);
}
// holder.collectionImage.setImageResource(R.drawable.goldentemple);
Glide.with(mActivity).load(collectionData.getCollectionImage()).into(holder.collectionImage);
holder.likeCount.setText(Integer.toString(collectionData.getLikeCount()));
holder.viewCount.setText(Integer.toString(collectionData.getViewCount()));
holder.likeIconRipple.setOnRippleCompleteListener(new RippleView.OnRippleCompleteListener() {
@Override
public void onComplete(RippleView rippleView) {
// if(holder.likeIcon.getDrawable()==mActivity.getDrawable(R.drawable.like_icon))
{
holder.likeIcon.setImageResource(R.drawable.like_fill_icon);
}
// else
{
// holder.likeIcon.setImageResource(R.drawable.like_icon);
}
}
});
holder.collectionImageRipple.setOnRippleCompleteListener(new RippleView.OnRippleCompleteListener() {
@Override
public void onComplete(RippleView rippleView) {
EventBus.getDefault().post(new CollectionMessageEvent(collectionData));
}
});
}
@Override
public int getItemCount() {
return mDataSet.size();
}
@Override
public long getItemId(int position) {
return super.getItemId(position);
}
public static class CollectionViewHolder extends RecyclerView.ViewHolder {
TextView titleText;
ImageView collectionImage;
TextView secondaryText;
TextView likeCount;
TextView viewCount;
ImageView profileImage;
ImageView viewIcon;
RippleView collectionImageRipple;
RippleView profileImageRipple;
RippleView likeIconRipple;
ImageView likeIcon;
public CollectionViewHolder(View itemView) {
super(itemView);
// View bottombar = findById(itemView,R.id.bottomBar);
collectionImage = findById(itemView, R.id.collection_image);
collectionImageRipple = findById(itemView, R.id.collection_image_ripple);
profileImage = findById(itemView, R.id.profileimage);
profileImageRipple = findById(itemView, R.id.profileimage_ripple);
titleText = findById(itemView, R.id.title_text);
secondaryText = findById(itemView, R.id.secondary_text);
likeCount = findById(itemView, R.id.like_count);
likeIcon = findById(itemView, R.id.like_icon);
likeIconRipple = findById(itemView, R.id.like_icon_ripple);
viewCount = findById(itemView, R.id.view_count);
viewIcon = findById(itemView, R.id.view_icon);
}
}
}
来源:https://stackoverflow.com/questions/36497190/recycler-view-creating-duplicate-items