标签:
最近在Demo中使用到了stickyListHeaders框架实现悬停效果
比较好用
在xml中
<se.emilsjolander.stickylistheaders.StickyListHeadersListView
android:id="@+id/list_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/white" />
和listView的配置方法差不多
在Activity中的使用
mAdapter = new MyStivkyAdapter(getActivity(), mGroupinfo);
lv.setAdapter(mAdapter);
我这是在fragment中使用的在actiity中使用才差不多
在适配器中的代码
public class MyStivkyAdapter extends BaseAdapter implements
StickyListHeadersAdapter {
private List<Group> groups;
private LayoutInflater mInflater;
private ViewHolder viewHolder;
HeaderViewHolder hvh;
public MyStivkyAdapter(Context context, List<Group> list) {
this.groups = list;
mInflater = LayoutInflater.from(context);
}
@Override
public int getCount() {
return groups.size();
}
@Override
public Group getItem(int position) {
return groups.get(position);
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
viewHolder = new ViewHolder();
convertView = mInflater.inflate(R.layout.list_item, null);
viewHolder.ivComplete = (ImageView) convertView
.findViewById(R.id.item_image);
viewHolder.tvTitle = (TextView) convertView
.findViewById(R.id.item_text1);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.ivComplete.setImageResource(R.drawable.ic_fire);
viewHolder.tvTitle.setText(groups.get(position).brandName);
return convertView;
}
@Override
public View getHeaderView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
hvh = new HeaderViewHolder();
convertView = mInflater.inflate(R.layout.item_heander, null);
hvh.tvHeader = (TextView) convertView.findViewById(R.id.text);
convertView.setTag(hvh);
} else {
hvh = (HeaderViewHolder) convertView.getTag();
}
hvh.tvHeader.setText(groups.get(position).brandInitial);
return convertView;
}
@Override
public long getHeaderId(int position) {
return groups.get(position).brandInitial.charAt(0);
}
class ViewHolder {
ImageView ivComplete;
TextView tvTitle;
}
class HeaderViewHolder {
TextView tvHeader;
}
}
其他和ListView的适配器差不多就是实现来一个框架的接口 并重写了2个方法
在
getHeaderView ()方法中是返回添加的头的内容 如一般的排序 A----Z来实现排序这时如果对getHeaderId不处理会默认在每个条目上添加一个头,
在getHeaderId返回值 猜测是用来判断两个条目的头是否相同,相同时第二个就不会添加,这时将实现将一个简单的悬浮了
标签:
原文地址:http://www.cnblogs.com/chenli123/p/4805902.html