您现在的位置是:群英 > 开发技术 > 移动开发
Android开发中滑动圆点翻页的效果怎样实现?
Admin发表于 2021-12-21 17:48:51996 次浏览

    这篇文章给大家分享的是Android开发中滑动圆点翻页的效果的实现。这一效果是很常见的,因此分享给大家做个参考,下文给大家介绍了两种方法,文中示例代码介绍的很详细,感兴趣的朋友接下来一起跟随小编看看吧。

第一种方法:

一、测试如下,直接设置小圆点不是图标

二、准备工作

    1.在drawable创建dot.xml,设置小圆点,比较方便

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_selected="true">
        <shape android:shape="oval">
            <solid android:color="@color/black" />
            <corners android:radius="8dp" />
        </shape>
    </item>
    <item android:state_selected="false">
        <shape android:shape="oval">
            <solid android:color="@color/white" />
            <corners android:radius="8dp" />
        </shape>
    </item>
</selector>

    2.布局小圆点的状态可以被左右滑动dotview.xml

<!--?xml version="1.0" encoding="utf-8"?-->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="5dp">

    <ImageView
        android:id="@+id/v_dot"
        android:layout_width="10dp"
        android:layout_height="10dp"
        android:src="@drawable/dot"/>
</LinearLayout>

    3.分页适配器自定义ViewPagerAdapter.java

public class ViewPagerAdapter extends PagerAdapter {
    private List<View> mViewList;

    public ViewPagerAdapter(List<View> mViewList) {
        this.mViewList = mViewList;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView(mViewList.get(position));
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        container.addView(mViewList.get(position));
        return (mViewList.get(position));
    }

    @Override
    public int getCount() {
        if (mViewList == null)
            return 0;
        return mViewList.size();
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }
}

三、使用工作:

    1.activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#D4D3D3">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:background="#FFFFFF"
        android:orientation="vertical">

        <android.support.v4.view.ViewPager
            android:id="@+id/viewpager"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

        <LinearLayout
            android:id="@+id/ll_dots"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:gravity="center"
            android:orientation="horizontal" />
    </RelativeLayout>
</LinearLayout>

    分析下布局结构:
    (1)首先是一个ViewPager,用于结合GridView实现左右滑动菜单
    (2)下面是一个LinearLayout,有多少个ViewPager的页面就会inflate出多少个小圆点,并且在ViewPager翻页时,也就是说在左右滑动时,下面小圆点的状态也要相应地做出改变
    2.MainActivity.java

public class MainActivity extends AppCompatActivity {
    private ViewPager mPager;
    private LinearLayout mLlDots;
    private LayoutInflater inflater;
    private List<View> mPagerList;
    private int pageCount = 3;//默认三个小点
    /**
     * 当前显示的是第几页
     */
    private int curIndex = 0;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mPager = (ViewPager) findViewById(R.id.viewpager);
        mLlDots = (LinearLayout) findViewById(R.id.ll_dots);
        inflater = LayoutInflater.from(this);
        mPagerList = new ArrayList<View>();
        //#FF9800:黄,#4CAF50:绿,#2196F3:蓝
        String[] colors = {"#FF9800", "#4CAF50", "#2196F3"};
        for (int i = 0; i < pageCount; i++) {
            LinearLayout mLL = (LinearLayout) inflater.inflate(R.layout.linearlayout, mPager, false);
            mLL.setBackgroundColor(Color.parseColor(colors[i]));
            mPagerList.add(mLL);
        }
        //设置适配器
        mPager.setAdapter(new ViewPagerAdapter(mPagerList));
        //设置圆点
        setDotLayout();
    }

    /**
     * 设置圆点
     */
    public void setDotLayout() {
        for (int i = 0; i < pageCount; i++) {
            mLlDots.addView(inflater.inflate(R.layout.dotview, null));
        }
        // 默认显示第一页
        mLlDots.getChildAt(0).setSelected(true);
        mPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                // 取消原点选中
                mLlDots.getChildAt(curIndex).setSelected(false);
                // 原点选中
                mLlDots.getChildAt(position).setSelected(true);
                curIndex = position;
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }
}

    这代码中一句,布局LinearLayout随着左右滑动小圆点翻页

LinearLayout mLL = (LinearLayout) inflater.inflate(R.layout.linearlayout, mPager, false);

    布局linearlayout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>

第二种方法:

一、测试如下,小圆点图标

二、dotview.xml

......
<ImageView
        android:id="@+id/v_dot"
        android:layout_width="10dp"
        android:layout_height="10dp"/>
......

三、设置二个小圆点图标(黑白)

    点击链接:二个小圆点图标.zip

        // 默认显示第一页
        mLlDots.getChildAt(0).findViewById(R.id.v_dot)
                .setBackgroundResource(R.drawable.dot_normal);

        mLlDots.getChildAt(1).findViewById(R.id.v_dot)
                .setBackgroundResource(R.drawable.dot_selected);

        mLlDots.getChildAt(2).findViewById(R.id.v_dot)
                .setBackgroundResource(R.drawable.dot_selected);

        mPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            public void onPageSelected(int position) {
                // 取消圆点选中
                mLlDots.getChildAt(curIndex)
                        .findViewById(R.id.v_dot)
                        .setBackgroundResource(R.drawable.dot_selected);
                // 圆点选中
                mLlDots.getChildAt(position)
                        .findViewById(R.id.v_dot)
                        .setBackgroundResource(R.drawable.dot_normal);
                curIndex = position;
            }

            public void onPageScrolled(int arg0, float arg1, int arg2) {
            }

            public void onPageScrollStateChanged(int arg0) {
            }
        });

    以上就是Android开发中滑动圆点翻页的效果的实现,本文代码仅供参考,需要的朋友可以了解看看,希望对大家学习Android开发有帮助,想要了解更多可以继续浏览群英网络其他相关的文章。

文本转载自脚本之家

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:mmqy2019@163.com进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。

相关信息推荐
2022-07-07 17:26:18 
摘要:这篇文章主要介绍了Golang通脉之数据类型,在编程语言中标识符就是定义的具有某种意义的词,比如变量名、常量名、函数名等等,Go语言中标识符允许由字母数字和_(下划线)组成,并且只能以字母和_开头,更详细内容请看下面文章吧
2022-10-13 17:41:10 
摘要:c编译程序又称c语言编译器,是指用c语言书写的源程序,翻译成等价的机器语言格式目标程序的翻译程序。c编译程序首先会检查源程序的正确性,并把它分解成若干基本成分;然后根据这些基本成分建立相应等价的目标程序部分。
2022-07-23 17:47:29 
摘要:本篇文章带大家了解一下PHP 8 中的 JIT,并聊聊JIT 是怎么参与解释流程的,希望对大家有所帮助!
云活动
推荐内容
热门关键词
热门信息
群英网络助力开启安全的云计算之旅
立即注册,领取新人大礼包
  • 联系我们
  • 24小时售后:4006784567
  • 24小时TEL :0668-2555666
  • 售前咨询TEL:400-678-4567

  • 官方微信

    官方微信
Copyright  ©  QY  Network  Company  Ltd. All  Rights  Reserved. 2003-2019  群英网络  版权所有   茂名市群英网络有限公司
增值电信经营许可证 : B1.B2-20140078   粤ICP备09006778号
免费拨打  400-678-4567
免费拨打  400-678-4567 免费拨打 400-678-4567 或 0668-2555555
微信公众号
返回顶部
返回顶部 返回顶部