码迷,mamicode.com
首页 > 移动开发 > 详细

Android广告轮播图实现

时间:2016-08-11 00:58:39      阅读:312      评论:0      收藏:0      [点我收藏+]

标签:

先看效果

技术分享

 

 

第一步,布局

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.xw.myviewpager.MainActivity">
<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="180dp">
<android.support.v4.view.ViewPager
    android:id="@+id/vp"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

</android.support.v4.view.ViewPager>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_alignParentBottom="true"
        android:orientation="horizontal"
        android:background="#66000000"
        android:layout_height="30dp">
        <LinearLayout
            android:id="@+id/point_container"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:layout_gravity="center"
            android:gravity="center"
           >

        </LinearLayout>
    </LinearLayout>
</RelativeLayout>
</RelativeLayout>

这里的point_container用来做小圆点的容器

 

二 圆点形状和状态选择器

bg_shape.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="oval">
    <corners android:radius="5dp"/>
    <solid android:color="#FFFFFF"/>
</shape>

bg_shape2.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
       android:shape="oval">
    <corners android:radius="5dp"/>
    <solid android:color="#808080"/>
</shape>

 

selector.xml

"1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_enabled="true" android:drawable="@drawable/bg_shape"/>
    <item android:state_enabled="false"  android:drawable="@drawable/bg_shape2"/>
</selector>

enable="false"是灰色圆点,当state_enabled="true"是白色圆点

 

三 MainActivity

import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity implements ViewPager.OnPageChangeListener{
    private ViewPager vp;
   private  int[] imas;
   private  LinearLayout ll;
   private ArrayList<ImageView> imaList;
    private Boolean flag;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //初始化布局
        initViews();
        //数据
        initData();

        //适配器
        initAdapter();
        flag=true;
        //实现自动轮播
        new Thread(new Runnable() {
            @Override
            public void run() {

                    while (flag) {
                        try {
                            Thread.sleep(2000);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                vp.setCurrentItem(vp.getCurrentItem() + 1);
                            }
                        });
                }
            }
        }).start();
    }

    private void initAdapter() {
        ll.getChildAt(0).setEnabled(true);
        vp.setAdapter(new MyAdapter());
        vp.setCurrentItem(Integer.MAX_VALUE/2+2);//这里以一个%5为0的数开始,这样打开默认会在第0个item显示
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        flag=false;
    }

    private void initData() {
    }

    private void initViews() {
        vp = (ViewPager) findViewById(R.id.vp);
        vp.setOnPageChangeListener(this);
      ll= (LinearLayout) findViewById(R.id.point_container);
        //图片数组
        imas = new int[]{R.drawable.a, R.drawable.b, R.drawable.c, R.drawable.d, R.drawable.e,};
        ImageView iv;
        imaList = new ArrayList<ImageView>();
        View pointview;
        for (int i = 0; i < imas.length; i++) {
            iv = new ImageView(this);
            iv.setImageResource(imas[i]);
            imaList.add(iv);

            //加小白点
            pointview =new View(this);
            pointview.setEnabled(false);
            pointview.setBackgroundResource(R.drawable.selector);
            LinearLayout.LayoutParams params =new LinearLayout.LayoutParams(15,15);
            //params.gravity= Gravity.CENTER;
            if(i!=0)
                params.leftMargin=10;
            ll.addView(pointview,
                   params );
        }

    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        for(int i=0;i<ll.getChildCount();i++){
            ll.getChildAt(i).setEnabled(false);
        }
            ll.getChildAt(position%5).setEnabled(true);//设置小白点
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }


    class MyAdapter extends PagerAdapter {
        @Override
        public Object instantiateItem(ViewGroup container, int position) {
           int newposition =position%5;//得到新位置
            ImageView imageView=imaList.get(newposition);
            container.addView(imageView);
            return imageView;//把View对象返回给框架,必须重写
        }




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

        }

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

        @Override //判断复用
        public boolean isViewFromObject(View view, Object object) {
            return view==object;
        }


    }
}

 

Android广告轮播图实现

标签:

原文地址:http://www.cnblogs.com/xurui1995/p/5759371.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!