2016-08-21 67 views
-1

假設您必須爲RecyclerView創建一個小型ImageButton的列表,每個具有不同的顏色或背景。在Android中爲RecyclerView創建ImageButtons模型類的正確方法

哪一個模型類是正確的方法?

,其延伸的ImageButton

public class ColorButton2 extends ImageButton{ 

private Context context; 
private boolean isPlain, isWallpaper, isTextured; 
private int color; 

public ColorButton2(Context context,int color,boolean isPlain,boolean isTextured, boolean isWallpaper) { 
    super(context); 
    this.color = color; 
    this.isPlain = isPlain; 
    this.isWallpaper = isWallpaper; 
    this.isTextured = isTextured; 
} 

具有的ImageButton作爲成員變量的類

模型類:

public class ColorButton2{ 

private ImageButton imageButton 
private Context context; 
private boolean isPlain, isWallpaper, isTextured; 
private int color; 

public ColorButton2(Context context,int color,boolean isPlain,boolean isTextured, boolean isWallpaper) { 
    super(context); 
    this.color = color; 
    this.isPlain = isPlain; 
    this.isWallpaper = isWallpaper; 
    this.isTextured = isTextured; 
} 

吸氣劑和setter是代碼的過程的一部分。

回答

2

您可以在自定義視圖項添加圖片按鈕,然後在它充氣和回收視圖,並設置它的顏色編程方式從模型

@Override 
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int 
    viewType) 
{ 
     //inflate custom view custom_items 

    View itemView = LayoutInflater.from(parent.getContext()) 
      .inflate(R.layout.custom_items, parent, false); 

    return new MyViewHolder(itemView); 
} 

申報圖像按鈕

public class MyViewHolder extends RecyclerView.ViewHolder { 
    public TextView title; 
    ImageButton imgButton; 
    LinearLayout linSetting; 
    public MyViewHolder(View view) { 
     super(view); 
     imgButton = (ImageView) view.findViewById(R.id.imgButton); 


    } 
} 

,然後設置顏色和任何其他你想要的參數

@Override 
public void onBindViewHolder(MyViewHolder holder, 
@SuppressLint("RecyclerView") final int position) { 
    ColorButton2 colorModel = arrayList.get(position); 

    holder.imgButton.setImageResource(colorModel.getColor()); 


} 

所以你有第二個模型類但你並不需要獲得ImageButton的實例