listview仅突出显示第一行

时间:2016-09-28 09:07:49

标签: android listview

大家好我有一个列表,我想只突出显示它的第一行,但问题是第一行 (这是最后一行)看不见的行(滚动后变得可见)也被突出显示,当我滚动得太快时,第二行和最后一行得到突出显示 这是我的适配器请帮助

 public class ValueItemsListArrayAdapter extends ArrayAdapter<ValueItem> {

private final Activity context;
private final ArrayList<ValueItem> mValueItem;
int listHeight;
ListView lv;
boolean multiOwner;
boolean containRisk;

//private boolean fillScreen;
public ValueItemsListArrayAdapter(Activity context, ArrayList<ValueItem> mValueItem, boolean multiOwner,boolean containRisk) {
    super(context, R.layout.result_list_item, mValueItem);
    this.context = context;
    this.mValueItem = mValueItem;
    lv = (ListView) context.findViewById(R.id.valueslist);
    this.multiOwner = multiOwner;
    this.containRisk = containRisk;
    //listHeight=lv.getHeight();
    //this.fillScreen=fillScreen;
}
// static to save the reference to the outer class and to avoid access to
// any members of the containing class

private class ViewHolder {
    public TextView titleTextView, valueTextView;
    public View row;
    public ImageView icon;
    public LinearLayout llResult;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    // ViewHolder will buffer the assess to the individual fields of the row layout

    final ViewHolder holder;
    // Recycle existing view if passed as parameter
    // This will save memory and time on Android
    // This only works if the base layout for all classes are the same
    View rowView = convertView;
    if (rowView == null) {
        LayoutInflater inflater = context.getLayoutInflater();
        rowView = inflater.inflate(R.layout.result_list_item, null, true);

        holder = new ViewHolder();

        holder.titleTextView = (TextView) rowView.findViewById(R.id.result_title);
        holder.llResult = (LinearLayout) rowView.findViewById(R.id.llResult);

        holder.valueTextView = (TextView) rowView.findViewById(R.id.result_value);
        holder.row = rowView.findViewById(R.id.row);
        holder.icon = (ImageView) rowView.findViewById(R.id.many_owner_icon);
        ViewFont.setListRowTextResizing(rowView, context);


        rowView.setTag(holder);

    } else {
        holder = (ViewHolder) rowView.getTag();
    }

    //String s=mValueItem.get(position).getTitle();
    //  int x=holder.titleTextView.getHeight();
    holder.titleTextView.setText(mValueItem.get(position).getTitle());
    if (multiOwner && mValueItem.get(position).getTitle().equals(Plate.getOwnerNameTitle())) {
        //rowView.setBackgroundColor(Color.GREEN);
        holder.titleTextView.setTextColor(Color.rgb(57, 113, 67));
        holder.valueTextView.setTextColor(Color.rgb(57, 113, 67));
        holder.icon.setVisibility(View.VISIBLE);

        if (containRisk && position == 0) {
            holder.llResult.setBackgroundColor(Color.RED);
            holder.icon.setImageResource(R.drawable.car_risk);
        }


    } else {
        holder.valueTextView.setTextColor(Color.BLACK);
        holder.titleTextView.setTextColor(Color.BLACK);
        holder.icon.setVisibility(View.GONE);
        if (containRisk && position == 0) {
            holder.llResult.setBackgroundColor(Color.RED);
            holder.icon.setVisibility(View.VISIBLE);
            holder.icon.setImageResource(R.drawable.car_risk);
        }
        Log.wtf("position",""+position);
    }

    holder.valueTextView.setText(mValueItem.get(position).getValue());
    //holder.valueTextView.setTextColor(mValueItem.get(position).getColor());
    if (mValueItem.get(position).getBackground() != 0)
        holder.row.setBackgroundResource(mValueItem.get(position).getBackground());
    else
        holder.row.setBackgroundResource(R.drawable.result_text);

    Log.d("pos",""+position);

    return rowView;


}


}

3 个答案:

答案 0 :(得分:0)

if (position != 0)
    holder.row.setBackgroundResource(mValueItem.get(position).getBackground());
else
    holder.row.setBackgroundResource(R.drawable.result_text);

将上面的代码放在下面(见评论)

if (rowView == null) {
    LayoutInflater inflater = context.getLayoutInflater();
    rowView = inflater.inflate(R.layout.result_list_item, null, true);

    holder = new ViewHolder();

    holder.titleTextView = (TextView) rowView.findViewById(R.id.result_title);
    holder.llResult = (LinearLayout) rowView.findViewById(R.id.llResult);

    holder.valueTextView = (TextView) rowView.findViewById(R.id.result_value);
    holder.row = rowView.findViewById(R.id.row);
    holder.icon = (ImageView) rowView.findViewById(R.id.many_owner_icon);
    ViewFont.setListRowTextResizing(rowView, context);

    //put above code here

    rowView.setTag(holder);
}

答案 1 :(得分:0)

由于listview的默认行为,这种情况正在发生。正如您使用持有者一样,它总是重用视图。为了让它在第一行发生,你去:

if (multiOwner && mValueItem.get(position).getTitle().equals(Plate.getOwnerNameTitle())) {
    //rowView.setBackgroundColor(Color.GREEN);
    holder.titleTextView.setTextColor(Color.rgb(57, 113, 67));
    holder.valueTextView.setTextColor(Color.rgb(57, 113, 67));
    holder.icon.setVisibility(View.VISIBLE);

    if (containRisk && position == 0) {
        holder.llResult.setBackgroundColor(Color.RED);
        holder.icon.setImageResource(R.drawable.car_risk);
    }else{

       // Set the default color which you want or which you already set to your row add your else part here and your are done

       holder.llResult.setBackgroundColor(Color.WHITE);
     }


} else {
    holder.valueTextView.setTextColor(Color.BLACK);
    holder.titleTextView.setTextColor(Color.BLACK);
    holder.icon.setVisibility(View.GONE);
    if (containRisk && position == 0) {
        holder.llResult.setBackgroundColor(Color.RED);
        holder.icon.setVisibility(View.VISIBLE);
        holder.icon.setImageResource(R.drawable.car_risk);
    }else{
      // Set the default color which you want or which you already set to your row add your else part here and your are done

       holder.llResult.setBackgroundColor(Color.WHITE);
     }
    Log.wtf("position",""+position);
}

答案 2 :(得分:0)

仅在getView方法中检查第一行的位置。

if (position != 0)
         holder.row.setBackgroundColor(Color.WHITE);
    else
        holder.row.setBackgroundColor(Color.RED);