回收站视图位置
Posted
技术标签:
【中文标题】回收站视图位置【英文标题】:recycler view position 【发布时间】:2018-11-17 06:28:43 【问题描述】:我是 android 开发新手。我有一个带有 Viewholder 的 recyclerview 用于显示照片。我在我的应用中实现了 like 功能,但我面临的唯一问题是添加 喜欢 在照片上 like 没有显示在我喜欢的照片上,而是在下面的另一张照片上显示 like,当我在 firebase 数据库中看到时它看起来不错,但在回收站视图中没有显示在正确的位置。
我认为它没有更新位置我该如何解决这个问题?
这是我的适配器类
@Override
public void onBindViewHolder(@NonNull final RecyclerView.ViewHolder holder, final int position)
// mHolder = holder;
photo = moviesList.get(position);
// final VideoHolder viewHolder2 = (VideoHolder)holder;
int viewType = getItemViewType(holder.getAdapterPosition());
switch ( viewType )
case IMAGE_TYPE:
PhotoHolder photoview = (PhotoHolder) holder;
mPhotoHolder = photoview;
getCurrentUsername();
getLikesPhotoString();
final ImageLoader imageLoader = ImageLoader.getInstance();
imageLoader.displayImage(getItem(position).getImage_path(),photoview.image);
photoview.mHeart.setOnLikeListener(new OnLikeListener()
@Override
public void liked(LikeButton likeButton)
addNewPhotolike(mPhotoHolder);
@Override
public void unLiked(LikeButton likeButton)
removePhotolike(mPhotoHolder);
);
photoview.Star.setOnLikeListener(new OnLikeListener()
@Override
public void liked(LikeButton likeButton)
@Override
public void unLiked(LikeButton likeButton)
);
break;
case VIDEO_TYPE:
final VideoHolder viewHolder2 = (VideoHolder)holder;
mVideoHolder = viewHolder2;
break;
@Override
public int getItemCount()
return moviesList.size();
public Photo getItem(int position)
return moviesList.get(position);
@Override
public int getItemViewType ( int position )
int viewType;
if (moviesList.get(position).getType_post().contains("Photo"))
viewType = IMAGE_TYPE;
else
viewType = VIDEO_TYPE;
return viewType;
这是添加到 firebase 的照片
private void addNewPhotolike(TestAdapter.PhotoHolder holder, final int position)
Log.d(TAG, "addNewlike: adding new like ");
String newLikeID = mReference.push().getKey();
Likes likes = new Likes();
likes.setUser_id(FirebaseAuth.getInstance().getCurrentUser().getUid());
mReference.child(mContext.getString(R.string.dbname_photos))
.child(getItem(position).getPhoto_id())
.child(mContext.getString(R.string.field_likes))
.child(newLikeID)
.setValue(likes);
mReference.child(mContext.getString(R.string.dbname_user_photos))
.child(getItem(position).getUser_id())
.child(getItem(position).getPhoto_id())
.child(mContext.getString(R.string.field_likes))
.child(newLikeID)
.setValue(likes);
holder.mHeartPhoto.setLiked(true);
HashMap<String ,String> notificationData = new HashMap<>();
notificationData.put("from",FirebaseAuth.getInstance().getCurrentUser().getUid());
notificationData.put("type","likes");
notificationData.put("photo_desc",getItem(position).getDescription());
holder.mNotification.child(getItem(position).getUser_id()).push().setValue(notificationData).addOnSuccessListener(new OnSuccessListener<Void>()
@Override
public void onSuccess(Void aVoid)
// getLikesString(mHolder);
getLikesPhotoString(mPhotoHolder,position);
).addOnFailureListener(new OnFailureListener()
@Override
public void onFailure(@NonNull Exception e)
// getLikesString(mHolder);
getLikesPhotoString(mPhotoHolder,position);
);
这是我的代码,其中 like 从 firebase 检索并以文本形式显示。
private void getLikesPhotoString(final TestAdapter.PhotoHolder holder, final int postion)
Log.d(TAG, "getLikesString: getting likes string");
try
DatabaseReference reference = FirebaseDatabase.getInstance().getReference();
reference.keepSynced(true);
Query query = reference
.child(mContext.getString(R.string.dbname_photos))
.child(getItem(postion).getPhoto_id())
.child(mContext.getString(R.string.field_likes));
query.addListenerForSingleValueEvent(new ValueEventListener()
@Override
public void onDataChange(DataSnapshot dataSnapshot)
holder.usersPhoto = new StringBuilder();
for(DataSnapshot singleSnapshot : dataSnapshot.getChildren())
DatabaseReference reference = FirebaseDatabase.getInstance().getReference();
Query query = reference
.child(mContext.getString(R.string.dbname_users))
.orderByChild(mContext.getString(R.string.field_user_id))
.equalTo(singleSnapshot.getValue(Likes.class).getUser_id());
query.addListenerForSingleValueEvent(new ValueEventListener()
@Override
public void onDataChange(DataSnapshot dataSnapshot)
for(DataSnapshot singleSnapshot : dataSnapshot.getChildren())
Log.d(TAG, "onDataChange: found like: " +
singleSnapshot.getValue(User.class).getUsername());
holder.usersPhoto.append(singleSnapshot.getValue(User.class).getUsername());
holder.usersPhoto.append(",");
String[] splitUsers = holder.usersPhoto.toString().split(",");
if( holder.usersPhoto.toString().contains(currentUsername + ","))
holder.likephotobycurrentUser = true;
else
holder.likephotobycurrentUser = false;
Log.d(TAG, "onDataChange: likes string: " + holder.mLIkePhotoString);
// setupwidjets();
int length = splitUsers.length;
if(length == 1)
holder.mLIkePhotoString = "Liked by " + splitUsers[0];
else if(length == 2)
holder.mLIkePhotoString = "Liked by " + splitUsers[0]
+ " and " + splitUsers[1];
else if(length == 3)
holder.mLIkePhotoString = "Liked by " + splitUsers[0]
+ ", " + splitUsers[1]
+ " and " + splitUsers[2];
else if(length == 4)
holder.mLIkePhotoString = "Liked by " + splitUsers[0]
+ ", " + splitUsers[1]
+ ", " + splitUsers[2]
+ " and " + splitUsers[3];
else if(length > 4)
holder.mLIkePhotoString = "Liked by " + splitUsers[0]
+ ", " + splitUsers[1]
+ ", " + splitUsers[2]
+ " and " + (splitUsers.length - 3) + " others";
Log.d(TAG, "onDataChange: likes string: " + holder.mLIkePhotoString);
//setup likes string
// setupLikePhotostring(holder, holder.mLIkePhotoString);
holder.mHeartPhoto.setLiked(true);
if (holder.likephotobycurrentUser)
holder.mHeartPhoto.setLiked(true);
else
holder.mHeartPhoto.setLiked(false);
holder.likes.setText(holder.mLIkePhotoString);
@Override
public void onCancelled(DatabaseError databaseError)
);
if(!dataSnapshot.exists())
holder.mLIkePhotoString = "";
holder.likephotobycurrentUser = false;
//setupwidjets();
// setupLikePhotostring(holder,holder.mLIkePhotoString);
if (holder.likephotobycurrentUser)
holder.mHeartPhoto.setLiked(true);
else
holder.mHeartPhoto.setLiked(false);
holder.likes.setText(holder.mLIkePhotoString);
holder.mHeartPhoto.setLiked(false);
@Override
public void onCancelled(DatabaseError databaseError)
);
catch (NullPointerException e)
Log.e(TAG, "removeStar: NullPointer"+e.getMessage());
holder.mLIkePhotoString = "";
holder.likephotobycurrentUser = false;
// setupLikePhotostring(mPhotoHolder,holder.mLIkePhotoString);
if (holder.likephotobycurrentUser)
holder.mHeartPhoto.setLiked(true);
else
holder.mHeartPhoto.setLiked(false);
holder.likes.setText(holder.mLIkePhotoString);
【问题讨论】:
你没有得到照片的位置 发布您的适配器代码 好的...请稍候 见兄弟..我有完整的代码 您需要提供点击哪个按钮的位置 【参考方案1】:改变这一行
photo = moviesList.get(holder.getAdapterPosition());
到
photo = moviesList.get(position);
其中位置变量与我们在下面的函数中得到的相同
public void onBindViewHolder(@NonNull final RecyclerView.ViewHolder holder, final int position);
同时在onBindViewHolder(...)
函数中将holder.getAdapterPosition()
更改为position
。
【讨论】:
不是错误。它没有崩溃,但它不像在喜欢的帖子上那样显示..而是在另一张照片上显示 请显示addNewPhotolike()
方法
不,添加新照片 like() 没有问题 ..它完美地添加到数据库中..如果你还想看 ..我会告诉你 ..我已经编辑过以上是关于回收站视图位置的主要内容,如果未能解决你的问题,请参考以下文章
如何从回收站视图 android 中传递一个 url onclick 项目并 onclick 获取位置 dipaly 在另一个活动中的 url