如何在android中的列表视图中添加标题部分?

时间:2014-03-29 07:42:49

标签: android android-listview baseadapter

我有android应用程序,显示一个listview,扩展Base Adapter以获取图像和文本。 现在我想为此列表视图添加标题部分如何操作?任何人都可以帮助我???

列表视图的显示方式为enter image description here

我知道的是,在第一个项目显示标题之前和最后一个项目之前显示标题。

activity_group_list.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".GroupList" >

    <ListView
        android:id="@+id/list"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" >
    </ListView>

</RelativeLayout>

row_list_group.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:padding="5dip" >

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_centerVertical="true"
        android:src="@drawable/algeria_flag" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_toRightOf="@+id/imageView1"
        android:ems="10"
        android:text="text" />

</RelativeLayout>

ItemDetails.java

package com.devleb.expandablelistdemo3;

public class ItemDetails {

    String Name;
    int image;
    public String getName() {
        return Name;
    }
    public void setName(String name) {
        Name = name;
    }
    public int getImage() {
        return image;
    }
    public void setImage(int image) {
        this.image = image;
    }


}

CustomAdapter.java

package com.devleb.expandablelistdemo3;

import java.util.ArrayList;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView.FindListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class CustomAdapter extends BaseAdapter {

    private static ArrayList<ItemDetails> itemDetailsarrayList;

    LayoutInflater layoutInflater;
    String[] teamName;
    int[] teamFlag;
    Context context;

    public CustomAdapter(ArrayList<ItemDetails> result, Context c) {
        itemDetailsarrayList = result;
        context = c;
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return itemDetailsarrayList.size();
    }

    @Override
    public Object getItem(int arg0) {
        // TODO Auto-generated method stub
        return itemDetailsarrayList.get(arg0);
    }

    @Override
    public long getItemId(int arg0) {
        // TODO Auto-generated method stub
        return arg0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub

        layoutInflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View row = layoutInflater.inflate(R.layout.row_list_group, parent,
                false);
        TextView txt = (TextView) row.findViewById(R.id.textView1);
        ImageView imgv = (ImageView) row.findViewById(R.id.imageView1);
        txt.setText(itemDetailsarrayList.get(position).getName());
        imgv.setImageResource(itemDetailsarrayList.get(position).getImage());

        return row;
    }

}

GroupList.java

package com.devleb.expandablelistdemo3;

import java.lang.reflect.Array;
import java.util.ArrayList;

import android.os.Bundle;
import android.app.Activity;
import android.content.ClipData.Item;
import android.view.Menu;
import android.widget.ListView;

public class GroupList extends Activity {

    int[] img = { R.drawable.brazil_flag, R.drawable.croatian_flag,
            R.drawable.mexico_flag, R.drawable.cameroon_flag, R.drawable.spain,
            R.drawable.netherlands_flag, R.drawable.czech_republic_flag,
            R.drawable.australia, R.drawable.colombia_flag, R.drawable.gress,
            R.drawable.cote_divoire_flag, R.drawable.japan,
            R.drawable.uruguay_flag, R.drawable.costa_rica_flag,
            R.drawable.england_flag, R.drawable.italy_flag,
            R.drawable.switzerland, R.drawable.ecuador_flag,
            R.drawable.france_flag, R.drawable.honduras_flag,
            R.drawable.argentina_flag, R.drawable.bousna, R.drawable.iran_flag,
            R.drawable.nigeria_flag, R.drawable.germany_flag,
            R.drawable.portugal, R.drawable.ghana_flag,
            R.drawable.united_states_flag, R.drawable.belgium_flag,
            R.drawable.algeria_flag, R.drawable.russia_flag,
            R.drawable.korea_flag };
    String[] name = { "BRA", "CRO", "MEX", "CMR", "ESP", "NED", "CHI", "AUS",
            "COL", "GRE", "CIV", "JPN", "URU", "CRC", "ENG", "ITA", "SUI",
            "ECU", "FRA", "HON", "ARG", "BIH", "IRN", "NGA", "GER", "POR",
            "GHA", "USA", "BEL", "ALG", "RUS", "KOR" };

    ItemDetails item_details;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_group_list);

        ArrayList<ItemDetails> result = getList();
        ListView lv = (ListView) findViewById(R.id.list);
        lv.setAdapter(new CustomAdapter(result, getApplicationContext()));

    }

    private ArrayList<ItemDetails> getList() {
        ArrayList<ItemDetails> results = new ArrayList<ItemDetails>();
        for (int i = 0; i <32; i++) {
            item_details = new ItemDetails();
            item_details.setName(name[i]);
            item_details.setImage(img[i]);
            results.add(item_details);
        }
        return results;
    }

添加了部分

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_group_list);

        TextView txtSection = (TextView)findViewById(R.id.textSeparator);

        ArrayList<ItemDetails> result = getList();
        lv = (ListView) findViewById(R.id.list);
        lv.addHeaderView(txtSection);
        lv.setAdapter(new CustomAdapter(result, getApplicationContext()));

    }

    private ArrayList<ItemDetails> getList() {
        ArrayList<ItemDetails> results = new ArrayList<ItemDetails>();
        for (int i = 0; i <4; i++) {
            item_details = new ItemDetails();
            View layout_section = getLayoutInflater().inflate(R.layout.activity_group_section_list, null);
            lv.addHeaderView(layout_section );
            item_details.setName(name[i]);
            item_details.setImage(img[i]);
            results.add(item_details);
        }
        return results;
    } 


  }

log cat

03-29 08:30:34.271: E/AndroidRuntime(2900): Caused by: java.lang.NullPointerException
03-29 08:30:34.271: E/AndroidRuntime(2900):     at com.devleb.expandablelistdemo3.GroupList.getList(GroupList.java:59)

2 个答案:

答案 0 :(得分:0)

在onCreate方法的GroupList中,只需添加以下行:

ListView lv = (ListView) findViewById(R.id.list);
lv.addHeaderView(<place your header view here>);

addHeaderView期望获取View对象。因此,您可以使用两种方式放置标题:

//Programmatically create button (or other view, that you need) and place it:
Button b = new Button(this);
lv.addHeaderView(b);

// Create xml layout, inflate it and place into listview as header view
View yourLayout = getLayoutInflater().inflate(R.layout.your_view_layout, null);
lv.addHeaderView(yourLayout );

答案 1 :(得分:0)

在row_list_group.xml中,创建标题布局并将其可见性设置为已消失。然后在需要添加标题时将其设置为可见。

或者你可以试试Nickolai的建议:

这样你需要创建一个布局并在列表适配器getView方法的运行时添加到listview。