在onBindViewHolder中将setTag设置为ImageView

时间:2018-04-04 05:09:03

标签: android firebase firebase-realtime-database android-recyclerview recycler-adapter

我有RecylcerView张持有信息的卡片,每张卡片都有一个喜欢和不喜欢的按钮。单击like按钮时,用户ID将存储在我的数据库中,用户不应再次喜欢它。如果他再次点击它,他的动作应该重置他的喜欢。同样应该适合不喜欢。

onBindViewHolder的适配器中,如果用户已经点击了按钮,我会检查我的数据库。如果是这样,我用红色箭头替换白色向上箭头图像。我还通过ImageView将标记设置为setTag。其中0代表未投票的案例,如果在之前按下了类似按钮,则为1:

@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {

    final String mUID = getUserID();
    final String postID = getPostID(position);
    final PostViewHolder holder1 = (PostViewHolder) holder;

    //REFERENCE WHERE USER IDs ARE STORED WHEN THEY LIKE THE POST     
    mRefForUID = rootRef.child("likesUIDs").child(postID);

    ValueEventListener valueEventListenerForUID = new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            //IF USER HAS LIKED A REFERENCE IS STORED WITH "mUID+"
            if (dataSnapshot.hasChild(mUID+"+")){
                //SET RED ARROW IF UP IS CLICKED BEFORE AND SETTAG TO 1
                holder1.mUp.setTag(1);
                holder1.mUp.setImageResource(R.drawable.arrow_up_red);

            } if(dataSnapshot.hasChild(mUID+"-")) {
              //SAME AS ABOVE FOR THE DISLIKED CASE
              //HERE I COULD SET THE TAG TO -1 

            } else {
                //IF USER ID IS NOT KNOWN HE HAS NOT CLICKED
                //SO I SET THE TAG TO 0 FOR THIS CASE
                holder1.mUp.setTag(0);
            }
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {
        }
    };
    mRefForUID.addListenerForSingleValueEvent(valueEventListenerForUID);
}

在我的ViewHolder中,我定义了按钮的onClick事件(在我的例子中,这些是ImageViews)。如果在onClick事件之前点击按钮应该是不同的,那么我借助我之前初始化的标记和switch case算法来区分这些状态:

public class PostViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{

    public ImageView mUp;
    public ImageView mDown;

    public PostViewHolder(View itemView, postClickListener listener){
        super(itemView);

        mUp = mView.findViewById(R.id.arrowUp);
        mDown = mView.findViewById(R.id.arrowDown);

        mUp.setOnClickListener(this);
        mDown.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {

        if (v.getId() == mUp.getId()){
            int tag = (int) mUp.getTag();
            int position = getAdapterPosition();

            switch(tag){

                case 0:

                    //CASE 0 = NOT CLICKED BEFORE
                    //MAKE A NORMAL LIKE AND SET THE TAG TO 1 FROM HERE

                case 1:

                    //CASE 1 = UPVOTE IS CLICKED BEFORE
                    //UNLIKE AND SET THE TAG TO 0 HERE

            }

        }

    }

我忽略了投票的理由。因此,正确的ImageView的分配确实有效。当我打开应用程序时,如果之前已经投票,我会得到红色的upvote按钮。但是当我启动应用程序时,标签始终为0!因此,onBindViewHolder正确执行以下操作holder1.mUp.setImageResource(R.drawable.arrow_up_red);,但未设置标记holder1.mUp.setTag(1);

第一次打开应用程序时尤其不起作用。点击几次后,我会得到正确的标签,因为它们将在ViewHolder中设置。

我希望找到错误,甚至更好地解决我的意图。

0 个答案:

没有答案