在FragmentLayout里面添加翻页圆点

13 篇文章 0 订阅
在一个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) {
}
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值