2016-04-03 16 views
0

Geri dönüştürücüdeGörüntülerim Tüm ImageButtons'un arka planı olarak uygulanan özel bir şekle sahip imageButtons listesi var. colors[]Özel şekilli bir ImageButton'un arka planını RecyclerView'da nasıl ayarlanır?

Bu şu anda böyle görünüyor:

enter image description here

Ben indeksi 0 yerine bir icon.png koymak istiyorum (Her ımagebutton henüz colorlist gelen düz bir renk vardır düz renk yerine kahverengi renkli düğme). Şimdiye kadar, kendimi bir çözüm bulmaya çalışırken, günlerimi kullanmadan birçok gün kullandım. Bu renkleri belirleyen recyclerView adaptörü kod

geçerli:: İşte

ilgili tüm kodudur

Resources resources = App.getAppContext().getResources(); 
String colors[] = resources.getStringArray(R.array.backgroundcolors); 

@Override 
public void onBindViewHolder(ViewHolder holder, int position) { 


    Drawable drawable = holder.colorButton.getBackground(); 

    //This is here where each ImageButton gets a color from the colorlist colors[] 

    if (drawable instanceof GradientDrawable) { 
     GradientDrawable gd = (GradientDrawable) drawable.getCurrent(); 
     gd.setColor(Color.parseColor(colors[position])); 

     //This does nothing 
    } else if (drawable instanceof RippleDrawable) { 
     RippleDrawable rd = (RippleDrawable) drawable; 
     int color = Color.parseColor(colors[position]); 
     rd.setColor(newColorStateList(color)); 
    } 
} 

her ımagebutton için colorStatList kodu:

private static ColorStateList newColorStateList(int color) { 
    int[][] states = new int[][]{ 

      new int[]{android.R.attr.state_enabled}, // enabled 
      new int[]{-android.R.attr.state_enabled}, // disabled 
    }; 

    int[] colors = new int[]{ 
      color, color 
    }; 
    return new ColorStateList(states, colors); 
} 

Geri dönüşüm için özel düğmem Görünüm A dapter: Ben String color; neden

public class ColorButton{ 

private ImageButton button; 
private String color; 
public static List<ColorButton> colorButtonList; 


public ColorButton(ImageButton button, String color) { 
    this.button = button; 
    this.color = color; 
} 

static ColorButton colorButton = new ColorButton(new ImageButton(App.getAppContext()), null); 

public static List<ColorButton> initColorButtons(){ 
    colorButtonList = new ArrayList<>(); 

    Resources resources = App.getAppContext().getResources(); 
    String colors[] = resources.getStringArray(R.array.backgroundcolors); 


    for(int i=0; i<colors.length; i++){ 

     colorButtonList.add(new ColorButton(new ImageButton(App.getAppContext()), colors[i])); 
    } 

    return colorButtonList; 
} 

merak edebilirsiniz Bu benim uygulamanın arka plan rengini ayarlamak için bir colorbutton tıkladığında zaman: mEditText.setBackgroundColor(Color.parseColor((mColorButtons.get(position).getColor())));

+0

ImageButton kullanıyorsanız, xml'de iki özellik vardır: 'background' - kendi şeklinizi uygulayın, ancak bu şekildeki rengi saydam (alpha = 0) ve' src' özelliği -> buraya koyun resmi ayarlayabilirsiniz – Vucko

cevap

3

bu deneyin:

@Override 
public void onBindViewHolder(ViewHolder holder, int position) { 
if(position ==0){ 
holder.colorButton.setBackgroundResource(R.drawable.colorpicker2); 
} 
else 
{ 
GradientDrawable gd = context.getResources().getDrawable(R.drawable.bbshape); 
gd.setColor(Color.parseColor(colors[position])); 
holder.colorButton.setBackGroundDrawable(gd); 
} 
} 
+0

Teşekkürler! Bir çekicilik gibi çalıştı! – Muddz

İlgili konular