实现上述功能,直接将PinnedSectionListView作为一个普通的ListView即可,但重点是在构造PinnedSectionListView适配器Adapter时,每一个Item需要小心设置View Type。
实现分组标签悬停滑入滑出效果,只需将PinnedSectionListView.java倒入自己的项目即可:
PinnedSectionListView.java代码如下:
/* * Copyright (C) 2013 Sergej Shafarenka, halfbit.de * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file kt in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.hb.views; import zhangphil.contacts.BuildConfig; import android.content.Context; import android.database.DataSetObserver; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.PointF; import android.graphics.Rect; import android.graphics.drawable.GradientDrawable; import android.graphics.drawable.GradientDrawable.Orientation; import android.os.Parcelable; import android.util.AttributeSet; import android.view.MotionEvent; import android.view.SoundEffectConstants; import android.view.View; import android.view.ViewConfiguration; import android.view.accessibility.AccessibilityEvent; import android.widget.AbsListView; import android.widget.HeaderViewListAdapter; import android.widget.ListAdapter; import android.widget.ListView; import android.widget.SectionIndexer; /** * ListView, which is capable to pin section views at its top while the rest is * still scrolled. */ public class PinnedSectionListView extends ListView { // -- inner classes /** * List adapter to be implemented for being used with PinnedSectionListView * adapter. */ public static interface PinnedSectionListAdapter extends ListAdapter { /** * This method shall return 'true' if views of given type has to be * pinned. */ boolean isItemViewTypePinned(int viewType); } /** Wrapper class for pinned section view and its position in the list. */ static class PinnedSection { public View view; public int position; public long id; } // -- class fields // fields used for handling touch events private final Rect mTouchRect = new Rect(); private final PointF mTouchPoint = new PointF(); private int mTouchSlop; private View mTouchTarget; private MotionEvent mDownEvent; // fields used for drawing shadow under a pinned section private GradientDrawable mShadowDrawable; private int mSectionsDistanceY; private int mShadowHeight; /** Delegating listener, can be null. */ OnScrollListener mDelegateOnScrollListener; /** Shadow for being recycled, can be null. */ PinnedSection mRecycleSection; /** shadow instance with a pinned view, can be null. */ PinnedSection mPinnedSection; /** * Pinned view Y-translation. We use it to stick pinned view to the next * section. */ int mTranslateY; /** Scroll listener which does the magic */ private final OnScrollListener mOnScrollListener = new OnScrollListener() { @Override public void onScrollStateChanged(AbsListView view, int scrollState) { if (mDelegateOnScrollListener != null) { // delegate mDelegateOnScrollListener.onScrollStateChanged(view, scrollState); } } @Override public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) { if (mDelegateOnScrollListener != null) { // delegate mDelegateOnScrollListener.onScroll(view, firstVisibleItem, visibleItemCount, totalItemCount); } // get expected adapter or fail fast ListAdapter adapter = getAdapter(); if (adapter == null || visibleItemCount == 0) return; // nothing to do final boolean isFirstVisibleItemSection = isItemViewTypePinned( adapter, adapter.getItemViewType(firstVisibleItem)); if (isFirstVisibleItemSection) { View sectionView = getChildAt(0); if (sectionView.getTop() == getPaddingTop()) { // view sticks to // the top, no // need for // pinned shadow destroyPinnedShadow(); } else { // section doesn't stick to the top, make sure we have // a pinned shadow ensureShadowForPosition(firstVisibleItem, firstVisibleItem, visibleItemCount); } } else { // section is not at the first visible position int sectionPosition = findCurrentSectionPosition(firstVisibleItem); if (sectionPosition > -1) { // we have section position ensureShadowForPosition(sectionPosition, firstVisibleItem, visibleItemCount); } else { // there is no section for the first visible item, // destroy shadow destroyPinnedShadow(); } } }; }; /** Default change observer. */ private final DataSetObserver mDataSetObserver = new DataSetObserver() { @Override public void onChanged() { recreatePinnedShadow(); }; @Override public void onInvalidated() { recreatePinnedShadow(); } }; // -- constructors public PinnedSectionListView(Context context, AttributeSet attrs) { super(context, attrs); initView(); } public PinnedSectionListView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); initView(); } private void initView() { setOnScrollListener(mOnScrollListener); mTouchSlop = ViewConfiguration.get(getContext()).getScaledTouchSlop(); initShadow(true); } // -- public API methods public void setShadowVisible(boolean visible) { initShadow(visible); if (mPinnedSection != null) { View v = mPinnedSection.view; invalidate(v.getLeft(), v.getTop(), v.getRight(), v.getBottom() + mShadowHeight); } } // -- pinned section drawing methods public void initShadow(boolean visible) { if (visible) { if (mShadowDrawable == null) { mShadowDrawable = new GradientDrawable(Orientation.TOP_BOTTOM, new int[] { Color.parseColor("#ffa0a0a0"), Color.parseColor("#50a0a0a0"), Color.parseColor("#00a0a0a0") }); mShadowHeight = (int) (8 * getResources().getDisplayMetrics().density); } } else { if (mShadowDrawable != null) { mShadowDrawable = null; mShadowHeight = 0; } } } /** Create shadow wrapper with a pinned view for a view at given position */ void createPinnedShadow(int position) { // try to recycle shadow PinnedSection pinnedShadow = mRecycleSection; mRecycleSection = null; // create new shadow, if needed if (pinnedShadow == null) pinnedShadow = new PinnedSection(); // request new view using recycled view, if such View pinnedView = getAdapter().getView(position, pinnedShadow.view, PinnedSectionListView.this); // read layout parameters LayoutParams layoutParams = (LayoutParams) pinnedView.getLayoutParams(); if (layoutParams == null) { layoutParams = (LayoutParams) generateDefaultLayoutParams(); pinnedView.setLayoutParams(layoutParams); } int heightMode = MeasureSpec.getMode(layoutParams.height); int heightSize = MeasureSpec.getSize(layoutParams.height); if (heightMode == MeasureSpec.UNSPECIFIED) heightMode = MeasureSpec.EXACTLY; int maxHeight = getHeight() - getListPaddingTop() - getListPaddingBottom(); if (heightSize > maxHeight) heightSize = maxHeight; // measure & layout int ws = MeasureSpec.makeMeasureSpec(getWidth() - getListPaddingLeft() - getListPaddingRight(), MeasureSpec.EXACTLY); int hs = MeasureSpec.makeMeasureSpec(heightSize, heightMode); pinnedView.measure(ws, hs); pinnedView.layout(0, 0, pinnedView.getMeasuredWidth(), pinnedView.getMeasuredHeight()); mTranslateY = 0; // initialize pinned shadow pinnedShadow.view = pinnedView; pinnedShadow.position = position; pinnedShadow.id = getAdapter().getItemId(position); // store pinned shadow mPinnedSection = pinnedShadow; } /** Destroy shadow wrapper for currently pinned view */ void destroyPinnedShadow() { if (mPinnedSection != null) { // keep shadow for being recycled later mRecycleSection = mPinnedSection; mPinnedSection = null; } } /** Makes sure we have an actual pinned shadow for given position. */ void ensureShadowForPosition(int sectionPosition, int firstVisibleItem, int visibleItemCount) { if (visibleItemCount < 2) { // no need for creating shadow at all, we // have a single visible item destroyPinnedShadow(); return; } if (mPinnedSection != null && mPinnedSection.position != sectionPosition) { // invalidate // shadow, // if // required destroyPinnedShadow(); } if (mPinnedSection == null) { // create shadow, if empty createPinnedShadow(sectionPosition); } // align shadow according to next section position, if needed int nextPosition = sectionPosition + 1; if (nextPosition < getCount()) { int nextSectionPosition = findFirstVisibleSectionPosition( nextPosition, visibleItemCount - (nextPosition - firstVisibleItem)); if (nextSectionPosition > -1) { View nextSectionView = getChildAt(nextSectionPosition - firstVisibleItem); final int bottom = mPinnedSection.view.getBottom() + getPaddingTop(); mSectionsDistanceY = nextSectionView.getTop() - bottom; if (mSectionsDistanceY < 0) { // next section overlaps pinned shadow, move it up mTranslateY = mSectionsDistanceY; } else { // next section does not overlap with pinned, stick to top mTranslateY = 0; } } else { // no other sections are visible, stick to top mTranslateY = 0; mSectionsDistanceY = Integer.MAX_VALUE; } } } int findFirstVisibleSectionPosition(int firstVisibleItem, int visibleItemCount) { ListAdapter adapter = getAdapter(); int adapterDataCount = adapter.getCount(); if (getLastVisiblePosition() >= adapterDataCount) return -1; // dataset has changed, no candidate if (firstVisibleItem + visibleItemCount >= adapterDataCount) {// added // to // prevent // index // Outofbound // (in // case) visibleItemCount = adapterDataCount - firstVisibleItem; } for (int childIndex = 0; childIndex < visibleItemCount; childIndex++) { int position = firstVisibleItem + childIndex; int viewType = adapter.getItemViewType(position); if (isItemViewTypePinned(adapter, viewType)) return position; } return -1; } int findCurrentSectionPosition(int fromPosition) { ListAdapter adapter = getAdapter(); if (fromPosition >= adapter.getCount()) return -1; // dataset has changed, no candidate if (adapter instanceof SectionIndexer) { // try fast way by asking section indexer SectionIndexer indexer = (SectionIndexer) adapter; int sectionPosition = indexer.getSectionForPosition(fromPosition); int itemPosition = indexer.getPositionForSection(sectionPosition); int typeView = adapter.getItemViewType(itemPosition); if (isItemViewTypePinned(adapter, typeView)) { return itemPosition; } // else, no luck } // try slow way by looking through to the next section item above for (int position = fromPosition; position >= 0; position--) { int viewType = adapter.getItemViewType(position); if (isItemViewTypePinned(adapter, viewType)) return position; } return -1; // no candidate found } void recreatePinnedShadow() { destroyPinnedShadow(); ListAdapter adapter = getAdapter(); if (adapter != null && adapter.getCount() > 0) { int firstVisiblePosition = getFirstVisiblePosition(); int sectionPosition = findCurrentSectionPosition(firstVisiblePosition); if (sectionPosition == -1) return; // no views to pin, exit ensureShadowForPosition(sectionPosition, firstVisiblePosition, getLastVisiblePosition() - firstVisiblePosition); } } @Override public void setOnScrollListener(OnScrollListener listener) { if (listener == mOnScrollListener) { super.setOnScrollListener(listener); } else { mDelegateOnScrollListener = listener; } } @Override public void onRestoreInstanceState(Parcelable state) { super.onRestoreInstanceState(state); post(new Runnable() { @Override public void run() { // restore pinned view after configuration // change recreatePinnedShadow(); } }); } @Override public void setAdapter(ListAdapter adapter) { // assert adapter in debug mode if (BuildConfig.DEBUG && adapter != null) { if (!(adapter instanceof PinnedSectionListAdapter)) throw new IllegalArgumentException( "Does your adapter implement PinnedSectionListAdapter?"); if (adapter.getViewTypeCount() < 2) throw new IllegalArgumentException( "Does your adapter handle at least two types" + " of views in getViewTypeCount() method: items and sections?"); } // unregister observer at old adapter and register on new one ListAdapter oldAdapter = getAdapter(); if (oldAdapter != null) oldAdapter.unregisterDataSetObserver(mDataSetObserver); if (adapter != null) adapter.registerDataSetObserver(mDataSetObserver); // destroy pinned shadow, if new adapter is not same as old one if (oldAdapter != adapter) destroyPinnedShadow(); super.setAdapter(adapter); } @Override protected void onLayout(boolean changed, int l, int t, int r, int b) { super.onLayout(changed, l, t, r, b); if (mPinnedSection != null) { int parentWidth = r - l - getPaddingLeft() - getPaddingRight(); int shadowWidth = mPinnedSection.view.getWidth(); if (parentWidth != shadowWidth) { recreatePinnedShadow(); } } } @Override protected void dispatchDraw(Canvas canvas) { super.dispatchDraw(canvas); if (mPinnedSection != null) { // prepare variables int pLeft = getListPaddingLeft(); int pTop = getListPaddingTop(); View view = mPinnedSection.view; // draw child canvas.save(); int clipHeight = view.getHeight() + (mShadowDrawable == null ? 0 : Math.min(mShadowHeight, mSectionsDistanceY)); canvas.clipRect(pLeft, pTop, pLeft + view.getWidth(), pTop + clipHeight); canvas.translate(pLeft, pTop + mTranslateY); drawChild(canvas, mPinnedSection.view, getDrawingTime()); if (mShadowDrawable != null && mSectionsDistanceY > 0) { mShadowDrawable.setBounds(mPinnedSection.view.getLeft(), mPinnedSection.view.getBottom(), mPinnedSection.view.getRight(), mPinnedSection.view.getBottom() + mShadowHeight); mShadowDrawable.draw(canvas); } canvas.restore(); } } // -- touch handling methods @Override public boolean dispatchTouchEvent(MotionEvent ev) { final float x = ev.getX(); final float y = ev.getY(); final int action = ev.getAction(); if (action == MotionEvent.ACTION_DOWN && mTouchTarget == null && mPinnedSection != null && isPinnedViewTouched(mPinnedSection.view, x, y)) { // create // touch // target // user touched pinned view mTouchTarget = mPinnedSection.view; mTouchPoint.x = x; mTouchPoint.y = y; // copy down event for eventually be used later mDownEvent = MotionEvent.obtain(ev); } if (mTouchTarget != null) { if (isPinnedViewTouched(mTouchTarget, x, y)) { // forward event to // pinned view mTouchTarget.dispatchTouchEvent(ev); } if (action == MotionEvent.ACTION_UP) { // perform onClick on pinned // view super.dispatchTouchEvent(ev); performPinnedItemClick(); clearTouchTarget(); } else if (action == MotionEvent.ACTION_CANCEL) { // cancel clearTouchTarget(); } else if (action == MotionEvent.ACTION_MOVE) { if (Math.abs(y - mTouchPoint.y) > mTouchSlop) { // cancel sequence on touch target MotionEvent event = MotionEvent.obtain(ev); event.setAction(MotionEvent.ACTION_CANCEL); mTouchTarget.dispatchTouchEvent(event); event.recycle(); // provide correct sequence to super class for further // handling super.dispatchTouchEvent(mDownEvent); super.dispatchTouchEvent(ev); clearTouchTarget(); } } return true; } // call super if this was not our pinned view return super.dispatchTouchEvent(ev); } private boolean isPinnedViewTouched(View view, float x, float y) { view.getHitRect(mTouchRect); // by taping top or bottom padding, the list performs on click on a // border item. // we don't add top padding here to keep behavior consistent. mTouchRect.top += mTranslateY; mTouchRect.bottom += mTranslateY + getPaddingTop(); mTouchRect.left += getPaddingLeft(); mTouchRect.right -= getPaddingRight(); return mTouchRect.contains((int) x, (int) y); } private void clearTouchTarget() { mTouchTarget = null; if (mDownEvent != null) { mDownEvent.recycle(); mDownEvent = null; } } private boolean performPinnedItemClick() { if (mPinnedSection == null) return false; OnItemClickListener listener = getOnItemClickListener(); if (listener != null && getAdapter().isEnabled(mPinnedSection.position)) { View view = mPinnedSection.view; playSoundEffect(SoundEffectConstants.CLICK); if (view != null) { view.sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_CLICKED); } listener.onItemClick(this, view, mPinnedSection.position, mPinnedSection.id); return true; } return false; } public static boolean isItemViewTypePinned(ListAdapter adapter, int viewType) { if (adapter instanceof HeaderViewListAdapter) { adapter = ((HeaderViewListAdapter) adapter).getWrappedAdapter(); } return ((PinnedSectionListAdapter) adapter) .isItemViewTypePinned(viewType); } }
java代码:
package com.example.pinnedsectionlistview; import java.util.ArrayList; import java.util.List; import java.util.zip.Inflater; import com.example.pinnedsectionlistview.PinnedSectionListView.PinnedSectionListAdapter; import android.app.Activity; import android.content.ContentResolver; import android.content.Context; import android.content.Intent; import android.database.Cursor; import android.graphics.Color; import android.net.Uri; import android.os.Bundle; import android.provider.ContactsContract; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.ArrayAdapter; import android.widget.TextView; import android.widget.Toast; public class MainActivity extends Activity { private PinnedSectionListView list_view; private ArrayList<Item> item; private ArrayList<Contact> contacts; private final int VIEW_TYPE_COUNT = 2; private final int GROUP = 0; private final int CHILD = 1; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); contacts = new ArrayList<Contact>(); // 把联系人说装载到contacts中。 readContacts(contacts); item = new ArrayList<Item>(); // 从字母A开始到Z。26个字母,遍历联系人中的首字符是否相等。 // 相等则归入一组。 int A = 'A'; for (int i = 0; i < 26; i++) { int letter = A + i; char c = (char) letter; Item group_item = new Item(); group_item.type = GROUP; group_item.text = c + ""; item.add(group_item); for (int j = 0; j < contacts.size(); j++) { Contact mcontact = contacts.get(j); String frist_name = mcontact.firstLetterOfName(); if (frist_name.equals(group_item.text)) { Item child_item = new Item(); child_item.type = CHILD; child_item.text = mcontact.name + mcontact.getPhoneNumbers(); child_item.contact = mcontact; item.add(child_item); } } } list_view = (PinnedSectionListView) findViewById(R.id.listview); list_view.setAdapter(new MyAdapter(this, android.R.layout.simple_list_item_1, item)); // 增加点击事件,当用户点击ListView的item时候,打电话。 list_view.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { Item mitem = item.get(position); int type = mitem.type; switch (type) { case GROUP: Toast.makeText(getApplicationContext(), "没有号码", Toast.LENGTH_SHORT).show(); break; case CHILD: Contact contact = mitem.contact; String number = contact.phoneNumbers.get(0); // Android拨打电话 Intent intent = new Intent(Intent.ACTION_CALL,Uri.parse("tel:" + number)); startActivity(intent); break; default: break; } } }); } // 用于承载数据块的类。 // 字段分为类型(type)和值(text)。 private class Item { private int type; private String text; private Contact contact; } private class MyAdapter extends ArrayAdapter<Item>implements PinnedSectionListAdapter { private LayoutInflater inflater; private int resource; public MyAdapter(Context context, int textViewResourceId, List<Item> objects) { super(context, textViewResourceId, objects); this.resource = textViewResourceId; inflater = LayoutInflater.from(context); } @Override public int getCount() { // TODO Auto-generated method stub return item.size(); } @Override public int getItemViewType(int position) { Item mItem = getItem(position); return mItem.type; } @Override public Item getItem(int position) { return item.get(position); } @Override public int getViewTypeCount() { return VIEW_TYPE_COUNT; } @Override public View getView(int position, View convertView, ViewGroup parent) { int type = getItemViewType(position); if (convertView == null) convertView = inflater.inflate(resource, null); // 以下是根据不同的类型,加载不同的布局。用于显示不同的数据类型。 // 标签和联系人详情。 switch (type) { case GROUP: TextView group_text = (TextView) convertView.findViewById(android.R.id.text1); group_text.setText(getItem(position).text); group_text.setBackgroundColor(Color.RED); break; case CHILD: TextView child_text = (TextView) convertView.findViewById(android.R.id.text1); child_text.setText(getItem(position).text); child_text.setTextColor(Color.GRAY); break; default: break; } return convertView; } // 假设此方法返回皆为false。那么PinnedSectionListView将退化成为一个基础的ListView. // 只不过退化后的ListView只是一个拥有两个View Type的ListView。 // 从某种角度上讲,此方法对于PinnedSectionListView至关重要,因为返回值true或false, //将直接导致PinnedSectionListView是一个PinnedSectionListView,还是一个普通的ListView。 @Override public boolean isItemViewTypePinned(int viewType) { boolean type = false; switch (viewType) { case GROUP: type = true; break; case CHILD: type = false; break; default: type = false; break; } return type; } } // 读取设备联系人的一般方法。大致流程就是这样,模板化的操作代码。 private void readContacts(ArrayList<Contact> contacts){ Uri uri = Uri.parse("content://com.android.contacts/contacts"); ContentResolver resolver = this.getContentResolver(); // 在这里我们给query传递进去一个SORT_KEY_PRIMARY。 // 告诉ContentResolver获得的结果按照联系人名字的首字母有序排列。 Cursor cursor = resolver.query(uri, null, null, null, android.provider.ContactsContract.Contacts.SORT_KEY_PRIMARY); while(cursor.moveToNext()){ // 联系人ID String id = cursor.getString(cursor .getColumnIndex(android.provider.ContactsContract.Contacts._ID)); // 获得联系人姓名 String name = cursor.getString(cursor.getColumnIndex(android.provider.ContactsContract.Contacts.DISPLAY_NAME)); // Sort Key,读取的联系人按照姓名从 A->Z 排序分组。 String sort_key_primary = cursor.getString(cursor .getColumnIndex(android.provider.ContactsContract.Contacts.SORT_KEY_PRIMARY)); Contact mContact = new Contact(); mContact.id = id; mContact.name = name; mContact.sort_key_primary = sort_key_primary; // 获得联系人手机号码 Cursor phone_cursor = resolver.query( ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID + "=" + id, null, null); // 取得电话号码(可能存在多个号码) // 因为同一个名字下,用户可能存有一个以上的号, // 遍历。 ArrayList<String> phoneNumbers = new ArrayList<String>(); while(phone_cursor.moveToNext()){ String phoneNumber = phone_cursor.getString(phone_cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)); phoneNumbers.add(phoneNumber); } mContact.phoneNumbers = phoneNumbers; contacts.add(mContact); } } }
自定义一个用于装载从联系人数据库中读取到的数据,数据结构化,便于数据操作和访问:
package com.example.pinnedsectionlistview; import java.util.ArrayList; public class Contact { public String id; public String name; public String sort_key_primary; public ArrayList<String> phoneNumbers; // 获得一个联系人名字的首字符。 // 比如一个人的名字叫“安卓”,那么这个人联系人的首字符是:A。 public String firstLetterOfName() { String s = sort_key_primary.charAt(0) + ""; return s.toUpperCase(); } public String getPhoneNumbers() { String phones = " "; for (int i = 0; i < phoneNumbers.size(); i++) { phones += "号码" + i + ":" + phoneNumbers.get(i); } return phones; } }
需要的布局文件xml 如下:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.example.pinnedsectionlistview.MainActivity" > <com.example.pinnedsectionlistview.PinnedSectionListView android:id="@+id/listview" android:layout_width="match_parent" android:layout_height="match_parent"> </com.example.pinnedsectionlistview.PinnedSectionListView> </RelativeLayout>
<!-- 拨打电话权限 --> <uses-permission android:name="android.permission.CALL_PHONE" /> <!-- 写通讯录权限 --> <uses-permission android:name="android.permission.WRITE_CONTACTS" /> <!-- 读通讯录权限 --> <uses-permission android:name="android.permission.READ_CONTACTS" />