From 6b8b15acbefdfca1cb014e72484161ebca792bb8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=90=D0=B4=D0=BE=D0=BD=D0=B0=D0=B9=20=D0=AD=D0=BB=D0=BE?= =?UTF-8?q?=D1=85=D0=B8=D0=BC?= Date: Fri, 1 Apr 2016 01:06:10 +0300 Subject: [PATCH] Add fast-scroll indexer to MediaAdapter --- res/layout/listview.xml | 3 +- .../android/vanilla/MediaAdapter.java | 70 ++++++++++++++++++- 2 files changed, 71 insertions(+), 2 deletions(-) diff --git a/res/layout/listview.xml b/res/layout/listview.xml index be033248..d79c8353 100644 --- a/res/layout/listview.xml +++ b/res/layout/listview.xml @@ -26,4 +26,5 @@ THE SOFTWARE. android:layout_height="fill_parent" android:divider="?android:attr/dividerHorizontal" android:dividerHeight="1dip" - android:scrollbarStyle="outsideInset" /> + android:scrollbarStyle="outsideInset" + android:fastScrollEnabled="true"/> diff --git a/src/ch/blinkenlights/android/vanilla/MediaAdapter.java b/src/ch/blinkenlights/android/vanilla/MediaAdapter.java index 69b1523f..374d541e 100644 --- a/src/ch/blinkenlights/android/vanilla/MediaAdapter.java +++ b/src/ch/blinkenlights/android/vanilla/MediaAdapter.java @@ -33,14 +33,17 @@ import android.provider.BaseColumns; import android.provider.MediaStore; import android.text.Spannable; import android.text.SpannableStringBuilder; +import android.text.TextUtils; import android.text.style.ForegroundColorSpan; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.ImageView; -import android.widget.LinearLayout; +import android.widget.SectionIndexer; import android.widget.TextView; + +import java.util.List; import java.util.regex.Pattern; import java.util.ArrayList; import java.lang.StringBuilder; @@ -60,6 +63,7 @@ public class MediaAdapter extends BaseAdapter implements LibraryAdapter , View.OnClickListener + , SectionIndexer { private static final Pattern SPACE_SPLIT = Pattern.compile("\\s+"); @@ -144,6 +148,8 @@ public class MediaAdapter * Setting this to MediaUtils.TYPE_INVALID disables cover artwork */ private int mCoverCacheType; + + private List mAlphabet = new ArrayList<>(512); /** * Construct a MediaAdapter representing the given type of @@ -457,6 +463,7 @@ public class MediaAdapter { Cursor old = mCursor; mCursor = cursor; + buildAlphabet(); if (cursor == null) { notifyDataSetInvalidated(); } else { @@ -635,4 +642,65 @@ public class MediaAdapter { return true; } + + private class SectionIndex + { + + public SectionIndex(char letter, int position) { + this.letter = letter; + this.position = position; + } + + private char letter; + private int position; + + @Override + public String toString() { + return String.valueOf(letter); + } + } + + private void buildAlphabet() + { + mAlphabet.clear(); + + Cursor cursor = mCursor; + if(cursor == null) + return; + + cursor.moveToFirst(); + char lastKnown = 0; + do { + String title = cursor.getString(2); + if(!TextUtils.isEmpty(title)) { + Character next = title.charAt(0); + if(next != lastKnown) { // new char + mAlphabet.add(new SectionIndex(next, cursor.getPosition())); + lastKnown = next; + } + } + } while (cursor.moveToNext()); + } + + @Override + public Object[] getSections() + { + return mAlphabet.toArray(new SectionIndex[mAlphabet.size()]); + } + + @Override + public int getPositionForSection(int sectionIndex) + { + return mAlphabet.get(sectionIndex).position; + } + + @Override + public int getSectionForPosition(int position) + { + for(int i = 0; i < mAlphabet.size(); ++i) { + if(mAlphabet.get(i).position > position) + return i - 1; + } + return 0; + } }