imagebutton没有提交recyclerView中的更改 [英] The imagebutton isn't commiting to the change in recyclerView

查看:42
本文介绍了imagebutton没有提交recyclerView中的更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

感谢您的帮助.我试图在我的回收者视图中实现添加到收藏夹"功能,除了一件事之外,其他所有东西都工作正常.我无法在回收者视图中提交更改的图像按钮,每次按该按钮时,帖子都会添加到收藏夹中,并且图像按钮变为黄色,但是一旦我移至其他片段,黄色按钮就会消失回到初始阶段.你们中的任何人可以帮助我如何使我的按钮提交到回收者"视图中的更改吗?下面是我的相关代码.

thanks for helping. I was trying to implement an add to favorite function in my recycler view, everything is working fine except for one thing. I am unable to commit the changed image button in my recycler view, every time I press the button, the post is added to the favorites and the image button turns yellow, but as soon as I move to some other fragment, the yellow button goes back to its initial stage. Can anyone of you help me on how can I make my button commit to the changes in recycler view. Below is my relevant code.

支架类中按钮的初始化:

Initialization of buttons in holder class:

class UsersViewHolder1 extends RecyclerView.ViewHolder {

        View mView;

        private ImageButton mFavouritesBlack, mFavouritesYellow;
        private GestureDetector mGestureDetector;
        private Heart mHeart;


        public UsersViewHolder1(View itemView) {
            super(itemView);

            mView = itemView;
            mFavouritesBlack = mView.findViewById(R.id.ad_start_fav);
            mFavouritesYellow = mView.findViewById(R.id.ad_start_fav1);
       }
}

OnBindViewHOlder类(我省略了不相关的代码):

OnBindViewHOlder class(I omitted the irrelevant code):

protected void onBindViewHolder(@NonNull final UsersViewHolder1 Holder, final int position, @NonNull
                    Ad ad) {
                Holder.setTitle(ad.getTitle());
                Holder.setPrice(ad.getPrice());
                Holder.setCategory(ad.getCategory());
                Holder.setImage(ad.getImage(), getContext());
                Holder.setTime(ad.getTime());


                String user_id = getRef(position).getKey();
                final String kk = user_id.toString();

                Holder.mView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent mew = new Intent(getActivity(), ExpandActivity.class);
                        mew.putExtra("user_id", kk);
                        startActivity(mew);

                    }
                });


                Holder.mFavouritesBlack.setOnTouchListener(new View.OnTouchListener() {
                    @Override
                    public boolean onTouch(View view, MotionEvent motionEvent) {

                        mFav.child(puid).child(kk).child("fav_status").setValue("Added as fav").addOnSuccessListener(new OnSuccessListener<Void>() {
                            @Override
                            public void onSuccess(Void aVoid) {
                                Toast.makeText(getContext(),"Added to Fav",Toast.LENGTH_SHORT).show();

                                if (Holder.mFavouritesBlack.isShown())
                                Holder.mFavouritesBlack.setVisibility(View.GONE);
                                Holder.mFavouritesYellow.setVisibility(View.VISIBLE);


                            }
                        });
                        return true;
                    }
                });
                Holder.mFavouritesYellow.setOnTouchListener(new View.OnTouchListener() {
                    @Override
                    public boolean onTouch(View view, MotionEvent motionEvent) {
                        mFav.child(puid).child(kk).removeValue().addOnSuccessListener(new OnSuccessListener<Void>() {
                            @Override
                            public void onSuccess(Void aVoid) {
                                Toast.makeText(getContext(),"Removed from Fav",Toast.LENGTH_SHORT).show();
                                if (Holder.mFavouritesYellow.isShown())
                                Holder.mFavouritesBlack.setVisibility(View.VISIBLE);
                                Holder.mFavouritesYellow.setVisibility(View.GONE);




                            }
                        });
                        return true;
                    }
                });

            }

推荐答案

经过两天的搜索和工作,我终于消除了我的问题,那就是回收者视图在更改后无法单独保持状态,这就是为什么我们需要提供一种存储状态(或本例中的图像)的方法,以便它在每次回收视图时都能记住它,但是我添加了直接语句,通过它可以检查数据库中是否存在喜欢的节点是否基于按钮的行为.下面是代码.

After 2 days of messed up searches and work I finally removed my the problem was that recycler view isn't able to hold the state by itself after change, that's why we need to provide a way to store the state (or the image in my case) so that it can remember it everytime it recycles the view, but instead I added the direct statement through which it checks whether the favourite node is present in database or not and based on that the buttons behaved. Below is the code.

 mFav.addValueEventListener(new ValueEventListener() {
               @Override
               public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                   if (dataSnapshot.child(puid).hasChild(kk)){
                       Holder.mFavouritesBlack.setVisibility(View.GONE);
                       Holder.mFavouritesYellow.setVisibility(View.VISIBLE);

                   }else{
                       Holder.mFavouritesBlack.setVisibility(View.VISIBLE);
                       Holder.mFavouritesYellow.setVisibility(View.GONE);
                   }
               }

               @Override
               public void onCancelled(@NonNull DatabaseError databaseError) {

               }
           });
 Holder.mFavouritesBlack.setOnTouchListener(new View.OnTouchListener() {
                @Override
                public boolean onTouch(View view, MotionEvent motionEvent) {

                    mFav.child(puid).child(kk).child("fav_status").setValue("Added as fav").addOnSuccessListener(new OnSuccessListener<Void>() {
                        @Override
                        public void onSuccess(Void aVoid) {
                            Toast.makeText(getContext(), "Added to Fav", Toast.LENGTH_SHORT).show();

                            if (!itemStateArray.get(adapterPosition, false))
                                Holder.mFavouritesBlack.setVisibility(View.GONE);
                            Holder.mFavouritesYellow.setVisibility(View.VISIBLE);


                        }
                    });
                    return true;
                }
            });
            Holder.mFavouritesYellow.setOnTouchListener(new View.OnTouchListener() {
                @Override
                public boolean onTouch(View view, MotionEvent motionEvent) {
                    mFav.child(puid).child(kk).removeValue().addOnSuccessListener(new OnSuccessListener<Void>() {
                        @Override
                        public void onSuccess(Void aVoid) {
                            Toast.makeText(getContext(), "Removed from Fav", Toast.LENGTH_SHORT).show();
                            if (itemStateArray.get(adapterPosition, false))
                                Holder.mFavouritesBlack.setVisibility(View.VISIBLE);
                            Holder.mFavouritesYellow.setVisibility(View.GONE);


                        }
                    });
                    return true;
                }
            });

但是,非常感谢所有尝试提供帮助的人.

But huge thanks to everyone who tried to help.

这篇关于imagebutton没有提交recyclerView中的更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆