创建分段列表视图

时间:2012-03-20 11:54:13

标签: android android-listview

我目前正在创建分段列表视图。到目前为止,我已经制作了一个带有标题和项目列表的列表视图。最终这应该变成我可以在我的项目中使用的模板。我需要知道的是如何在以下代码中在此标题后面添加标题和相应的项目列表:

public class MainActivity extends Activity {

private ListView listView1;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    Preferences preference1_data[] = new Preferences[]
            {
                new Preferences(R.drawable.bird, "Bird Preference"),
                new Preferences(R.drawable.gear, "Gear Preference"),
                new Preferences(R.drawable.planet, "Planet Preference"),
            };

    Preferences preference2_data[] = new Preferences[]
            {
                new Preferences(R.drawable.bird, "Bird Preference"),
                new Preferences(R.drawable.gear, "Gear Preference"),
                new Preferences(R.drawable.planet, "Planet Preference"),
            };

    PreferencesAdapter adapter = new PreferencesAdapter(this, 
            R.layout.listview_item_row, preference1_data);


    listView1 = (ListView)findViewById(R.id.listView1);

    // Add Preference 1 section
    View header = (View)getLayoutInflater().inflate(R.layout.listview_header_row, null);
    listView1.addHeaderView(header);
    listView1.setAdapter(adapter);

    // Add Preference 2 section

    // What code goes here?
}

}

3 个答案:

答案 0 :(得分:3)

检查this library,为您处理所有事情。

答案 1 :(得分:1)

我遇到了类似的问题,并为此找到了一个很好的教程。

这是:http://blogingtutorials.blogspot.co.uk/2010/11/android-listview-header-two-or-more-in.html

希望有所帮助:)

答案 2 :(得分:0)

this code will help you

LayoutInflater inflater = this.getLayoutInflater();
        LinearLayout listFooterView = (LinearLayout)inflater.inflate(
                com.demo.list.R.layout.footerview, null);

        LinearLayout listHeaderView = (LinearLayout)inflater.inflate(
                com.demo.list.R.layout.headerview, null);
        ListView lv = getListView();
        lv.setTextFilterEnabled(false);
        lv.addFooterView(listFooterView);
        lv.addHeaderView(listHeaderView);

here headerview and footerview is different layout in which i am showing buttons with listview.