PageAdapter で使用する CirclepageIndicator クラスと Adapter クラス。ここでは、CirclePageIndicator を使用してアクティビティの下部に円を表示し、2 つのフラグメント クラスを追加しています (任意の数のフラグメントを追加できます)。
ステップ 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;
}
}
ステップ 3: ViewPager_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: ViewPager_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 最後に、res/value フォルダー内の style.xml を更新する必要があります。
<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>