系统自带的listview可以实现addHeaderView()方法,
代码如下:
View headerView = LayoutInflater.from(getActivity()).inflate(R.layout.layout_header , null);
im_header = (ImageView) headerView.findViewById(R.id.header_img);
im_header.setOnClickListener(this);
listView.addHeaderView(headerView);
在布局中给ListView外层嵌套一个ScrollView,并重写ListView就可以实现,之前写过文章 http://blog.csdn.net/u014732430/article/details/52240015
但其实系统在ListView中给过解决办法:
下面上代码:
主布局:
就是一个简单的列表显示
下边是数据处理,List
我有新建了一个List
private List list_type;
private List list;
public void setListData(){
ListBean bean =new ListBean();
list_type = new ArrayList<>();
list_type.add(0);
for (int i = 0; i < 80; i++){
bean = new ListBean();
bean.setType(1);
bean.setName("New"+(i));
list.add(bean);
list_type.add(1);
}
}
下面重点是Adapter , 多重写以下两个方法,在getView也要加判断:
//返回postion的类别
@Override
public int getItemViewType(int position) {
return list_type.get(position);
}
//返回一共有几种类型
@Override
public int getViewTypeCount() {
return 2;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
ViewHolderHeadr head_holder = null;
//通过区分itemviewtype判断显示类别
if (getItemViewType(position) == 0) {
if (convertView == null){
convertView = LayoutInflater.from(context).inflate(R.layout.layout_header, null);
head_holder = new ViewHolderHeadr();
head_holder.im_header = (ImageView) convertView.findViewById(R.id.header_img);
convertView.setTag(head_holder);
}else{
head_holder = (ViewHolderHeadr) convertView.getTag();
}
}else{
if (convertView == null) {
convertView = LayoutInflater.from(context).inflate(R.layout.list_item, null);
holder = new ViewHolder();
holder.item_im_photo = (ImageView) convertView.findViewById(R.id.item_im_photo);
holder.item_tv_name = (TextView) convertView.findViewById(R.id.item_tv_name);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.item_tv_name.setText(list.get(position).getName());
}
return convertView;
}
class ViewHolder{
ImageView item_im_photo;
TextView item_tv_name;
}
class ViewHolderHeadr{
ImageView im_header;
}
在Activity什么也不用做
最近又学到了另一种方法:
直接上代码
/**
* 设置Header
*/
AbsListView.LayoutParams layoutParams = new AbsListView.LayoutParams(
AbsListView.LayoutParams.MATCH_PARENT, AbsListView.LayoutParams.WRAP_CONTENT);
View header = getLayoutInflater().inflate(R.layout.new_view_titlegalleryview, listView, false);//listview就是PullToRefreshListView
header.setLayoutParams(layoutParams);
ListView lv = listView.getRefreshableView();
lv.addHeaderView(header);
之后header里的空间就用header.来操作就行了