滚动时,选定的图像按钮会更改其在 recyclerView 中的位置

selected Image button changes its position in recyclerView when scrolling

我的 MainActivity 中有一个 recyclerview,并且我没有通过 RecyclerView.Adapter 以列表方式显示任何项目。这是我的 recyclerview_list_items.xml 文件,

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:padding="10dp">

    <ImageView
        android:id="@+id/person_photo"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:scaleType="centerCrop"
        android:src="@drawable/rounded_img" />

    <TextView
        android:id="@+id/person_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_toLeftOf="@+id/btnfollow"
        android:layout_toRightOf="@+id/person_photo"
        android:ellipsize="end"
        android:paddingLeft="10dp"
        android:paddingRight="4dp"
        android:singleLine="true"
        android:text="*********"
        android:textColor="#303030"
        android:textSize="17sp" />

    <ImageButton
        android:id="@+id/btnfollow"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_marginRight="8dp"
        android:layout_marginTop="4dp"
        android:background="@drawable/follow_inactive" />

</RelativeLayout>

这是我的适配器 class 文件,

public class SuggestionListItemAdapter extends RecyclerView.Adapter<SuggestionListItemAdapter.MyViewHolder> {
    private List<MovieData> moviesList;
    Context context;
    private boolean isButtonClicked = false;
    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(MyViewHolder holder, int position) {
        MovieData movieData = moviesList.get(position);

        holder.person_name.setText(movieData.getGenre());
        holder.person_photo.setImageResource(movieData.getPhoto());
        holder.person_follow.setOnClickListener(clickListener);
        holder.person_follow.setTag(holder);
    }

        View.OnClickListener clickListener = new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (v.getId() == R.id.btnfollow) {
                    isButtonClicked = !isButtonClicked; // toggle the boolean flag
                    v.setBackgroundResource(isButtonClicked ? R.drawable.following_img : R.drawable.follow_inactive);
                }

            }
        };


    @Override
    public int getItemCount() {
        return moviesList.size();
    }


}

单击 holder.person_follow 图像按钮,可绘制的图像背景正在相应地改变我想要的,但是当我滚动页面时,可绘制的图像背景会自动改变其位置。例如。如果我 select 图像按钮位置 = 1,则在滚动页面后它会更改 selected 按钮位置。

不要将 isButtonClicked 放在 adapter class 中,而是将其放在 MovieData 模态 class.Then 中,在 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();
}

}