Create a gist now

Instantly share code, notes, and snippets.

Simple ListView page indicator for use with ViewPagerIndicator
import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.AttributeSet;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import com.viewpagerindicator.PageIndicator;
import java.util.ArrayList;
public class ListPageIndicator extends ListView implements PageIndicator, OnItemClickListener {
private static final CharSequence EMPTY_TITLE = "";
private Context context;
private ViewPager pager;
private OnPageChangeListener listener;
private ArrayList<CharSequence> titles;
private int selectedIndex;
public ListPageIndicator(Context context, AttributeSet attrs) {
super(context, attrs);
this.context = context;
setChoiceMode(CHOICE_MODE_SINGLE);
setOnItemClickListener(this);
}
@Override
public void setViewPager(ViewPager pager) {
if (this.pager == pager) {
return;
}
if (this.pager != null) {
this.pager.setOnPageChangeListener(null);
}
final PagerAdapter adapter = pager.getAdapter();
if (adapter == null) {
throw new IllegalStateException("ViewPager does not have adapter instance.");
}
this.pager = pager;
pager.setOnPageChangeListener(this);
notifyDataSetChanged();
}
@Override
public void setViewPager(ViewPager pager, int initialPosition) {
setViewPager(pager);
setCurrentItem(initialPosition);
}
@Override
public void setCurrentItem(int position) {
if (pager == null) {
throw new IllegalStateException("ViewPager has not been bound.");
}
selectedIndex = position;
pager.setCurrentItem(selectedIndex);
setItemChecked(position, true);
}
@Override
public void notifyDataSetChanged() {
titles = new ArrayList<CharSequence>();
PagerAdapter pagerAdapter = pager.getAdapter();
int count = pagerAdapter.getCount();
for (int i = 0; i < count; i++) {
CharSequence title = pagerAdapter.getPageTitle(i);
if (title == null) {
title = EMPTY_TITLE;
}
titles.add(title);
}
if (selectedIndex > count) {
selectedIndex = count - 1;
}
setData();
setCurrentItem(selectedIndex);
}
private void setData() {
setAdapter(new ArrayAdapter<CharSequence>(context, android.R.layout.simple_list_item_activated_1, titles));
}
@Override
public void setOnPageChangeListener(OnPageChangeListener listener) {
this.listener = listener;
}
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
if (listener != null) {
listener.onPageScrolled(position, positionOffset, positionOffsetPixels);
}
}
@Override
public void onPageSelected(int position) {
setCurrentItem(position);
if (listener != null) {
listener.onPageSelected(position);
}
}
@Override
public void onPageScrollStateChanged(int i) {
if (listener != null) {
listener.onPageScrollStateChanged(i);
}
}
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
setCurrentItem(i);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment