2015-11-02 49 views
0

我正在使用RecyclerView,我有一个ImageButton作为子视图,我将单击后更改该ImageButton的背景。查看更改滚动时RecyclerView

现在我的问题是,当通过点击改变图片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); 

感谢您的帮助。

+0

在你的模型中维护一个状态变量,并根据状态设置setImageResource –

+0

我试图让我的ImageButton静态,但它说“静态类型只能在静态或顶级类型中声明”。 –

+0

改变你的数据模型,你把INT []对象数组。改变它像Model []对象;和型号{INT图像标识,布尔州} –

回答

0

根据您的自定义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持有人,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.. 
     } 

希望它可以帮助你。