滚动RecyclerView时查看更改

时间:2015-11-02 10:55:56

标签: android android-layout

我正在使用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);

        }
    }
}

在我的 MainActicity 中,我正在做

rv = (RecyclerView) findViewById(R.id.gvViewAll);
    rv.setHasFixedSize(true);
    GridLayoutManager glm = new GridLayoutManager(getApplicationContext(),
            2);
    rv.setLayoutManager(glm);

    final ViewAllAdapter adp = new ViewAllAdapter(productImage,
            getApplicationContext());
    rv.setAdapter(adp);

    rvh = (RecyclerViewHeader) findViewById(R.id.header);
    rvh.attachTo(rv, true);

感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

根据您的Custom RecyclerView适配器,我建议您使用HashMap存储值。所有数据模型都是将数据加载到RecyclerView的最佳选择。

HashMap<Integer, Integer> hashMapTest = new HashMap<>();

当用户点击ImageButton然后添加以下代码:

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);
                }

            }
        });

在你的onBindViewHolder(ProductHolder holder,int position)中添加以下代码

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..
        }

希望它对你有所帮助。