开发者

Style first 2 TextViews in Android ListView differently

I have a ListView and I want the first two entries in it to be displayed differently than the rest. Nothing fancy, I want them all to just be text views. But the first two entries need to have different sizes and weights than the rest. I tried modifying the ArrayAdapter class like so:

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;
  }

}

But this inadvertantly causes some of the other textviews to take on these special attributes. I think it's because cause textviews get "recycled" in the AbsListAdapter class.


Try like this:

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.........
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜