将Android ListView中的前两个TextViews设置为不同

时间:2010-12-21 05:14:47

标签: android listview textview

我有一个ListView,我希望其中的前两个条目与其余条目的显示方式不同。没什么好看的,我希望它们都只是文本视图。但前两个条目需要具有不同的尺寸和重量。我尝试修改ArrayAdapter类,如下所示:

private class BusStopAdapter<T> extends ArrayAdapter<T>{

  public BusStopAdapter(  
    Context context, int textViewResourceId,
    List<T> objects)
  {
    super(context, textViewResourceId, objects);
  }

  public View getView(int position, View convertView, 
    ViewGroup parent)
  {
    TextView toReturn = 
      (TextView)super.getView(position, convertView, parent);
    if(position == 0){
     toReturn.setTextSize(12);
     toReturn.setText("Previous Bus: " + toReturn.getText());
     toReturn.setPadding(0,0,0,0);
    }
    else if(position == 1){
      toReturn.setTextSize(20);
      toReturn.setPadding(
        toReturn.getPaddingLeft(),
        0,
        toReturn.getPaddingRight(),
        0
      );
      toReturn.setText("Next Bus: " + toReturn.getText());
      toReturn.setGravity(Gravity.CENTER_HORIZONTAL|Gravity.TOP);
    }
    return toReturn;
  }

}

但是这无意中导致其他一些文本视图呈现出这些特殊属性。我认为这是因为原因文本视图在AbsListAdapter类中被“回收”。

1 个答案:

答案 0 :(得分:1)

试试这样:

if(position == 0){
     toReturn.setTextSize(12);
     toReturn.setText("Previous Bus: " + toReturn.getText());
     toReturn.setPadding(0,0,0,0);
    }
    else if(position == 1){
      toReturn.setTextSize(20);
      toReturn.setPadding(
        toReturn.getPaddingLeft(),
        0,
        toReturn.getPaddingRight(),
        0
      );
   else
      style it normally or whatever you want.........