我使用下面的代码填充包含图像视图的回收站视图,并且效果很好
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical"
>
<!-- Put the boxes here and make all of them have a height 0dp, and layout weight as 1 -->
</LinearLayout>
<!-- the navigation box goes here with a specific height-->
</FrameLayout>
}
但是当我使用
public class PopularImageAdapter extends RecyclerView.Adapter<PopularImageAdapter.PhotoViewHolder> {
private static final int PHOTO_ANIMATION_DELAY = 600;
private static final Interpolator INTERPOLATOR = new DecelerateInterpolator();
private List<FeedItem> feedItems;
private Context context;
private final int cellSize;
private int lastAnimatedItem = -1;
private boolean lockedAnimations = false;
private OnPhotoFeedItemClickListener onFeedItemClickListener;
public PopularImageAdapter(Context context, List<FeedItem> feedItems){
this.context = context;
this.feedItems = feedItems;
this.cellSize = Utils.getScreenWidth(context) / 3;
}
@NonNull
@Override
public PopularImageAdapter.PhotoViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(context).inflate(R.layout.item_photo, parent, false);
StaggeredGridLayoutManager.LayoutParams layoutParams = (StaggeredGridLayoutManager.LayoutParams) view.getLayoutParams();
layoutParams.height = cellSize;
layoutParams.width = cellSize;
layoutParams.setFullSpan(false);
view.setLayoutParams(layoutParams);
PhotoViewHolder cellFeedViewHolder = new PhotoViewHolder(view);
setupClickableViews(view,cellFeedViewHolder);
return new PhotoViewHolder(view);
}
private void setupClickableViews(final View view,final PhotoViewHolder cellFeedViewHolder) {
cellFeedViewHolder.ivPhoto.setOnClickListener(v -> onFeedItemClickListener.onPhotoClick(view, cellFeedViewHolder.getAdapterPosition(),feedItems));
}
@Override
public void onBindViewHolder(@NonNull PopularImageAdapter.PhotoViewHolder holder,final int position) {
holder.bindView(feedItems.get(position));
FeedItem item = feedItems.get(position);
Picasso.with(context)
.load(item.getImge())
.resize(cellSize, cellSize)
.centerCrop()
.into(holder.ivPhoto, new Callback() {
@Override
public void onSuccess() {
animatePhoto(holder);
}
@Override
public void onError() {
}
});
if (lastAnimatedItem < position) lastAnimatedItem = position;
}
private void animatePhoto(PhotoViewHolder viewHolder) {
if (!lockedAnimations) {
if (lastAnimatedItem == viewHolder.getAdapterPosition()) {
setLockedAnimations(true);
}
long animationDelay = PHOTO_ANIMATION_DELAY + viewHolder.getAdapterPosition() * 30;
viewHolder.flRoot.setScaleY(0);
viewHolder.flRoot.setScaleX(0);
viewHolder.flRoot.animate()
.scaleY(1)
.scaleX(1)
.setDuration(200)
.setInterpolator(INTERPOLATOR)
.setStartDelay(animationDelay)
.start();
}
}
public void setLockedAnimations(boolean lockedAnimations) {
this.lockedAnimations = lockedAnimations;
}
@Override
public int getItemCount() {
return feedItems.size();
}
public void setOnFeedItemClickListener(OnPhotoFeedItemClickListener onFeedItemClickListener) {
this.onFeedItemClickListener = onFeedItemClickListener;
}
public interface OnPhotoFeedItemClickListener {
void onPhotoClick(View v,int position, List<FeedItem> feedItems);
}
public static class PhotoViewHolder extends RecyclerView.ViewHolder {
@BindView(R.id.flRoot)
FrameLayout flRoot;
@BindView(R.id.ivPhoto)
ImageView ivPhoto;
FeedItem feedItem;
PhotoViewHolder(View view) {
super(view);
ButterKnife.bind(this, view);
}
void bindView(FeedItem feedItem){
this.feedItem = feedItem;
}
}
我收到一条错误消息,表明我的适配器位置是-1,我不确定-1的来源,因为即使我删除了初始化为-1的全局变量,我仍然得到-1作为适配器位置
答案 0 :(得分:0)
在您的onCreateViewHolder
方法中,您将返回PhotoViewHolder
的新实例,而不是在其中定义点击次数的cellFeedViewHolder
。