在一个FragmentLayout里面添加圆点实在翻页的基础上完成的,其实圆点的视图(View)就在Fragment里面与Viewpager不用层面上,就好像你穿了一件针织衫,现在再在你的衣服表面钉几个扣子是一个现象。看下面xml代码:
<?xml version="1.0" encoding="UTF-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.newworld.dmsapp2.fragment.MainHomeFragment"
tools:ignore="MergeRootFrame" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="20dp"
android:orientation="vertical" >
<android.support.v4.view.ViewPager
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<!-- 定义导航状态条组件 -->
<!-- <这里可以用一个炫酷的图片或两个点表示!> -->
<!--
<android.support.v4.view.PagerTitleStrip
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="top"
android:background="#33b5e5"
android:textColor="#fff"
android:paddingTop="4dp"
android:paddingBottom="4dp" />
-->
</android.support.v4.view.ViewPager>
<!--
<GridView
android:id="@+id/home_grid"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:horizontalSpacing="20dp"
android:numColumns="2"
android:padding="15dp"
android:stretchMode="columnWidth"
android:verticalSpacing="15dp" />
-->
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="5dp"
android:layout_marginBottom="1dp"
android:orientation="horizontal" >
<ImageView
android:id="@+id/down_page_num"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="35dip"
android:layout_gravity="bottom"
android:gravity="center"
android:orientation="vertical" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="3dip"
android:orientation="horizontal" >
<View
android:id="@+id/dot_1"
android:layout_width="10dip"
android:layout_height="10dip"
android:layout_marginLeft="2dip"
android:layout_marginRight="5dip"
android:background="@drawable/dot_normal" />
<View
android:id="@+id/dot_2"
android:layout_width="10dip"
android:layout_height="10dip"
android:layout_marginLeft="5dip"
android:layout_marginRight="2dip"
android:background="@drawable/dot_normal" />
</LinearLayout>
</LinearLayout>
</FrameLayout>
做完这步后,再java里面加上相应显示与操作:
private void initView(View parentView){
resources = getResources();
dots = new ArrayList<View>();
dot1 = (View)parentView.findViewById(R.id.dot_1);
dot2 = (View)parentView.findViewById(R.id.dot_2);
dots.add(parentView.findViewById(R.id.dot_1));
dots.get(0).setBackgroundResource(R.drawable.dot_focused);
if(Config.FUNCTION_PAGE == 1){
dot2.setVisibility(View.GONE);
}else if(Config.FUNCTION_PAGE == 2){
dots.add(parentView.findViewById(R.id.dot_2));
dot2.setVisibility(View.VISIBLE);
}
mPager = (ViewPager) parentView.findViewById(R.id.pager);
fragmentsList = new ArrayList<Fragment>();
if (Config.FUNCTION_PAGE == 1) {
home1 = new MainHomeFragmentPiece();
fragmentsList.add(home1);
} else if (Config.FUNCTION_PAGE == 2) {
home1 = new MainHomeFragmentPiece();
fragmentsList.add(home1);
home2 = new MainHomeFragmentPieceNext();
fragmentsList.add(home2);
}
mPager.setAdapter(new MyFragmentPagerAdapter(getChildFragmentManager(),
fragmentsList));
mPager.setOnPageChangeListener(new MyOnPageChangeListener());
mPager.setCurrentItem(0);
}
public class MyOnPageChangeListener implements OnPageChangeListener {
@Override
public void onPageSelected(int arg0) {
//Animation animation = null;
dots.get(oldPosition).setBackgroundResource(
R.drawable.dot_normal);
dots.get(arg0)
.setBackgroundResource(R.drawable.dot_focused);
oldPosition = arg0;
currentItem = arg0;
}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
}
@Override
public void onPageScrollStateChanged(int arg0) {
}
}