一、主类
package com.example.listviewdemo;
import java.util.ArrayList;
import android.app.Activity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class MainActivity extends Activity {
private ListView listView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = (ListView) this.findViewById(R.id.listview);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, getData());
listView.setAdapter(adapter);
}
private ArrayList<String> getData() {
ArrayList<String> data = new ArrayList<String>();
data.add("item1");
data.add("item2");
data.add("item3");
data.add("item4");
data.add("item5");
data.add("item6");
data.add("item7");
data.add("item8");
data.add("item9");
data.add("item10");
return data;
}
}
二、布局1
场景
布局文件
<ScrollView 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:orientation="vertical"
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=".MainActivity" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="button" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="button" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="button" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="button" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="button" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="button" />
<ListView
android:id="@+id/listview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:divider="@android:drawable/divider_horizontal_textfield" />
</LinearLayout>
</ScrollView>
三、布局2
场景
布局文件
<LinearLayout 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:orientation="vertical"
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=".MainActivity" >
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="button" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="button" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="button" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="button" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="button" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="button" />
<ListView
android:id="@+id/listview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:divider="@android:drawable/divider_horizontal_textfield" />
</LinearLayout>
这种场景适用于做标题栏
布局文件
<LinearLayout 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:orientation="vertical"
tools:context=".MainActivity" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#BEFEBE"
android:orientation="horizontal" >
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/arrow_drawer" />
</LinearLayout>
<ListView
android:id="@+id/listview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:divider="@android:drawable/divider_horizontal_textfield" />
</LinearLayout>
头部和尾部标题栏
布局文件
<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:orientation="vertical"
tools:context=".MainActivity" >
<LinearLayout
android:id="@+id/layout01"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:background="#BEFEBE"
android:orientation="horizontal" >
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/arrow_drawer" />
</LinearLayout>
<LinearLayout
android:id="@+id/layout02"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="#BEFEBE"
android:orientation="horizontal" >
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/arrow_drawer" />
</LinearLayout>
<ListView
android:id="@+id/listview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@id/layout02"
android:layout_below="@id/layout01"
android:divider="@android:drawable/divider_horizontal_textfield" />
</RelativeLayout>
四、布局3
场景
从图片中看出有2个item1,其实第一个item1是addHeaderView中的ListView中的第一条,由于listView.addHeaderView(view);但是因为这个view必须是一个固定大小的一个View才行,由于ListView含有很多View,所以只显示第一条.
主类
package com.example.listviewdemo;
import java.util.ArrayList;
import android.app.Activity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class MainActivity extends Activity {
private ListView listView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = (ListView) this.findViewById(R.id.listview);
View view = LayoutInflater.from(this).inflate(R.layout.headerview01, null);
listView.addHeaderView(view);//addHeaderView方法必须在setAdapter之前调用
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, getData());
listView.setAdapter(adapter);
ListView header_list = (ListView) view.findViewById(R.id.header_listView);
header_list.setAdapter(adapter);
}
private ArrayList<String> getData() {
ArrayList<String> data = new ArrayList<String>();
data.add("item1");
data.add("item2");
data.add("item3");
data.add("item4");
data.add("item5");
data.add("item6");
data.add("item7");
data.add("item8");
data.add("item9");
data.add("item10");
return data;
}
}
activity_main.xml
<LinearLayout 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"
android:orientation="vertical"
tools:context=".MainActivity" >
<ListView
android:id="@+id/listview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:divider="@android:drawable/divider_horizontal_textfield" />
</LinearLayout>
headerview01.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ListView
android:id="@+id/header_listView"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
//这里,即使将LinearLayout的高设置成固定的数值,结果还是一样,只显示第一条。
如果想要在ListView布局之上的东西或者之下的东西可以和ListView一起滚动,可以参考这种方案,但是注意HeadView和FooterView中不能含有ListView,结果如下: