有标题的自定义列表视图的建议

时间:2011-12-06 20:02:54

标签: android listview layout

我想在列表中添加标题自定义列表视图。我找到了一些提示,但可以解决我的问题。所以也许这里有人可以提供线索:

我的班级(包括两个班级)

package com.droidfish.apps.acli;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class ListProduct2 extends Activity {
private static ArrayList<String> sProducts;
private static String[] sPro;
private Integer iType;

private static class EfficientAdapter extends BaseAdapter {
    private LayoutInflater mInflater;

    public EfficientAdapter(Context context) {
        mInflater = LayoutInflater.from(context);

    }

    public int getCount() {
        return sPro.length;
    }

    public Object getItem(int position) {
        return position;
    }

    public long getItemId(int position) {
        return position;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.list_item, null);
            holder = new ViewHolder();
            holder.text = (TextView) convertView
                    .findViewById(R.id.listitemdef);
            Log.d("Create item", sPro[position]);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        holder.text.setText(sPro[position]);

        return convertView;
    }

    static class ViewHolder {
        TextView text;
    }
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.listproducts);
    savedInstanceState = this.getIntent().getExtras();
    sProducts = savedInstanceState.getStringArrayList("aircrafts");
    sPro = (String[]) sProducts.toArray(new String[0]);
    iType = savedInstanceState.getInt("type");
    ListView l1 = (ListView) findViewById(R.id.ListView01);
    TextView titleList = (TextView) findViewById(R.id.titleProductList);
    TextView titleList2 = new TextView(this);
    titleList2.setText("Manufacturer");
    l1.addHeaderView(titleList2);
    l1.setAdapter(new EfficientAdapter(this));

(这个,android.R.layout.simple_list_item_1,sPro));     }

private static final String[] country = { "Iceland", "India", "Indonesia",
        "Iran", "Iraq", "Ireland", "Israel", "Italy", "Laos", "Latvia",
        "Lebanon", "Lesotho ", "Liberia", "Libya", "Lithuania",
        "Luxembourg" };

}

我的listview布局

<?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="fill_parent" > 
<ListView
    android:id="@+id/ListView01"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" >
</ListView>

<TextView
    android:id="@+id/titleProductList"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Title" />
</LinearLayout>

布局列表项

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="left|center"
android:paddingBottom="5px"
android:paddingLeft="5px"
android:paddingTop="5px" >
<TextView
    android:id="@+id/listitemdef"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:text="hi"
    android:textColor="#FFFF00" >
</TextView>
</LinearLayout>

欢迎任何建议

2 个答案:

答案 0 :(得分:1)

您可以使用here

所述的addHeaderView方法

答案 1 :(得分:0)

我看到了您的问题,您没有指定LinearLayout的orientation属性,默认为水平。因此,textview将添加到列表视图的右侧。此外,创建标题视图的最简单方法是使用layout_weight属性。所以,试试这个:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >  <!-- missing orientation-->
  <TextView
    android:id="@+id/titleProductList"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Title" />

  <ListView
    android:id="@+id/ListView01"
    android:layout_width="fill_parent"
    android:layout_height="0dp"
    android:layout_weight="1" > <!-- weight attribute -->
</ListView>


</LinearLayout>