ViewHolder打破了ImageView

时间:2014-12-11 08:34:37

标签: android android-layout android-listview android-viewholder

我正在为ListView使用自定义适配器。基本上它输入三个东西两个textview和一个RelativeLayout。 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;
    }
} 

1 个答案:

答案 0 :(得分:1)

将以下代码放在此函数中

public View getView(int position,View convertView,ViewGroup parent){}

    convertView = null;
    ViewHolder holder = null;