ListView添加headview

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ListView添加headview相关的知识,希望对你有一定的参考价值。

1、头部view:设置view的点击时间,用于屏蔽listview的item的点击事件

		View headerView = getLayoutInflater().inflate(R.layout.listhead, null);
		Button btn = (Button) headerView.findViewById(R.id.button1);
		btn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				Toast.makeText(MainActivity.this, bn, Toast.LENGTH_SHORT)
						.show();
			}
		});

 

2、添加头部view:必须放在setAdapter前面不然会报错

		lv.addHeaderView(headerView);

 

3、listview填充假数据

		List> list = new ArrayList>();
		HashMap map = new HashMap();
		map.put(tv, tv);
		list.add(map);
		list.add(map);
		list.add(map);
		list.add(map);
		list.add(map);
		list.add(map);
		SimpleAdapter sa = new SimpleAdapter(this, list, R.layout.item_lv,
				new String[] { tv }, new int[] { R.id.tv });

 

4、设置适配器:如果listview不设置适配器,headview也不会显示的

		lv.setAdapter(sa);

 

5、设置item点击事件

		lv.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView arg0, View arg1,
					int position, long id) {
				
				//position 如果有headview  position =0 的第一个为headview
				Toast.makeText(MainActivity.this, position: + position,
						Toast.LENGTH_SHORT).show();
				
				//id:如果有headview或者footview  则这两个view的id为-1
				if (id == -1) {
					Toast.makeText(MainActivity.this, head: + id,
							Toast.LENGTH_SHORT).show();
				} else {
					Toast.makeText(MainActivity.this, id: + id,
							Toast.LENGTH_SHORT).show();
				}

			}
		});


main.java全部代码:

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;

public class MainActivity extends Activity {
	ListView lv;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		lv = (ListView) findViewById(R.id.listView1);
		
		// 头部view:设置view的点击时间,用于屏蔽listview的item的点击事件
		View headerView = getLayoutInflater().inflate(R.layout.listhead, null);
		Button btn = (Button) headerView.findViewById(R.id.button1);
		btn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				Toast.makeText(MainActivity.this, bn, Toast.LENGTH_SHORT)
						.show();
			}
		});
		
		// 添加头部view:必须放在adapter前面不然会报错
		lv.addHeaderView(headerView);
		
		// 假数据
		List> list = new ArrayList>();
		HashMap map = new HashMap();
		map.put(tv, tv);
		list.add(map);
		list.add(map);
		list.add(map);
		list.add(map);
		list.add(map);
		list.add(map);
		SimpleAdapter sa = new SimpleAdapter(this, list, R.layout.item_lv,
				new String[] { tv }, new int[] { R.id.tv });
		
		// 设置适配器:如果listview不设置适配器,headview也不会显示的
		lv.setAdapter(sa);
		
		// 设置item点击事件:
		lv.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView arg0, View arg1,
					int position, long id) {
				
				//position 如果有headview  position =0 的第一个为headview
				Toast.makeText(MainActivity.this, position: + position,
						Toast.LENGTH_SHORT).show();
				
				//id:如果有headview或者footview  则这两个view的id为-1
				if (id == -1) {
					Toast.makeText(MainActivity.this, head: + id,
							Toast.LENGTH_SHORT).show();
				} else {
					Toast.makeText(MainActivity.this, id: + id,
							Toast.LENGTH_SHORT).show();
				}

			}
		});
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

}

以上是关于ListView添加headview的主要内容,如果未能解决你的问题,请参考以下文章

android listview headview过长显示不全怎么办

在片段中添加 ListView

Android LinearLayout实现下拉刷新

添加数据库后填充listview

在 ListView 中动态添加 LinearLayout

片段中 ListView 的 setOnItemClickListener