转载请注明出处:ListView头尾简介Header/Footer_Mr_Leixiansheng的博客-CSDN博客
内容:以前只会用ListView显示数据,最近才知道还有头尾一说。学会了头尾可以很方便的设置一些需要的布局
步骤:
1、布局添加ListView
2、添加头尾的item,xml文件布局
3、调用listview的addHeaderView(mHeader)和addFooterView(mFooter)添加上头尾
4、listView添加适配器和设置按键监听
代码如下:
1、主界面布局(listView)
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.hello.listviewhead.MainActivity">
<ListView
android:id="@+id/list_view"
android:layout_width="match_parent"
android:layout_height="match_parent">
</ListView>
</android.support.constraint.ConstraintLayout>
2、头尾item布局
<?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">
<ImageView
android:id="@+id/image_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@mipmap/ic_launcher"/>
<ImageView
android:id="@+id/image_view2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@mipmap/ic_launcher"/>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/image_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@mipmap/ic_launcher"/>
</android.support.constraint.ConstraintLayout>
3、实现代码
package com.example.hello.listviewhead;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity implements AdapterView.OnItemClickListener{
private ListView mListView;
private View mHeader;
private View mFooter;
private String[] mStrings = {"小明", "小红", "小黄", "小张"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mListView = (ListView) findViewById(R.id.list_view);
mHeader = View.inflate(this, R.layout.item_head, null);
mFooter = View.inflate(this, R.layout.item_footer, null);
//添加头
mListView.addHeaderView(mHeader);
//添加尾
mListView.addFooterView(mFooter);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, mStrings);
mListView.setAdapter(adapter);
mListView.setOnItemClickListener(this);
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(this, "position:"+position, Toast.LENGTH_SHORT).show();
}
}