android如何在listview使用addheaderview,如何使用addHeaderView()在单个ListView中添加多个标题?...

Android的addHeaderView()可用于在单个ListView中添加多个标头吗?有人可以举例说明如何做到这一点吗?

通过操作IconicAdapter类,我能够完成我想要的...有什么理由不这样做吗?我觉得这可以修改为更高级的实现。在我的情况下,我知道我将有两个部分,每个部分有一个标题+ 2行。

class IconicAdapter extends ArrayAdapter {

IconicAdapter() {

super(ContactTabProfileResource.this, R.layout.row_iconic, mArrayList);

}

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

LayoutInflater inflater = getLayoutInflater();

View row = null;

if(position == 1 || position == 5) { // phone

row = inflater.inflate(R.layout.row_iconic, parent, false);

TextView label =(TextView)row.findViewById(R.id.label);

label.setText(mArrayList.get(position));

ImageView icon = (ImageView)row.findViewById(R.id.rowicon);

icon.setImageResource(R.drawable.icon_phone);

} else if (position == 2 || position == 6) { // email

row = inflater.inflate(R.layout.row_iconic, parent, false);

TextView label =(TextView)row.findViewById(R.id.label);

label.setText(mArrayList.get(position));

ImageView icon = (ImageView)row.findViewById(R.id.rowicon);

icon.setImageResource(R.drawable.icon_email);

} else if (position == 0 || position == 4) { // section header

row = inflater.inflate(R.layout.row_header, parent, false);

TextView label =(TextView)row.findViewById(R.id.label);

label.setText(mArrayList.get(position));

label.setBackgroundColor(Color.GRAY);

} else if (position == 3) { // section divider

row = inflater.inflate(R.layout.row_header, parent, false);

TextView label =(TextView)row.findViewById(R.id.label);

label.setText(" ");

}

return(row);

}

}

然后我创建了两个不同的XML布局。 row_header.xml用于标题行,row_iconic.xml用于非标题行,其中包含图标。

row_header.xml

xmlns:android="http://schemas.android.com/apk/res/android"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:orientation="horizontal"

android:gravity="right"

>

android:id="@+id/label"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:textSize="20sp"

android:paddingTop="10dp"

android:paddingBottom="10dp"

android:paddingRight="10dp"

android:paddingLeft="10px"

android:gravity="left"

android:textStyle="bold"

/>

row_iconic.xml

xmlns:android="http://schemas.android.com/apk/res/android"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:orientation="horizontal"

android:gravity="right"

>

android:id="@+id/label"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:textSize="16sp"

android:paddingTop="10dp"

android:paddingBottom="10dp"

android:paddingRight="10dp"

android:paddingLeft="44px"

/>

android:id="@+id/rowicon"

android:layout_width="40dp"

android:paddingRight="10dp"

android:paddingTop="10dp"

android:layout_height="30dp"

android:src="@drawable/icon"

/>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值