ListView 用のカスタム アダプターを使用していました。基本的に、2 つのテキストビューと RelativeLayout の 3 つに入ります。RelativeLayout は、色のブロックを表示するために使用されます。以前は、以下の同じコードを使用していましたが、ViewHolder は使用せず、findViewById() を使用して getView() メソッドからすべてを呼び出していました。これは完璧に機能しました。ViewHolder を使用して速度を少し上げたところ、突然カラー ブロックが表示されなくなりました。私が得るのは、カラーブロックがあるはずの白いブロックがあるはずのすべてのテキストです(RelativeLayoutの背景のデフォルトの色設定が黒であるため、これは興味深いことです)。
コードは次のとおりです。
public class colorlibListViewAdapterClass extends ArrayAdapter<String>
{
//Context storage. Will be populate from creation call in display class
private final Context context;
//Class location to store array of HEX values - populate from class call.
private final String[] values;
public Typeface typeface;
public Typeface typefaceSecond;
static class ViewHolder
{
public TextView textView;
public TextView rgbText;
public RelativeLayout colorBlock;
}
public colorlibListViewAdapterClass(Context context, String[] values)
{
//R.layout.rowlayout is the custom look for the row. Stored in layout folder
super(context, R.layout.rowlayout, values);
this.context = context;
this.values = values;
typeface = Typeface.createFromAsset(context.getAssets(), "fonts/cabinbold.otf");
typefaceSecond = Typeface.createFromAsset(context.getAssets(), "fonts/cabinregular.otf");
}
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
View rows = convertView;
if (rows == null)
{
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
rows = inflater.inflate(R.layout.rowlayout, null);
// configure view holder
ViewHolder viewHolder = new ViewHolder();
viewHolder.textView = (TextView) rows.findViewById(R.id.title);
viewHolder.rgbText = (TextView) rows.findViewById(R.id.rbgText);
viewHolder.colorBlock = (RelativeLayout) rows.findViewById(R.id.colorBlock);
rows.setTag(viewHolder);
}
//Prepare the inflation service.
ViewHolder holder = (ViewHolder) rows.getTag();
//Create variables to reference the items on the Listview.
//The ImageView is the background used to set colour and TextView is used to display HEX code.
//Set custom font to the HEX Textview
holder.textView.setTypeface(typeface);
//Set Custom font to the RGB TextView
holder.rgbText.setTypeface(typefaceSecond);
//Create instance of libraryClass
libraryClass colorLib = new libraryClass();
//HERE BE CODE FOR DECIDING WHETHER RBG OR HSV
//Variable for getting HSV
float[] hsvReturn;
//Get HSV - Returned as array h,s,v
hsvReturn = colorLib.getHSV(Integer.parseInt(values[position], 16)+0xFF000000);
//Create and initialize integer variable for conversion
int[] hsvInt = {0,0,0};
//Round off to get rid of excess and conver to percentage rather than 0.*
hsvReturn[0] = Math.round(hsvReturn[0]);
hsvReturn[1] = Math.round(hsvReturn[1] * 100);
hsvReturn[2] = Math.round(hsvReturn[2] * 100);
//Cast to int
hsvInt[0] = (int)hsvReturn[0];
hsvInt[1] = (int)hsvReturn[1];
hsvInt[2] = (int)hsvReturn[2];
//Variable to display
String hsvDisplay;
//Construct display
hsvDisplay = Integer.toString(hsvInt[0]) + (char) 0x00B0 + " , " + Integer.toString(hsvInt[1]) + "% , " + Integer.toString(hsvInt[2]) + "%";
//Set display
holder.rgbText.setText(hsvDisplay);
//Set RGB TextView
//rgbText.setText(colorLib.hex2Rgb(values[position]));
//Set the HEX color code name to the textview on the listview
holder.textView.setText(values[position]);
//Set background to defined colour. parseInt(values[position], 16)+0xFF000000) tells us it's a HEX colour without hash
holder.colorBlock.setBackgroundColor(Integer.parseInt(values[position], 16) + 0xFF000000);
//Text color should be ABB7B7 if sat < 14.5 and brightness > 98
return rows;
}
}