Download the libraries:
ActionBarSherlock
ViewPagerIndicator
Extract the zip files. These libraries are packaged with sample codes. Import these libraries into your Eclipse projects.
Create your new project. I assume that you are an Eclipse user and not new to android development. Add these libraries to your project as a
library project. By adding these libraries to your new project, you are telling Android that you are using ActionBarSherlock and ViewPagerIndicator as part of your android project.
To check if libraries are added, go to Project Properties > Android. It should look like this:
If you encountered an error like this, it's okay don't panic.
This tells that Eclipse detected android-support-v4.jar file in the dependecy list of your project twice but with different SHA-1 hash. To fix this, just remove the file under <your_projectname>/libs/android-support-v4.jar
Now let's proceed to the fun part... the code.
layout xml.
MainActivity.java
package com.zipcerio.vpi;
import java.util.ArrayList;
import java.util.List;
import com.actionbarsherlock.app.SherlockFragmentActivity;
import com.actionbarsherlock.view.Menu;
import com.viewpagerindicator.TitlePageIndicator;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
public class MainActivity extends SherlockFragmentActivity {
private ViewPager mPager;
private TitlePageIndicator mIndicator;
private MainPagerAdapter mAdapter;
private List mFragments;
private static final String FRAGMENT1 = Fragment1.class.getName();
private static final String FRAGMENT2 = Fragment1.class.getName();
private static final String FRAGMENT3 = Fragment1.class.getName();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.a_main);
// add fragments
mFragments = new ArrayList();
mFragments.add(Fragment.instantiate(this, FRAGMENT1));
mFragments.add(Fragment.instantiate(this, FRAGMENT2));
mFragments.add(Fragment.instantiate(this, FRAGMENT3));
// adapter
mAdapter = new MainPagerAdapter(getSupportFragmentManager(), mFragments);
// pager
mPager = (ViewPager) findViewById(R.id.view_pager);
mPager.setAdapter(mAdapter);
// indicator
mIndicator = (TitlePageIndicator) findViewById(R.id.title_indicator);
mIndicator.setViewPager(mPager);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
return true;
}
}
MainPagerAdapter.java
package com.zipcerio.vpi;
import java.util.List;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
public class MainPagerAdapter extends FragmentPagerAdapter {
private List mFragments;
private String[] titles = new String[] {"FRAGMENT1", "FRAGMENT2", "FRAGMENT3"};
private int mCount = titles.length;
public MainPagerAdapter(FragmentManager fm, List f) {
super(fm);
mFragments = f;
}
@Override
public Fragment getItem(int position) {
return mFragments.get(position);
}
@Override
public int getCount() {
return mCount;
}
@Override
public CharSequence getPageTitle(int position) {
return titles[position];
}
}
Fragment1.java
package com.zipcerio.vpi;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import com.actionbarsherlock.app.SherlockFragment;
public class Fragment1 extends SherlockFragment {
private ListView mList;
@Override
public View onCreateView(LayoutInflater inf, ViewGroup grp, Bundle icicle) {
View v = inf.inflate(R.layout.f_fragment1, grp, false);
mList = (ListView) v.findViewById(R.id.listView1);
return v;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
ArrayAdapter adapter = new ArrayAdapter(
getActivity(), android.R.layout.simple_list_item_1, Cheese.STRINGS);
mList.setAdapter(adapter);
}
}
AndroidManifest file
Finally, this is how it should look like.
See the source code
here.
Credit goes to Jake Wharton for these great libraries.
Github:
https://github.com/JakeWharton
Twitter:
https://twitter.com/JakeWharton
StackOverflow:
http://stackoverflow.com/users/132047/jake-wharton