Android HorizontalScrollView with center lock

During the development of News app , i needed a view which should slide horizontally and auto highlight the selected item in center of device screen. Something like Pulse app does in their news detail page where they have a quick access to news of browsing category. From user perceptive it was very handy feature so that user have quick access on current browsed category.
So i decide to use Android HorizontalScrollview with some modification.
Here is, how i implemented it on my news app.

1) create a custom java class extending HorizontalScrollView and define the function which center the current selected item. Something like this.


public class CenterLockHorizontalScrollview extends HorizontalScrollView {
	Context context;
	int prevIndex = 0;

	public CenterLockHorizontalScrollview(Context context, AttributeSet attrs) {
		super(context, attrs);
		this.context = context;
		this.setSmoothScrollingEnabled(true);

	}

	public void setAdapter(Context context, CustomListAdapter mAdapter) {

		try {
			fillViewWithAdapter(mAdapter);
		} catch (ZeroChildException e) {

			e.printStackTrace();
		}
	}

	private void fillViewWithAdapter(CustomListAdapter mAdapter)
			throws ZeroChildException {
		if (getChildCount() == 0) {
			throw new ZeroChildException(
					"CenterLockHorizontalScrollView must have one child");
		}
		if (getChildCount() == 0 || mAdapter == null)
			return;

		ViewGroup parent = (ViewGroup) getChildAt(0);

		parent.removeAllViews();

		for (int i = 0; i < mAdapter.getCount(); i++) {
			parent.addView(mAdapter.getView(i, null, parent));
		}
	}

	public void setCenter(int index) {

		ViewGroup parent = (ViewGroup) getChildAt(0);

		View preView = parent.getChildAt(prevIndex);
		preView.setBackgroundColor(Color.parseColor("#64CBD8"));
		android.widget.LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
				LinearLayout.LayoutParams.WRAP_CONTENT,
				LinearLayout.LayoutParams.WRAP_CONTENT);
		lp.setMargins(5, 5, 5, 5);
		preView.setLayoutParams(lp);

		View view = parent.getChildAt(index);
		view.setBackgroundColor(Color.RED);

		int screenWidth = ((Activity) context).getWindowManager()
				.getDefaultDisplay().getWidth();

		int scrollX = (view.getLeft() - (screenWidth / 2))
				+ (view.getWidth() / 2);
		this.smoothScrollTo(scrollX, 0);
		prevIndex = index;
	}

}

2)Create xml file in res/layout

<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" >

    <com.krish.horizontalscrollview.CenterLockHorizontalScrollview
        android:id="@+id/scrollView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true" >

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal" >
        </LinearLayout>
    </com.krish.horizontalscrollview.CenterLockHorizontalScrollview>

    <LinearLayout
        android:id="@+id/bottomLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_above="@id/scrollView"
        android:orientation="horizontal"
        android:weightSum="2" >

        <Button
            android:id="@+id/btnPrev"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Prev" />

        <Button
            android:id="@+id/btnNext"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Next" />
    </LinearLayout>

    <TextView
        android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@id/bottomLayout"
        android:layout_alignParentTop="true"
        android:layout_centerInParent="true"
        android:textColor="#FF0000"
        android:padding="@dimen/padding_medium" />

</RelativeLayout>



3)Use the CenterLockHorizontalScrollview in your activity as follows.

public class HorizontalScrollViewActivity extends Activity {
	CenterLockHorizontalScrollview centerLockHorizontalScrollview;
	CustomListAdapter customListAdapter;
	Button btnPrev, btnNext;
	int currIndex = 0;
	private TextView text;
	ArrayList<String> list = new ArrayList<String>() {

		{
			add("Manchester city");
			add("Manchester United");
			add("Chelsea");
			add("Liverpool");
			add("TottenHam");
			add("Everton");
			add("WestHam");
			add("Arsenal");
			add("West Broom");
			add("New Castle");
			add("Norich City");
			add("Swansea city");
			add("stroke city");

		}
	};

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_horizontal_scroll_view);
		btnNext = (Button) findViewById(R.id.btnNext);
		btnPrev = (Button) findViewById(R.id.btnPrev);
		text=(TextView)findViewById(R.id.text);
		centerLockHorizontalScrollview = (CenterLockHorizontalScrollview) findViewById(R.id.scrollView);
		customListAdapter = new CustomListAdapter(this,
				R.layout.news_list_item, list);
		centerLockHorizontalScrollview.setAdapter(this, customListAdapter);
		btnNext.setOnClickListener(onClickListener);
		btnPrev.setOnClickListener(onClickListener);

	}

	OnClickListener onClickListener = new OnClickListener() {

		@Override
		public void onClick(View v) {
			if (v.getId() == R.id.btnPrev) {
				if (currIndex != 0) {
					currIndex--;
					centerLockHorizontalScrollview.setCenter(currIndex);
					text.setText(list.get(currIndex==0?0:currIndex-1));
				}
			} else if (v.getId() == R.id.btnNext) {

				if (currIndex < list.size()) {
					centerLockHorizontalScrollview.setCenter(currIndex);
					currIndex++;
					text.setText(list.get(currIndex-1));
				}
			}

		}
	};

}

4)Expected out

device-2012-12-09-234522

source with and demo project can also be found on my github account.