actvity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<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.samset.listviewindexer.MainActivity">
<TextView
android:id="@+id/txt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dp"
android:layout_centerHorizontal="true"
android:text="Samset Fast List Indexer" />
<com.samset.listviewindexer.utility.IndexerFastListView
android:id="@+id/listview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/txt"
android:layout_centerHorizontal="true" />
</RelativeLayout>
<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.samset.listviewindexer.MainActivity">
<TextView
android:id="@+id/txt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dp"
android:layout_centerHorizontal="true"
android:text="Samset Fast List Indexer" />
<com.samset.listviewindexer.utility.IndexerFastListView
android:id="@+id/listview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/txt"
android:layout_centerHorizontal="true" />
</RelativeLayout>
ActvityMain.java
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import com.samset.listviewindexer.adapter.ListIndexAdapter;
import com.samset.listviewindexer.utility.Constants;
import com.samset.listviewindexer.utility.IndexerFastListView;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private IndexerFastListView listView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = (IndexerFastListView) findViewById(R.id.listview);
ListIndexAdapter adapter = new ListIndexAdapter(Constants.getData(), this);
listView.setAdapter(adapter);
listView.setFastScrollEnabled(true);
}
}
import android.os.Bundle;
import com.samset.listviewindexer.adapter.ListIndexAdapter;
import com.samset.listviewindexer.utility.Constants;
import com.samset.listviewindexer.utility.IndexerFastListView;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private IndexerFastListView listView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = (IndexerFastListView) findViewById(R.id.listview);
ListIndexAdapter adapter = new ListIndexAdapter(Constants.getData(), this);
listView.setAdapter(adapter);
listView.setFastScrollEnabled(true);
}
}
IndexerFastListView.java
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SectionIndexer;
public class IndexerFastListView extends ListView {
private Context ctx;
private static int indWidth = 20;
private String[] sections;
private float scaledWidth;
private float sx;
private int indexSize;
private String section;
private boolean showLetter = true;
private Handler listHandler;
public IndexerFastListView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
ctx = context;
}
public IndexerFastListView(Context context, AttributeSet attrs) {
super(context, attrs);
ctx = context;
}
public IndexerFastListView(Context context, String keyList) {
super(context);
ctx = context;
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
scaledWidth = indWidth * getSizeInPixel(ctx);
sx = this.getWidth() - this.getPaddingRight() - scaledWidth;
Paint p = new Paint();
p.setColor(Color.WHITE);
p.setAlpha(100);
canvas.drawRect(sx, this.getPaddingTop(), sx + scaledWidth,
this.getHeight() - this.getPaddingBottom(), p);
indexSize = (this.getHeight() - this.getPaddingTop() - getPaddingBottom())
/ sections.length;
Paint textPaint = new Paint();
textPaint.setColor(Color.DKGRAY);
textPaint.setTextSize(scaledWidth / 2);
for (int i = 0; i < sections.length; i++)
canvas.drawText(sections[i].toUpperCase(),
sx + textPaint.getTextSize() / 2, getPaddingTop()
+ indexSize * (i + 1), textPaint);
// We draw the letter in the middle
if (showLetter & section != null && !section.equals("")) {
Paint textPaint2 = new Paint();
textPaint2.setColor(Color.DKGRAY);
textPaint2.setTextSize(2 * indWidth);
canvas.drawText(section.toUpperCase(), getWidth() / 2, getHeight() / 2, textPaint2);
}
}
private static float getSizeInPixel(Context ctx) {
return ctx.getResources().getDisplayMetrics().density;
}
@Override
public void setAdapter(ListAdapter adapter) {
super.setAdapter(adapter);
if (adapter instanceof SectionIndexer)
sections = (String[]) ((SectionIndexer) adapter).getSections();
}
@Override
public boolean onTouchEvent(MotionEvent event) {
float x = event.getX();
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN: {
if (x < sx)
return super.onTouchEvent(event);
else {
// We touched the index bar
float y = event.getY() - this.getPaddingTop() - getPaddingBottom();
int currentPosition = (int) Math.floor(y / indexSize);
section = sections[currentPosition];
this.setSelection(((SectionIndexer) getAdapter())
.getPositionForSection(currentPosition));
}
break;
}
case MotionEvent.ACTION_MOVE: {
if (x < sx)
return super.onTouchEvent(event);
else {
float y = event.getY();
int currentPosition = (int) Math.floor(y / indexSize);
section = sections[currentPosition];
this.setSelection(((SectionIndexer) getAdapter())
.getPositionForSection(currentPosition));
}
break;
}
case MotionEvent.ACTION_UP: {
listHandler = new ListHandler();
listHandler.sendEmptyMessageDelayed(0, 30 * 1000);
break;
}
}
return true;
}
private class ListHandler extends Handler {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
showLetter = false;
IndexerFastListView.this.invalidate();
}
}
}
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SectionIndexer;
public class IndexerFastListView extends ListView {
private Context ctx;
private static int indWidth = 20;
private String[] sections;
private float scaledWidth;
private float sx;
private int indexSize;
private String section;
private boolean showLetter = true;
private Handler listHandler;
public IndexerFastListView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
ctx = context;
}
public IndexerFastListView(Context context, AttributeSet attrs) {
super(context, attrs);
ctx = context;
}
public IndexerFastListView(Context context, String keyList) {
super(context);
ctx = context;
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
scaledWidth = indWidth * getSizeInPixel(ctx);
sx = this.getWidth() - this.getPaddingRight() - scaledWidth;
Paint p = new Paint();
p.setColor(Color.WHITE);
p.setAlpha(100);
canvas.drawRect(sx, this.getPaddingTop(), sx + scaledWidth,
this.getHeight() - this.getPaddingBottom(), p);
indexSize = (this.getHeight() - this.getPaddingTop() - getPaddingBottom())
/ sections.length;
Paint textPaint = new Paint();
textPaint.setColor(Color.DKGRAY);
textPaint.setTextSize(scaledWidth / 2);
for (int i = 0; i < sections.length; i++)
canvas.drawText(sections[i].toUpperCase(),
sx + textPaint.getTextSize() / 2, getPaddingTop()
+ indexSize * (i + 1), textPaint);
// We draw the letter in the middle
if (showLetter & section != null && !section.equals("")) {
Paint textPaint2 = new Paint();
textPaint2.setColor(Color.DKGRAY);
textPaint2.setTextSize(2 * indWidth);
canvas.drawText(section.toUpperCase(), getWidth() / 2, getHeight() / 2, textPaint2);
}
}
private static float getSizeInPixel(Context ctx) {
return ctx.getResources().getDisplayMetrics().density;
}
@Override
public void setAdapter(ListAdapter adapter) {
super.setAdapter(adapter);
if (adapter instanceof SectionIndexer)
sections = (String[]) ((SectionIndexer) adapter).getSections();
}
@Override
public boolean onTouchEvent(MotionEvent event) {
float x = event.getX();
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN: {
if (x < sx)
return super.onTouchEvent(event);
else {
// We touched the index bar
float y = event.getY() - this.getPaddingTop() - getPaddingBottom();
int currentPosition = (int) Math.floor(y / indexSize);
section = sections[currentPosition];
this.setSelection(((SectionIndexer) getAdapter())
.getPositionForSection(currentPosition));
}
break;
}
case MotionEvent.ACTION_MOVE: {
if (x < sx)
return super.onTouchEvent(event);
else {
float y = event.getY();
int currentPosition = (int) Math.floor(y / indexSize);
section = sections[currentPosition];
this.setSelection(((SectionIndexer) getAdapter())
.getPositionForSection(currentPosition));
}
break;
}
case MotionEvent.ACTION_UP: {
listHandler = new ListHandler();
listHandler.sendEmptyMessageDelayed(0, 30 * 1000);
break;
}
}
return true;
}
private class ListHandler extends Handler {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
showLetter = false;
IndexerFastListView.this.invalidate();
}
}
}
ListIndexAdapter.java
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.SectionIndexer;
import android.widget.TextView;
import java.util.List;
public class ListIndexAdapter extends ArrayAdapter<String> implements SectionIndexer {
private List<String> listdata;
private Context context;
private static String sections = "abcdefghilmnopqrstuvz";
public ListIndexAdapter(List<String> itemList, Context ctx) {
super(ctx, android.R.layout.simple_list_item_1, itemList);
this.listdata = itemList;
this.context = ctx;
}
public int getCount() {
return listdata.size();
}
public String getItem(int position) {
return listdata.get(position);
}
public long getItemId(int position) {
return listdata.get(position).hashCode();
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if (v == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = inflater.inflate(android.R.layout.simple_list_item_1, null);
}
TextView text = (TextView) v.findViewById(android.R.id.text1);
text.setText(listdata.get(position));
return v;
}
@Override
public int getPositionForSection(int section) {
Log.d("ListView", "Get position for section");
for (int i=0; i < this.getCount(); i++) {
String item = this.getItem(i).toLowerCase();
if (item.charAt(0) == sections.charAt(section))
return i;
}
return 0;
}
@Override
public int getSectionForPosition(int arg0) {
Log.d("ListView", "Get section");
return 0;
}
@Override
public Object[] getSections() {
Log.d("ListView", "Get sections");
String[] sectionsArr = new String[sections.length()];
for (int i=0; i < sections.length(); i++)
sectionsArr[i] = "" + sections.charAt(i);
return sectionsArr;
}
}
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.SectionIndexer;
import android.widget.TextView;
import java.util.List;
public class ListIndexAdapter extends ArrayAdapter<String> implements SectionIndexer {
private List<String> listdata;
private Context context;
private static String sections = "abcdefghilmnopqrstuvz";
public ListIndexAdapter(List<String> itemList, Context ctx) {
super(ctx, android.R.layout.simple_list_item_1, itemList);
this.listdata = itemList;
this.context = ctx;
}
public int getCount() {
return listdata.size();
}
public String getItem(int position) {
return listdata.get(position);
}
public long getItemId(int position) {
return listdata.get(position).hashCode();
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if (v == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = inflater.inflate(android.R.layout.simple_list_item_1, null);
}
TextView text = (TextView) v.findViewById(android.R.id.text1);
text.setText(listdata.get(position));
return v;
}
@Override
public int getPositionForSection(int section) {
Log.d("ListView", "Get position for section");
for (int i=0; i < this.getCount(); i++) {
String item = this.getItem(i).toLowerCase();
if (item.charAt(0) == sections.charAt(section))
return i;
}
return 0;
}
@Override
public int getSectionForPosition(int arg0) {
Log.d("ListView", "Get section");
return 0;
}
@Override
public Object[] getSections() {
Log.d("ListView", "Get sections");
String[] sectionsArr = new String[sections.length()];
for (int i=0; i < sections.length(); i++)
sectionsArr[i] = "" + sections.charAt(i);
return sectionsArr;
}
}
Thank you
No comments:
Post a Comment