like unlike button recyclerView image

不问归期 提交于 2019-12-13 03:12:16

问题


I have an android app in which users can like and unlike an image.I'm using recyclerView.I Just disable the button(Like/Unlike) once user clicked. Problem, when I click on button like , the apps go to main activity and the button Like doesn't change to unlike What I have done :

1 ) layout that holds the each recycler view layout item

2 ) A view holder for creating each layout

3 ) A Model Class to holds the data

4 ) Recycler Adaptor which deals with the data for the Each Layout item

Hier ist my view holder

//Initializing Views
        public ViewHolder(View itemView) {
            super(itemView);
            imageView = (NetworkImageView)      itemView.findViewById(R.id.imageViewHero);
            textViewName = (TextView) itemView.findViewById(R.id.textViewName);
            //textViewPublisher = (TextView) itemView.findViewById(R.id.textViewPublisher);
            likeImageView = (ImageView) itemView.findViewById(R.id.likeImageView);
            likeImageView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {


                    int id = (int)likeImageView.getTag();
                    if( id == R.drawable.ic_like){

                        likeImageView.setTag(R.drawable.ic_liked);
                        likeImageView.setImageResource(R.drawable.ic_liked);                       

                    }else{

                        likeImageView.setTag(R.drawable.ic_like);
                        likeImageView.setImageResource(R.drawable.ic_like);               


                    }

                }
            });

回答1:


I answered this type of questions over and over. You didn't search enough and you didn't understand how ListView or RecycleView works. Changing current state of views ( such as changing text of TextView or changing resource of ImageView) is the wrong thing. You need data set (a list related to items in ListView) and you need to change corresponding data of the list and call notifyDataSetChanged() method of your adapter.

Don't forget. getView() method of your adapter is called every time any view of your list become on the screen and if you update only the view (instead of change data) your view will show the past value of item because your data didn't changed.

Look link below and search much more about how ListView and RecycleView works.

How ListView's recycling mechanism works



来源:https://stackoverflow.com/questions/45550090/like-unlike-button-recyclerview-image

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!