Skip to content

Instantly share code, notes, and snippets.

@daichan4649
Created November 22, 2011 02:51
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save daichan4649/1384757 to your computer and use it in GitHub Desktop.
Save daichan4649/1384757 to your computer and use it in GitHub Desktop.
customize fastScroller(change overlay and scroller) for Android
// Activity sample
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TestAdapter adapter = new TestAdapter(getApplicationContext(), createBindDataList());
ListView listView = (ListView) findViewById(R.id.listview);
listView.setAdapter(adapter);
// 高速スクローラ有効化設定
listView.setFastScrollEnabled(true);
// AbsListView#setFastScrollEnabled 直後に実行
Drawable overlay = getResources().getDrawable(R.drawable.ic_launcher);
Drawable scroller = getResources().getDrawable(R.drawable.ic_launcher);
customizeFastScroller(listView, overlay, scroller);
}
/**
* 高速スクローラカスタマイズ
* @param listView 対象のAbsListView({@link AbsListView})
* @param overlay overlay描画に使用するdrawable
* @param scroller スクローラ描画に使用するdrawable
*/
private void customizeFastScroller(AbsListView listView, Drawable overlay, Drawable scroller) {
try {
// 新FastScrollerインスタンス生成
Class<?> clazz = Class.forName("android.widget.FastScroller");
Constructor<?> constructor = clazz.getConstructor(Context.class, AbsListView.class);
Object newFastScroller = constructor.newInstance(this, listView);
// ガイド用drawable(グレーの四角)を上書き
Field fieldOverlay = clazz.getDeclaredField("mOverlayDrawable");
fieldOverlay.setAccessible(true);
fieldOverlay.set(newFastScroller, overlay);
// スクローラ用drawableを上書き
Field fieldThumb = clazz.getDeclaredField("mThumbDrawable");
fieldThumb.setAccessible(true);
fieldThumb.set(newFastScroller, scroller);
// FastScrollerインスタンス(オリジナル)を上書き
Field orgFastScroller = AbsListView.class.getDeclaredField("mFastScroller");
orgFastScroller.setAccessible(true);
orgFastScroller.set(listView, newFastScroller);
} catch (Exception e) {
e.printStackTrace();
}
}
// 以下、ListView構築用
private List<BindData> createBindDataList() {
List<BindData> list = new ArrayList<BindData>();
for (int i = 0; i < ITEM_SIZE; i++) {
list.add(new BindData("[" + i + "]"));
}
return list;
}
private static class TestAdapter extends ArrayAdapter<BindData> implements SectionIndexer {
private LayoutInflater inflater;
public TestAdapter(Context context, List<BindData> objects) {
super(context, 0, objects);
inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = inflater.inflate(R.layout.list_item, null);
holder = new ViewHolder();
holder.textView = (TextView) convertView.findViewById(R.id.textView);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
// BindData反映
BindData bindData = getItem(position);
holder.textView.setText(bindData.getText());
return convertView;
}
// SectionIndexer関連
private static final String[] sections;
static {
sections = new String[ITEM_SIZE];
for (int i = 0; i < ITEM_SIZE; i++) {
sections[i] = "i=" + i;
}
}
@Override
public int getPositionForSection(int section) {
return section;
}
@Override
public int getSectionForPosition(int position) {
return position;
}
@Override
public Object[] getSections() {
return sections;
}
}
private static class ViewHolder {
private TextView textView;
}
private static class BindData {
private String text;
public BindData(String text) {
this.text = text;
}
public String getText() {
return text;
}
@Override
public String toString() {
StringBuilder log = new StringBuilder();
log.append("[");
log.append("text=").append(text);
log.append("]");
return log.toString();
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#64ffffff" >
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</TextView>
</LinearLayout>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment