Skip to content
This repository has been archived by the owner on Jun 9, 2020. It is now read-only.

Commit

Permalink
Code refactoring & bug fixes
Browse files Browse the repository at this point in the history
- Pinned section detection algorithm was dramatically simplified.
- Fast scroll issues were fixed.
- Section click handler for a list with paddings was fixed.
- On layout is handled properly.
- Example application was extended with fast scroll and padding options.
  • Loading branch information
beworker committed Nov 4, 2013
1 parent 822b076 commit 80f9afb
Show file tree
Hide file tree
Showing 4 changed files with 439 additions and 427 deletions.
12 changes: 9 additions & 3 deletions example/res/menu/main.xml
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
<menu xmlns:android="http://schemas.android.com/apk/res/android" >

<item
android:id="@+id/action_settings"
android:orderInCategory="100"
android:id="@+id/action_fastscroll"
android:showAsAction="never"
android:title="@string/action_settings"/>
android:title="@string/action_fastscroll"
android:checkable="true"/>

<item
android:id="@+id/action_addpadding"
android:showAsAction="never"
android:title="@string/action_addpadding"
android:checkable="true"/>

</menu>
3 changes: 2 additions & 1 deletion example/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
<resources>

<string name="app_name">Pinned Section Demo</string>
<string name="action_settings">Settings</string>
<string name="action_fastscroll">Fast scroll</string>
<string name="action_addpadding">Add padding</string>

</resources>
206 changes: 159 additions & 47 deletions example/src/com/hb/examples/PinnedSectionListActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,22 @@

package com.hb.examples;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

import android.annotation.SuppressLint;
import android.app.ListActivity;
import android.content.Context;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.SectionIndexer;
import android.widget.TextView;
import android.widget.Toast;

Expand All @@ -37,91 +40,200 @@

public class PinnedSectionListActivity extends ListActivity implements OnClickListener {

private static final int[] COLORS = new int[] {
R.color.green_light, R.color.orange_light,
R.color.blue_light, R.color.red_light };
static class SimpleAdapter extends ArrayAdapter<Item> implements PinnedSectionListAdapter {

private static final int[] COLORS = new int[] {
R.color.green_light, R.color.orange_light,
R.color.blue_light, R.color.red_light };

public SimpleAdapter(Context context, int resource, int textViewResourceId) {
super(context, resource, textViewResourceId);

final int sectionsNumber = 'Z' - 'A' + 1;
prepareSections(sectionsNumber);

int sectionPosition = 0, listPosition = 0;
for (char i=0; i<sectionsNumber; i++) {
Item section = new Item(Item.SECTION, String.valueOf((char)('A' + i)));
section.sectionPosition = sectionPosition;
section.listPosition = listPosition++;
onSectionAdded(section, sectionPosition);
add(section);

final int itemsNumber = (int) Math.abs((Math.cos(2f*Math.PI/3f * sectionsNumber / (i+1f)) * 25f));
for (int j=0;j<itemsNumber;j++) {
Item item = new Item(Item.ITEM, section.text.toUpperCase(Locale.ENGLISH) + " - " + j);
item.sectionPosition = sectionPosition;
item.listPosition = listPosition++;
add(item);
}

sectionPosition++;
}
}

protected void prepareSections(int sectionsNumber) { }
protected void onSectionAdded(Item section, int sectionPosition) { }

@Override public View getView(int position, View convertView, ViewGroup parent) {
TextView view = (TextView) super.getView(position, convertView, parent);
view.setTextColor(Color.DKGRAY);
view.setTag("" + position);
Item item = getItem(position);
if (item.type == Item.SECTION) {
//view.setOnClickListener(PinnedSectionListActivity.this);
view.setBackgroundColor(parent.getResources().getColor(COLORS[item.sectionPosition % COLORS.length]));
}
return view;
}

@Override public int getViewTypeCount() {
return 2;
}

@Override public int getItemViewType(int position) {
return getItem(position).type;
}

@Override
public boolean isItemViewTypePinned(int viewType) {
return viewType == Item.SECTION;
}

private class MyPinnedSectionListAdapter extends ArrayAdapter<Item> implements PinnedSectionListAdapter {
}

public MyPinnedSectionListAdapter(Context context, int resource, int textViewResourceId, List<Item> objects) {
super(context, resource, textViewResourceId, objects);
}
static class FastScrollAdapter extends SimpleAdapter implements SectionIndexer {

@Override public View getView(int position, View convertView, ViewGroup parent) {
TextView view = (TextView) super.getView(position, convertView, parent);
view.setTextColor(Color.DKGRAY);
view.setTag("" + position);
if (getItem(position).type == Item.SECTION) {
//view.setOnClickListener(PinnedSectionListActivity.this);
view.setBackgroundColor(parent.getResources().getColor(COLORS[position % COLORS.length]));
}
return view;
}
private Item[] sections;

@Override public int getViewTypeCount() {
return 2;
}
public FastScrollAdapter(Context context, int resource, int textViewResourceId) {
super(context, resource, textViewResourceId);
}

@Override public int getItemViewType(int position) {
return getItem(position).type;
}
@Override protected void prepareSections(int sectionsNumber) {
sections = new Item[sectionsNumber];
}

@Override public boolean isItemViewTypePinned(int viewType) {
return viewType == Item.SECTION;
}
}
@Override protected void onSectionAdded(Item section, int sectionPosition) {
sections[sectionPosition] = section;
}

@Override public Item[] getSections() {
return sections;
}

@Override public int getPositionForSection(int section) {
if (section >= sections.length) {
section = sections.length - 1;
}
return sections[section].listPosition;
}

@Override public int getSectionForPosition(int position) {
if (position >= getCount()) {
position = getCount() - 1;
}
return getItem(position).sectionPosition;
}

}

static class Item {

private static class Item {
public static final int ITEM = 0;
public static final int SECTION = 1;

public final int type;
public final String text;

public int sectionPosition;
public int listPosition;

public Item(int type, String text) {
this.type = type;
this.text = text;
this.type = type;
this.text = text;
}

@Override public String toString() {
return text;
}

}

private boolean isFastScroll;
private boolean addPadding;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
MyPinnedSectionListAdapter adapter = new MyPinnedSectionListAdapter(
this, android.R.layout.simple_list_item_1, android.R.id.text1, prepareItems());
setListAdapter(adapter);
if (savedInstanceState != null) {
isFastScroll = savedInstanceState.getBoolean("isFastScroll");
addPadding = savedInstanceState.getBoolean("addPadding");
}
initializeAdapter();
initializePadding();
}

@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putBoolean("isFastScroll", isFastScroll);
outState.putBoolean("addPadding", addPadding);
}

@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
Toast.makeText(this, "Item: " + position, Toast.LENGTH_SHORT).show();
Item item = (Item) getListAdapter().getItem(position);
Toast.makeText(this, "Item " + position + ": " + item.text, Toast.LENGTH_SHORT).show();
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
menu.getItem(0).setChecked(isFastScroll);
menu.getItem(1).setChecked(addPadding);
return true;
}

private static ArrayList<Item> prepareItems() {
ArrayList<Item> result = new ArrayList<Item>();
for (int i = 0; i < 30; i++) {
result.add(new Item(Item.SECTION, "Section " + i));
for (int j=0; j<4; j++) {
result.add(new Item(Item.ITEM, "Item " + j));
}
}
return result;
@Override
public boolean onOptionsItemSelected(MenuItem item) {

if (item.getItemId() == R.id.action_fastscroll) {
isFastScroll = !isFastScroll;
item.setChecked(isFastScroll);
initializeAdapter();
} else {
addPadding = !addPadding;
item.setChecked(addPadding);
initializePadding();
}

return true;
}

private void initializePadding() {
float density = getResources().getDisplayMetrics().density;
int padding = addPadding ? (int) (16 * density) : 0;
getListView().setPadding(padding, padding, padding, padding);
}

@SuppressLint("NewApi")
private void initializeAdapter() {
getListView().setFastScrollEnabled(isFastScroll);
if (isFastScroll) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
getListView().setFastScrollAlwaysVisible(true);
}
setListAdapter(new FastScrollAdapter(this, android.R.layout.simple_list_item_1, android.R.id.text1));
} else {
setListAdapter(new SimpleAdapter(this, android.R.layout.simple_list_item_1, android.R.id.text1));
}
}

@Override
public void onClick(View v) {
Toast.makeText(this, "Item: " + v.getTag(), Toast.LENGTH_SHORT).show();
Toast.makeText(this, "Item: " + v.getTag() , Toast.LENGTH_SHORT).show();
}

}
}
Loading

0 comments on commit 80f9afb

Please sign in to comment.