之前在使用iOS时,看到过一种分组的View,每一组都有一个Header,在上下滑动的时候,会有一个悬浮的Header,这种体验觉得很不错,请看下图:
上图中标红的1,2,3,4四张图中,当向上滑动时,仔细观察灰色条的Header变化,当第二组向上滑动时,会把第一组的悬浮Header挤上去。
这种效果在Android是没有的,iOS的SDK就自带这种效果。这篇文章就介绍如何在Android实现这种效果。
/** * Adapter interface. The list adapter must implement this interface. */ public interface PinnedHeaderAdapter { /** * Pinned header state: don't show the header. */ public static final int PINNED_HEADER_GONE = 0; /** * Pinned header state: show the header at the top of the list. */ public static final int PINNED_HEADER_VISIBLE = 1; /** * Pinned header state: show the header. If the header extends beyond * the bottom of the first shown element, push it up and clip. */ public static final int PINNED_HEADER_PUSHED_UP = 2; /** * Computes the desired state of the pinned header for the given * position of the first visible list item. Allowed return values are * {@link #PINNED_HEADER_GONE}, {@link #PINNED_HEADER_VISIBLE} or * {@link #PINNED_HEADER_PUSHED_UP}. */ int getPinnedHeaderState(int position); /** * Configures the pinned header view to match the first visible list item. * * @param header pinned header view. * @param position position of the first visible list item. * @param alpha fading of the header view, between 0 and 255. */ void configurePinnedHeader(View header, int position, int alpha); }1.2、如何绘制Header View
@Override protected void dispatchDraw(Canvas canvas) { super.dispatchDraw(canvas); if (mHeaderViewVisible) { drawChild(canvas, mHeaderView, getDrawingTime()); } }1.3、配置Header View
public void configureHeaderView(int position) { if (mHeaderView == null || null == mAdapter) { return; } int state = mAdapter.getPinnedHeaderState(position); switch (state) { case PinnedHeaderAdapter.PINNED_HEADER_GONE: { mHeaderViewVisible = false; break; } case PinnedHeaderAdapter.PINNED_HEADER_VISIBLE: { mAdapter.configurePinnedHeader(mHeaderView, position, MAX_ALPHA); if (mHeaderView.getTop() != 0) { mHeaderView.layout(0, 0, mHeaderViewWidth, mHeaderViewHeight); } mHeaderViewVisible = true; break; } case PinnedHeaderAdapter.PINNED_HEADER_PUSHED_UP: { View firstView = getChildAt(0); int bottom = firstView.getBottom(); // int itemHeight = firstView.getHeight(); int headerHeight = mHeaderView.getHeight(); int y; int alpha; if (bottom < headerHeight) { y = (bottom - headerHeight); alpha = MAX_ALPHA * (headerHeight + y) / headerHeight; } else { y = 0; alpha = MAX_ALPHA; } mAdapter.configurePinnedHeader(mHeaderView, position, alpha); if (mHeaderView.getTop() != y) { mHeaderView.layout(0, y, mHeaderViewWidth, mHeaderViewHeight + y); } mHeaderViewVisible = true; break; } } }1.4、onLayout和onMeasure
@Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); if (mHeaderView != null) { measureChild(mHeaderView, widthMeasureSpec, heightMeasureSpec); mHeaderViewWidth = mHeaderView.getMeasuredWidth(); mHeaderViewHeight = mHeaderView.getMeasuredHeight(); } } @Override protected void onLayout(boolean changed, int left, int top, int right, int bottom) { super.onLayout(changed, left, top, right, bottom); if (mHeaderView != null) { mHeaderView.layout(0, 0, mHeaderViewWidth, mHeaderViewHeight); configureHeaderView(getFirstVisiblePosition()); } }好了,到这里,悬浮Header View就完了,各位可能看不到完整的代码,只要明白这几个核心的方法,自己写出来,也差不多了。
private class ListViewAdapter extends BaseAdapter implements PinnedHeaderAdapter { private ArrayList<Contact> mDatas; private static final int TYPE_CATEGORY_ITEM = 0; private static final int TYPE_ITEM = 1; public ListViewAdapter(ArrayList<Contact> datas) { mDatas = datas; } @Override public boolean areAllItemsEnabled() { return false; } @Override public boolean isEnabled(int position) { // 异常情况处理 if (null == mDatas || position < 0|| position > getCount()) { return true; } Contact item = mDatas.get(position); if (item.isSection) { return false; } return true; } @Override public int getCount() { return mDatas.size(); } @Override public int getItemViewType(int position) { // 异常情况处理 if (null == mDatas || position < 0|| position > getCount()) { return TYPE_ITEM; } Contact item = mDatas.get(position); if (item.isSection) { return TYPE_CATEGORY_ITEM; } return TYPE_ITEM; } @Override public int getViewTypeCount() { return 2; } @Override public Object getItem(int position) { return (position >= 0 && position < mDatas.size()) ? mDatas.get(position) : 0; } @Override public long getItemId(int position) { return 0; } @Override public View getView(int position, View convertView, ViewGroup parent) { int itemViewType = getItemViewType(position); Contact data = (Contact) getItem(position); TextView itemView; switch (itemViewType) { case TYPE_ITEM: if (null == convertView) { itemView = new TextView(SectionListView.this); itemView.setLayoutParams(new AbsListView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, mItemHeight)); itemView.setTextSize(16); itemView.setPadding(10, 0, 0, 0); itemView.setGravity(Gravity.CENTER_VERTICAL); //itemView.setBackgroundColor(Color.argb(255, 20, 20, 20)); convertView = itemView; } itemView = (TextView) convertView; itemView.setText(data.toString()); break; case TYPE_CATEGORY_ITEM: if (null == convertView) { convertView = getHeaderView(); } itemView = (TextView) convertView; itemView.setText(data.toString()); break; } return convertView; } @Override public int getPinnedHeaderState(int position) { if (position < 0) { return PINNED_HEADER_GONE; } Contact item = (Contact) getItem(position); Contact itemNext = (Contact) getItem(position + 1); boolean isSection = item.isSection; boolean isNextSection = (null != itemNext) ? itemNext.isSection : false; if (!isSection && isNextSection) { return PINNED_HEADER_PUSHED_UP; } return PINNED_HEADER_VISIBLE; } @Override public void configurePinnedHeader(View header, int position, int alpha) { Contact item = (Contact) getItem(position); if (null != item) { if (header instanceof TextView) { ((TextView) header).setText(item.sectionStr); } } } }
public class Contact { int id; String name; String pinyin; String sortLetter = "#"; String sectionStr; String phoneNumber; boolean isSection; static CharacterParser sParser = CharacterParser.getInstance(); Contact() { } Contact(int id, String name) { this.id = id; this.name = name; this.pinyin = sParser.getSpelling(name); if (!TextUtils.isEmpty(pinyin)) { String sortString = this.pinyin.substring(0, 1).toUpperCase(); if (sortString.matches("[A-Z]")) { this.sortLetter = sortString.toUpperCase(); } else { this.sortLetter = "#"; } } } @Override public String toString() { if (isSection) { return name; } else { //return name + " (" + sortLetter + ", " + pinyin + ")"; return name + " (" + phoneNumber + ")"; } } }
package com.lee.sdk.test.section; import java.util.ArrayList; import android.graphics.Color; import android.os.Bundle; import android.view.Gravity; import android.view.View; import android.view.ViewGroup; import android.widget.AbsListView; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.BaseAdapter; import android.widget.TextView; import android.widget.Toast; import com.lee.sdk.test.GABaseActivity; import com.lee.sdk.test.R; import com.lee.sdk.widget.PinnedHeaderListView; import com.lee.sdk.widget.PinnedHeaderListView.PinnedHeaderAdapter; public class SectionListView extends GABaseActivity { private int mItemHeight = 55; private int mSecHeight = 25; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); float density = getResources().getDisplayMetrics().density; mItemHeight = (int) (density * mItemHeight); mSecHeight = (int) (density * mSecHeight); PinnedHeaderListView mListView = new PinnedHeaderListView(this); mListView.setAdapter(new ListViewAdapter(ContactLoader.getInstance().getContacts(this))); mListView.setPinnedHeaderView(getHeaderView()); mListView.setBackgroundColor(Color.argb(255, 20, 20, 20)); mListView.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { ListViewAdapter adapter = ((ListViewAdapter) parent.getAdapter()); Contact data = (Contact) adapter.getItem(position); Toast.makeText(SectionListView.this, data.toString(), Toast.LENGTH_SHORT).show(); } }); setContentView(mListView); } private View getHeaderView() { TextView itemView = new TextView(SectionListView.this); itemView.setLayoutParams(new AbsListView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, mSecHeight)); itemView.setGravity(Gravity.CENTER_VERTICAL); itemView.setBackgroundColor(Color.WHITE); itemView.setTextSize(20); itemView.setTextColor(Color.GRAY); itemView.setBackgroundResource(R.drawable.section_listview_header_bg); itemView.setPadding(10, 0, 0, itemView.getPaddingBottom()); return itemView; } private class ListViewAdapter extends BaseAdapter implements PinnedHeaderAdapter { private ArrayList<Contact> mDatas; private static final int TYPE_CATEGORY_ITEM = 0; private static final int TYPE_ITEM = 1; public ListViewAdapter(ArrayList<Contact> datas) { mDatas = datas; } @Override public boolean areAllItemsEnabled() { return false; } @Override public boolean isEnabled(int position) { // 异常情况处理 if (null == mDatas || position < 0|| position > getCount()) { return true; } Contact item = mDatas.get(position); if (item.isSection) { return false; } return true; } @Override public int getCount() { return mDatas.size(); } @Override public int getItemViewType(int position) { // 异常情况处理 if (null == mDatas || position < 0|| position > getCount()) { return TYPE_ITEM; } Contact item = mDatas.get(position); if (item.isSection) { return TYPE_CATEGORY_ITEM; } return TYPE_ITEM; } @Override public int getViewTypeCount() { return 2; } @Override public Object getItem(int position) { return (position >= 0 && position < mDatas.size()) ? mDatas.get(position) : 0; } @Override public long getItemId(int position) { return 0; } @Override public View getView(int position, View convertView, ViewGroup parent) { int itemViewType = getItemViewType(position); Contact data = (Contact) getItem(position); TextView itemView; switch (itemViewType) { case TYPE_ITEM: if (null == convertView) { itemView = new TextView(SectionListView.this); itemView.setLayoutParams(new AbsListView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, mItemHeight)); itemView.setTextSize(16); itemView.setPadding(10, 0, 0, 0); itemView.setGravity(Gravity.CENTER_VERTICAL); //itemView.setBackgroundColor(Color.argb(255, 20, 20, 20)); convertView = itemView; } itemView = (TextView) convertView; itemView.setText(data.toString()); break; case TYPE_CATEGORY_ITEM: if (null == convertView) { convertView = getHeaderView(); } itemView = (TextView) convertView; itemView.setText(data.toString()); break; } return convertView; } @Override public int getPinnedHeaderState(int position) { if (position < 0) { return PINNED_HEADER_GONE; } Contact item = (Contact) getItem(position); Contact itemNext = (Contact) getItem(position + 1); boolean isSection = item.isSection; boolean isNextSection = (null != itemNext) ? itemNext.isSection : false; if (!isSection && isNextSection) { return PINNED_HEADER_PUSHED_UP; } return PINNED_HEADER_VISIBLE; } @Override public void configurePinnedHeader(View header, int position, int alpha) { Contact item = (Contact) getItem(position); if (null != item) { if (header instanceof TextView) { ((TextView) header).setText(item.sectionStr); } } } } }