selected Image button changes its position in recyclerView when scrolling

后端 未结 1 464
醉酒成梦
醉酒成梦 2021-01-22 02:55

i have a recyclerview in my MainActivity and i\'m showing no of items in list manner through RecyclerView.Adapter. here is my recyclerview_list_items.xml

相关标签:
1条回答
  • 2021-01-22 03:29

    Instead of placing isButtonClicked in adapter class place it in MovieData modal class.Then make following changes in your adapter class:

    public class SuggestionListItemAdapter extends RecyclerView.Adapter<SuggestionListItemAdapter.MyViewHolder> {
    private List<MovieData> moviesList;
    Context context;
    
    public class MyViewHolder extends RecyclerView.ViewHolder {
        public TextView  person_name;
        ImageView person_photo;
        ImageButton person_follow;
        public MyViewHolder(View view) {
            super(view);
            person_photo = (ImageView) view.findViewById(R.id.person_photo);
            person_name = (TextView) view.findViewById(R.id.person_name);
            person_follow = (ImageButton) view.findViewById(R.id.btnfollow);
        }
    }
    public SuggestionListItemAdapter(Context mContext,List<MovieData> moviesList) {
        this.moviesList = moviesList;
        this.context=mContext;
    }
    
    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.recyclerview_list_items, parent, false);
    
        MyViewHolder viewHolder = new MyViewHolder(v);
    
        return viewHolder;
    }
    
    @Override
    public void onBindViewHolder(final MyViewHolder holder, int position) {
        final MovieData movieData = moviesList.get(position);
    
        holder.person_name.setText(movieData.getGenre());
        holder.person_photo.setImageResource(movieData.getPhoto());
        holder.person_follow.setBackgroundResource(movieData.isButtonClicked() ? R.drawable.following_img : R.drawable.follow_inactive);
        holder.person_follow.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                movieData.setIsButtonClicked(!movieData.isButtonClicked());
                holder.person_follow.setBackgroundResource(movieData.isButtonClicked() ? R.drawable.following_img : R.drawable.follow_inactive);
            }
        });
        holder.person_follow.setTag(holder);
    }
    
    @Override
    public int getItemCount() {
        return moviesList.size();
    }
    

    }

    0 讨论(0)
提交回复
热议问题