2015-11-02 102 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.. 
     } 

希望它可以幫助你。