如何在整个单个ListView控件添加多个头与addHeaderView()?多个、控件、在整个、addHeaderView

2023-09-05 06:34:20 作者:羊村你喜哥

能否Android的addHeaderView()被用来在整个单个ListView控件添加多个头?有人可以给如何做到这一点的例子吗?

我能够完成我想要的东西通过操纵IconicAdapter类......没有任何理由为什么我不应该做这种方式?我觉得这可能被修改为更高级的实现。就我而言,我知道我将有两个部分,在每个部分的标题+ 2行。

 类IconicAdapter扩展ArrayAdapter<字符串> {
    IconicAdapter(){
        超(ContactTabProfileResource.this,R.layout.row_iconic,mArrayList);
    }


    公共查看getView(INT位置,查看convertView,ViewGroup中父){

        LayoutInflater充气= getLayoutInflater();
        查看排= NULL;

        如果(位置== 1 ||位置== 5){//电话
            行= inflater.inflate(R.layout.row_iconic,父母,假);
            TextView的标签=(TextView中)row.findViewById(R.id.label);
            label.setText(mArrayList.get(位置));
            ImageView的图标=(ImageView的)row.findViewById(R.id.rowicon);
            icon.setImageResource(R.drawable.icon_phone);
        }否则,如果(位置== 2 ||位置== 6){//电子邮件
            行= inflater.inflate(R.layout.row_iconic,父母,假);
            TextView的标签=(TextView中)row.findViewById(R.id.label);
            label.setText(mArrayList.get(位置));
            ImageView的图标=(ImageView的)row.findViewById(R.id.rowicon);
            icon.setImageResource(R.drawable.icon_email);
        }否则,如果(位置== 0 ||位置== 4){//节头
            行= inflater.inflate(R.layout.row_header,父母,假);
            TextView的标签=(TextView中)row.findViewById(R.id.label);
            label.setText(mArrayList.get(位置));
            label.setBackgroundColor(Color.GRAY);
        }否则,如果(位置== 3){//节分隔符
            行= inflater.inflate(R.layout.row_header,父母,假);
            TextView的标签=(TextView中)row.findViewById(R.id.label);
            label.setText();
        }

        返回(行);

    }
}
 

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

row_header.xml

 < XML版本=1.0编码=UTF-8&GT?;
<的LinearLayout
  的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
  机器人:layout_width =FILL_PARENT
  机器人:layout_height =WRAP_CONTENT
  机器人:方向=横向
  机器人:重力=右
>

  <的TextView
    机器人:ID =@ + /标记
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =WRAP_CONTENT
    机器人:TEXTSIZE =20SP
    机器人:paddingTop =10dp
    机器人:paddingBottom会=10dp
    机器人:paddingRight =10dp
    机器人:以下属性来=10px的
    机器人:重力=左
    机器人:TEXTSTYLE =黑体
    />
< / LinearLayout中>
 

row_iconic.xml

 < XML版本=1.0编码=UTF-8&GT?;
<的LinearLayout
  的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
  机器人:layout_width =FILL_PARENT
  机器人:layout_height =WRAP_CONTENT
  机器人:方向=横向
  机器人:重力=右
>

  <的TextView
    机器人:ID =@ + /标记
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =WRAP_CONTENT
    机器人:TEXTSIZE =16SP
    机器人:paddingTop =10dp
    机器人:paddingBottom会=10dp
    机器人:paddingRight =10dp
    机器人:以下属性来=44px
    />
    < ImageView的
    机器人:ID =@ + ID / rowicon
    机器人:layout_width =40dp
    机器人:paddingRight =10dp
    机器人:paddingTop =10dp
    机器人:layout_height =30dp
    机器人:SRC =@可绘制/图标
    />
< / LinearLayout中>
 
C listView控件的问题

解决方案

您想要Android开发者可以调用列表分隔或子目(头和页脚只生活在顶部或列表的底部)是什么。你如何能做到这一点的主旨是通过使用ListAdapter它包装等ListAdapters和足够聪明,返回头视图类型的某些行,并保持偏移轨道,或包在自己的迷你适配器这些分离的观点。

看看马克·墨菲的SectionedListAdapter, GPL,它采用第一种方法(由杰夫·夏基基于code),或他的 MergeAdapter ,看this SO质疑。

这是一个相去甚远的iPhone智能列表副标题曼妙的处理,但一旦你环绕究竟发生了什么适配器内部发生在你的脑袋是相当简单的使用MergeAdapter和非常灵活的。

Can Android's addHeaderView() be used to add multiple headers throughout a single ListView? Can someone give an example of how to do this?

I was able to accomplish what I wanted by manipulating the IconicAdapter Class... is there any reason why I should not do it this way? I feel this could be modified for more advanced implementations. In my case, I know that I will have two sections, with a header + 2 rows in each section.

class IconicAdapter extends ArrayAdapter<String> {
    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);

    }
}

Then I created two different XML layouts. row_header.xml is for the header rows and row_iconic.xml is for the non-header rows, which contain an icon.

row_header.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  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"
>    

  <TextView
    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"
    />    
</LinearLayout>

row_iconic.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  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"
>    

  <TextView
    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"
    />  
    <ImageView
    android:id="@+id/rowicon"
    android:layout_width="40dp"
    android:paddingRight="10dp"
    android:paddingTop="10dp"
    android:layout_height="30dp"        
    android:src="@drawable/icon"
    />    
</LinearLayout>

解决方案

What you want Android developers might call a list separator or subheading ("headers" and "footers" live only at the top or bottom of the list). The gist of how you can do this is by using a ListAdapter that wraps other ListAdapters and is smart enough to return a header view type for certain rows and keep track of the offsets, or wraps those separator views in their own mini adapters.

Take a look at Mark Murphy's SectionedListAdapter, GPL, which takes the first approach (based on code by Jeff Sharkey), or his MergeAdapter, and see this SO question.

It's a far cry from the graceful handling of smart list subheadings on the iPhone, but it is fairly straightforward to use MergeAdapter and very flexible once you wrap your head around exactly what's going on inside the adapters.