用户注册



邮箱:

密码:

用户登录


邮箱:

密码:
记住登录一个月忘记密码?

发表随想


还能输入:200字
云代码 - android代码库

无限轮播

2017-02-05 作者: cwm1996举报

[android]代码库

//////////////////////////////////////////////
//黑色
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <corners android:radius="8dp"/>
    <solid android:color="#88000000"/>
</shape>
//////////////////////////////////////
//白色
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
<corners android:radius="8dp"/>
<solid android:color="#fff"/>
</shape>
///////////////////////////////////
XML中存放小圆点的控件
<LinearLayout
    android:id="@+id/line"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:layout_centerHorizontal="true"
    android:layout_alignBottom="@id/pager">
</LinearLayout>
////////////////////////////////////////////////////
代码:
package com.example.viewpager_infiniteplay;

import android.os.Handler;
import android.os.Message;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.example.adapter.Mypageadapter;
import com.lidroid.xutils.BitmapUtils;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    private ViewPager viewPager;
    //图片资源
    private String[] imageUrls = new String[] {
            "http://mimg.xmeise.com/thumb/m/allimg/160203/1-160203151139.jpg",
            "http://mimg.xmeise.com/thumb/m/allimg/160203/1-160203151143.jpg",
            "http://mimg.xmeise.com/thumb/m/allimg/160203/1-160203151146.jpg",
            "http://mimg.xmeise.com/thumb/m/allimg/160203/1-160203151150.jpg",
            "http://mimg.xmeise.com/thumb/m/allimg/160203/1-160203151155.jpg",
            "http://mimg.xmeise.com/thumb/m/allimg/160203/1-160203151200.jpg"};
    private List<ImageView> list;
    private LinearLayout line;
    private List<ImageView> imagelist;
    private Handler handler=new Handler(){
    @Override
    public void handleMessage(Message msg) {

        int currentItem = viewPager.getCurrentItem();
        currentItem++;
        viewPager.setCurrentItem(currentItem);
        handler.sendEmptyMessageDelayed(0,2000);

    }
};
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        viewPager = (ViewPager) findViewById(R.id.pager);
        line = (LinearLayout) findViewById(R.id.line);
        //获取图片
        getdate();
        //初始化小圆点
        initcircle();
        //适配数据
        Mypageadapter mypageadapter=new Mypageadapter(list,this,handler);
        viewPager.setAdapter(mypageadapter);
        //設置初始化的界面
        viewPager.setCurrentItem(list.size()*10000000);
        //發送延時操作
        handler.sendEmptyMessageDelayed(0,2000);
        //监听换页变化
        viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            }
            @Override
            public void onPageSelected(int position) {
                //遍历图片集合
                 for (int i=0;i<imagelist.size();i++){
                     //索引相同即为亮点
                     if (i==position%list.size()){
                         imagelist.get(i).setImageResource(R.drawable.bright);
                     }else{
                         imagelist.get(i).setImageResource(R.drawable.hidden);
                     }
                 }
            }
            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    private void initcircle() {
        //存放圆点
        imagelist = new ArrayList<>();
        for (int i=0;i<imageUrls.length;i++){
            ImageView imageView=new ImageView(this);
            //第一个默认为亮点
            if(i==0){
                imageView.setImageResource(R.drawable.bright);
            }else{
                imageView.setImageResource(R.drawable.hidden);
            }
            //默认圆点高宽20
            LinearLayout.LayoutParams params=new LinearLayout.LayoutParams(20,20);
            //设置间距(左上右下)
            params.setMargins(5,0,5,0);
            //放在容器中
            line.addView(imageView,params);
            imagelist.add(imageView);
        }
    }

    //初始化图片
    private void getdate() {
        list = new ArrayList<ImageView>();
        BitmapUtils bitmapUtils=new BitmapUtils(MainActivity.this);
        for (int i=0;i<imageUrls.length;i++){
        ImageView imageView=new ImageView(MainActivity.this);
            bitmapUtils.display(imageView,imageUrls[i]);
            list.add(imageView);
        }

    }
}
////////////////////////////////////////////////////////////////////////////////
适配器
package com.example.adapter;

import android.content.Context;
import android.os.Handler;
import android.support.v4.view.PagerAdapter;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.example.viewpager_infiniteplay.MainActivity;

import java.util.List;


public class Mypageadapter extends PagerAdapter {
    private Context context;
    private List<ImageView> list;
    private Handler handler;

    public Mypageadapter(List<ImageView> list, MainActivity context, Handler handler) {
        this.list = list;
        this.context = context;
        this.handler = handler;
    }

    @Override
    public int getCount() {
        return Integer.MAX_VALUE;
    }

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

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

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        ImageView imageView = list.get(position % list.size());
        //设置触摸监听
        imageView.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                switch (motionEvent.getAction()) {
                    //按下时停止轮播
                    case MotionEvent.ACTION_DOWN:
                        handler.removeCallbacksAndMessages(null);
                        break;
                    //移动时停止轮播
                    case MotionEvent.ACTION_MOVE:
                        handler.removeCallbacksAndMessages(null);
                        break;
                    //取消时开始轮播
                    case MotionEvent.ACTION_CANCEL:
                        handler.sendEmptyMessageDelayed(0,2000);
                        break;
                    //抬起时开始轮播
                    case MotionEvent.ACTION_UP:
                        handler.sendEmptyMessageDelayed(0,2000);
                        break;

                }
                //處理事物
                return true;
            }
        });
        container.addView(imageView);
        return imageView;
    }
}


网友评论    (发表评论)


发表评论:

评论须知:

  • 1、评论每次加2分,每天上限为30;
  • 2、请文明用语,共同创建干净的技术交流环境;
  • 3、若被发现提交非法信息,评论将会被删除,并且给予扣分处理,严重者给予封号处理;
  • 4、请勿发布广告信息或其他无关评论,否则将会删除评论并扣分,严重者给予封号处理。


扫码下载

加载中,请稍后...

输入口令后可复制整站源码

加载中,请稍后...