Android 滚动RecyclerView时查看更改

Android 滚动RecyclerView时查看更改,android,android-layout,Android,Android Layout,我正在使用RecyclerView,我有一个ImageButton作为子视图,我正在单击更改该ImageButton的背景 现在我的问题是,当单击并再次向上滚动以更改ImageButton的图像时,滚动到顶部,设置为初始阶段的状态。我什么都试过了,但没有成功。帮我吧 我的适配器类 public class ViewAllAdapter extends RecyclerView.Adapter<ProductHolder> { int[] objects; Conte

我正在使用
RecyclerView
,我有一个
ImageButton
作为子视图,我正在单击更改该
ImageButton
的背景

现在我的问题是,当单击并再次向上滚动以更改
ImageButton
的图像时,滚动到顶部,设置为初始阶段的状态。我什么都试过了,但没有成功。帮我吧

我的适配器类

public class ViewAllAdapter extends RecyclerView.Adapter<ProductHolder> {

    int[] objects;
    Context context;
    Boolean flag = false;

    public ViewAllAdapter(int[] objects, Context context) {
        super();
        this.objects = objects;
        this.context = context;
    }

    @Override
    public int getItemCount() {
        return objects.length;
    }

    @Override
    public void onBindViewHolder(final ProductHolder arg0, int arg1) {
        arg0.title.setText("Product" + arg1 + 1);
        arg0.aPrice.setPaintFlags(arg0.aPrice.getPaintFlags()
                | Paint.STRIKE_THRU_TEXT_FLAG);
        arg0.aPrice.setText("\u20B9" + " " + "5000");
        arg0.off.setText("56% off");
        arg0.price.setText("\u20B9" + " " + "2300");
        arg0.mainImage.setImageResource(objects[arg1]);
        arg0.ratings.setRating(4f);
        arg0.clickme.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                Intent i = new Intent(MainActivity.this,
                        ProductDetailsPage.class);
                startActivity(i);
            }
        });
        arg0.wish.setBackgroundResource(R.drawable.heart);
        arg0.wish.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                if (!flag) {
                    arg0.wish.setBackgroundResource(R.drawable.heartfade);
                    YoYo.with(Techniques.Tada).duration(550)
                            .playOn(arg0.wish);
                    Toast.makeText(context, "Product Added to wish list..",
                            Toast.LENGTH_SHORT).show();
                    flag = true;
                } else {
                    arg0.wish.setBackgroundResource(R.drawable.heart);
                    Toast.makeText(context,
                            "Product Removed to wish list..",
                            Toast.LENGTH_SHORT).show();
                    flag = false;
                }
            }
        });
    }

    @Override
    public ProductHolder onCreateViewHolder(ViewGroup arg0, int arg1) {
        LayoutInflater inflater = LayoutInflater.from(arg0.getContext());
        View view = inflater.inflate(R.layout.viewall_item, arg0, false);
        return new ProductHolder(view);
    }

    public class ProductHolder extends RecyclerView.ViewHolder {

        protected TextView title;
        protected TextView aPrice;
        protected TextView off;
        protected TextView price;
        protected ImageView mainImage;
        protected RatingBar ratings;
        protected ImageButton wish;
        protected RelativeLayout clickme;

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

            title = (TextView) itemView.findViewById(R.id.productName);
            aPrice = (TextView) itemView
                    .findViewById(R.id.productActualPrice);
            off = (TextView) itemView.findViewById(R.id.offPrice);
            price = (TextView) itemView.findViewById(R.id.productPrice);
            mainImage = (ImageView) itemView
                    .findViewById(R.id.productImage);
            wish = (ImageButton) itemView.findViewById(R.id.addToWishList);
            ratings = (RatingBar) itemView
                    .findViewById(R.id.productRatings);
            clickme = (RelativeLayout) itemView
                    .findViewById(R.id.clickToGO);

        }
    }
}

谢谢您的帮助。

根据您的自定义RecyclerView适配器,我建议您使用HashMap存储值。毕竟,数据模型是将数据加载到RecyclerView的最佳选择

HashMap<Integer, Integer> hashMapTest = new HashMap<>();
在onBindViewHolder(ProductHolder,int位置)中添加以下代码

if(hashMapTest.get(position) != null && hashMapTest.get(position) != false) {
            // Show your ImageButton color Product Added to wish list..
        } else {
            // Show your ImageButton color Product Removed to wish list..
        }

希望对您有所帮助。

在您的模型中维护一个状态变量,并根据状态设置setImageResource我试图将我的
ImageButton
设置为静态,但它说“静态类型只能在静态或顶级类型中声明”。更改您在int[]对象数组中使用的数据模型。将其更改为类似Model[]对象的形式;和模型{int-imageId,boolean-state}您需要保存您单击的项的状态
button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(hashMapTest.get(position) != null && hashMapTest.get(position) != false) {
                    // Code of Product Added to wish list..
                    hashMapTest.put(getPosition(), 1);
                } else {
                    // Code of Product Removed to wish list..
                    hashMapTest.put(getPosition(), 0);
                }

            }
        });
if(hashMapTest.get(position) != null && hashMapTest.get(position) != false) {
            // Show your ImageButton color Product Added to wish list..
        } else {
            // Show your ImageButton color Product Removed to wish list..
        }