我是Android开发的新手,我在适配器中使用了两个Viewholder 一个用于照片(PHOTOVIEWHOLDER),另一个用于视频(VideoVIEWHOLDER)
我的问题是我拥有一个已通过一个视图保持器(Photoviewholder)的功能,我需要对我的(VideoViewholder)使用相同的功能。 与其重复两次相同的功能,我还想我还有其他方法可以
private void addNewlike(final PhotoHolder holder){
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(holder.photo.getPhoto_id())
.child(mContext.getString(R.string.field_likes))
.child(newLikeID)
.setValue(likes);
mReference.child(mContext.getString(R.string.dbname_user_photos))
.child(holder.photo.getUser_id())
.child(holder.photo.getPhoto_id())
.child(mContext.getString(R.string.field_likes))
.child(newLikeID)
.setValue(likes);
holder.heart.toggleLike();
}
private void addNewlike(final VideoViewHolder holder){
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(holder.photo.getPhoto_id())
.child(mContext.getString(R.string.field_likes))
.child(newLikeID)
.setValue(likes);
mReference.child(mContext.getString(R.string.dbname_user_photos))
.child(holder.photo.getUser_id())
.child(holder.photo.getPhoto_id())
.child(mContext.getString(R.string.field_likes))
.child(newLikeID)
.setValue(likes);
holder.heart.toggleLike();
}
public class GestureListener extends GestureDetector.SimpleOnGestureListener{
PhotoHolder mHolder;
public GestureListener(PhotoHolder holder) {
mHolder = holder;
}
@Override
public boolean onDown(MotionEvent e) {
return true;
}
@Override
public boolean onDoubleTap(MotionEvent e) {
Log.d(TAG, "onDoubleTap: double tap detected.");
Log.d(TAG, "onDoubleTap: clicked on photo: " + mHolder.photo.getPhoto_id());
DatabaseReference reference = FirebaseDatabase.getInstance().getReference();
Query query = reference
.child(mContext.getString(R.string.dbname_photos))
.child(mHolder.photo.getPhoto_id())
.child(mContext.getString(R.string.field_likes));
query.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot singleSnapshot : dataSnapshot.getChildren()){
String keyID = singleSnapshot.getKey();
//case1: Then user already liked the photo
if(mHolder.likephotobycurrentUser
&& singleSnapshot.getValue(Likes.class).getUser_id()
.equals(FirebaseAuth.getInstance().getCurrentUser().getUid())
){
mReference.child(mContext.getString(R.string.dbname_photos))
.child(mHolder.photo.getPhoto_id())
.child(mContext.getString(R.string.field_likes))
.child(keyID)
.removeValue();
/// mReference.child(mContext.getString(R.string.dbname_user_photos))
// .child(FirebaseAuth.getInstance().getCurrentUser().getUid())
.child(mHolder.photo.getUser_id())
.child(mHolder.photo.getPhoto_id())
.child(mContext.getString(R.string.field_likes))
.child(keyID)
.removeValue();
mHolder.heart.toggleLike();
getLikesString(mHolder);
}
//case2: The user has not liked the photo
else if(!mHolder.likephotobycurrentUser){
//add new like
addNewlike(mHolder);
break;
}
}
if(!dataSnapshot.exists()){
//add new like
addNewlike(mHolder);
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
return true;
}
}
答案 0 :(得分:2)
您可以创建一个接口,两个所有者都将实现该接口,并具有一个以该接口为参数的函数。
例如:
C:/Users/MinecraftMaster/Desktop/Python/Tests/monky.gif
那么您就是持有人:
logo = tk.PhotoImage(file ="C:/Users/MinecraftMaster/Desktop/Python/Tests/monky.gif")
最后是您的功能:
public interface MyInterface {
Photo getPhoto(); // I assume your holder.photo returns a photo class here but feel free to replace by whatever is correct
Heart getHeart();
}
它应该可以正常工作
答案 1 :(得分:2)
定义类型MyBaseViewHolder
的{{3}},该类型声明在PhotoHolder
和VideoViewHolder
中发现的方法和属性。
将您的私有void addNewlike
函数重构为
private void addNewlike(final MyBaseViewHolder holder)
并传递所需的任何ViewHolder
。
点击链接以了解有关抽象类的更多信息,它们非常有用!
答案 2 :(得分:1)
传递VideoViewHolder
和PhotoHolder
的基类
private void addNewlike(final BaseClass holder)