CirclepageIndicator 类和 Adapter 类与 PageAdapter 一起使用。这里我使用 CirclePageIndicator 在活动底部显示圆圈并添加两个片段类(可以添加任意数量的片段)。
第 1 步:FragmentActivity 类(如主类)
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.Window;
import com.viewpagerindicator.CirclePageIndicator;
import com.viewpagerindicator.PageIndicator;
public class ViewPager_activity extends FragmentActivity
{
private ViewPager _mViewPager;
private ViewPagerAdapter _adapter;
PageIndicator mIndicator;
CirclePageIndicator circlePageIndicator;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.viewpager_main_activity);
setUpView();
}
private void setUpView()
{
_adapter = new ViewPagerAdapter(getApplicationContext(),getSupportFragmentManager());
_mViewPager = (ViewPager) findViewById(R.id.pager);
_mViewPager.setAdapter(_adapter);
circlePageIndicator=(CirclePageIndicator)findViewById(R.id.indicator);
mIndicator=circlePageIndicator;
circlePageIndicator.setViewPager(_mViewPager);
final float density = getResources().getDisplayMetrics().density;
circlePageIndicator.setBackgroundColor(0xFFCCCCCC);
circlePageIndicator.setRadius(10 * density);
circlePageIndicator.setPageColor(0xFF888888);
circlePageIndicator.setFillColor(0x880000FF);
circlePageIndicator.setStrokeColor(0xFF000000);
circlePageIndicator.setStrokeWidth(2 * density);
circlePageIndicator.setSnap(true);
_mViewPager.setCurrentItem(0);
setCirclePageIndicator();
}
public void setCirclePageIndicator()
{
circlePageIndicator.setOnPageChangeListener(new OnPageChangeListener() {
@Override
public void onPageSelected(int position) {
// TODO Auto-generated method stub
btnAction(position);
}
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
// TODO Auto-generated method stub
//btnAction(position);
}
@Override
public void onPageScrollStateChanged(int arg0) {
// TODO Auto-generated method stub
}
});
}
private void btnAction(int action){
switch(action){
case 0:
break;
case 2:
startActivity(new Intent(ViewPager_activity.this,HomePage_Activity.class));
break;
}
}
}
第 2 步:FragmentPageAdapter.class
public class ViewPagerAdapter extends FragmentPagerAdapter
{
private Context _context;
public static int totalPage=3;
public ViewPagerAdapter(Context context, FragmentManager fm) {
super(fm);
_context=context;
}
@Override
public Fragment getItem(int position) {
Fragment f = new Fragment();
switch(position){
case 0:
f=ViewPager_first_fragment.newInstance(_context);
break;
case 1:
f=ViewPager_second_fragment.newInstance(_context);
break;
}
return f;
}
@Override
public int getCount() {
return totalPage;
}
}
第三步:查看Pager_first_fragment.class
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
public class ViewPager_first_fragment extends Fragment
{
ImageView advertiseImage;
public static Fragment newInstance(Context context) {
ViewPager_first_fragment f = new ViewPager_first_fragment();
return f;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState)
{
ViewGroup root = (ViewGroup) inflater.inflate(R.layout.viewpager_first_fragment, null);
advertiseImage=(ImageView)root.findViewById(R.id.advertiseScreen1);
advertiseImage.setImageResource(R.drawable.advertisescreen_1);
return root;
}
}
第 4 步:查看Pager_main_activity.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<android.support.v4.view.ViewPager
android:id="@+id/pager"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
<com.viewpagerindicator.CirclePageIndicator
android:id="@+id/indicator"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:padding="10dip" />
</RelativeLayout>
第 5 步:
<ImageView
android:id="@+id/advertiseScreen1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
</LinearLayout>
第 6 步:style.xml 最后你必须更新你的 style.xml indise res/value 文件夹。
<style name="AppTheme" parent="AppBaseTheme">
<item name="vpiCirclePageIndicatorStyle">@style/CirclePageIndicator</item>
</style>
<style name="Widget"></style>
<style name="CirclePageIndicator" parent="Widget">
<item name="android:textColor">#ffffff</item>
<item name="android:textSize">14sp</item>
<item name="android:textStyle">italic</item>
<item name="android:paddingLeft">16dp</item>
<item name="android:paddingRight">16dp</item>
<item name="android:fadingEdge">horizontal</item>
<item name="android:fadingEdgeLength">8dp</item>
</style>