Ввод описания hackbod в код для вас, так как его ответ создал больше вопросов, прежде чем ответы пришли. Иногда я просто хочу рыбу. Мне не всегда нужно знать, как устроена сеть ...
Для начала создайте макет, который выглядит следующим образом:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/myListViewWithHeader"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/header"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="6dp"
android:textStyle="bold"
android:textSize="20sp" />
<ListView
android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:drawSelectorOnTop="false" />
<TextView
android:id="@android:id/empty"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
Затем в вашем методе onCreateView вы делаете это:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.myListViewWithHeader, null);
return view;
}
Заголовок теперь может быть заполнен следующим образом:
// get the header view
TextView headerView = (TextView) getView().findViewById(R.id.header);
headerView.setText("Header text goes here");
Обратите внимание, что мой заголовок является TextView, но при желании его можно заменить другим представлением. В этом случае вам нужно будет выполнить getView (). FindViewById (R.id.xxxxx) для каждого представления внутри заголовка, с которым вы хотите работать