zoukankan      html  css  js  c++  java
  • ViewPager的使用

      在许多APP中经常会看到许多图片的滑动显示,是使用ViewPager的使用,我做了一个小Demo实现它的应用,有什么的不对希望读者留评论指正。示意图如下

    通过滑动图片,小红点也随着移动。首先先找几个图片作为显示:

    将图片放入集合之中:

    写ViewPager的适配器,需要另外添加两个方法。

    因为是无限循环向右滑动,所以:

    getCount要返回最大值。这样就实现了图片的无限向右滑动效果,但是还需要显示圆点呢,由于圆点的个数和图片是对应的 ,所以在刚开始的集合中加入圆点:

    圆点与圆点之间是有间隔的,设置为10dp.在显示图片时,在显示时,第一个设置为红点的。随着图片的转移,红点也要跟着转移,这就需要在ViewPager的滑动监听时候去写:

    mVp_view.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
    private ImageView mChildAt;

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
    }
    @Override
    public void onPageSelected(int position) {
    currentPage = position;
    ImageView mChildAtZero = (ImageView) mLl_point.getChildAt(0);
    ImageView mChildAtOne = (ImageView) mLl_point.getChildAt(1);
    ImageView mChildAtTwo = (ImageView) mLl_point.getChildAt(2);
    ImageView mChildAtThree = (ImageView) mLl_point.getChildAt(3);
    int x = position%4;
    Log.d("TAG","X="+x);
    if (x==0){
    mChildAtZero.setImageResource(R.drawable.shape_point_change);
    mChildAtOne.setImageResource(R.drawable.shape_point);
    mChildAtTwo.setImageResource(R.drawable.shape_point);
    mChildAtThree.setImageResource(R.drawable.shape_point);
    }else if (x==1){
    mChildAtZero.setImageResource(R.drawable.shape_point);
    mChildAtOne.setImageResource(R.drawable.shape_point_change);
    mChildAtTwo.setImageResource(R.drawable.shape_point);
    mChildAtThree.setImageResource(R.drawable.shape_point);
    }else if (x==2){
    mChildAtZero.setImageResource(R.drawable.shape_point);
    mChildAtOne.setImageResource(R.drawable.shape_point);
    mChildAtTwo.setImageResource(R.drawable.shape_point_change);
    mChildAtThree.setImageResource(R.drawable.shape_point);
    }else {
    mChildAtZero.setImageResource(R.drawable.shape_point);
    mChildAtOne.setImageResource(R.drawable.shape_point);
    mChildAtTwo.setImageResource(R.drawable.shape_point);
    mChildAtThree.setImageResource(R.drawable.shape_point_change);
    }
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }
    });

    滑动监听用addOnPageChangeListener代替之前过时的setOnPageChangeListener的这个方法。在进行图片改变时候,去变换原点的位置。这样就可以实现效果图了。

  • 相关阅读:
    UnicodeDecodeError: 'utf-8' codec can't decode byte 0x93 in position 23: invalid start byte
    cat1标准模组固件开发记录
    cmake 编译出现错误 Could NOT find Threads (missing: Threads_FOUND)
    TMC4361+TMC2130闭环控制带编码器步进电机
    《从优秀到卓越》 吉姆 柯林斯,书读后笔记
    编译MT7621的HTTP通讯信代码
    wireshark分析wifi加密报文
    利用 C# 给 Windows 资源管理器注册右键菜单(Windows Shell)(一):入门
    关于 C# 中 string 类、List 集合的 IndexOf 方法区分大小写的解决方案
    一个简单的利用 WebClient 异步下载的示例(五)(完结篇)
  • 原文地址:https://www.cnblogs.com/zpfwin/p/7045299.html
Copyright © 2011-2022 走看看