轮播图小圆点

//xml中代码

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.day13_viewpager_lb.MainActivity">

    <android.support.v4.view.ViewPager
        android:id="@+id/view_pager"
        android:layout_width="match_parent"
        android:layout_height="200dp"></android.support.v4.view.ViewPager>

    <LinearLayout
        android:id="@+id/linear_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:gravity="center"
        android:layout_marginBottom="7dp"
        android:layout_alignBottom="@id/view_pager"
        >

    </LinearLayout>

</RelativeLayout>


主类


public class MainActivity extends AppCompatActivity {

    private ViewPager view_pager;
    private LinearLayout linear_layout;
    @SuppressLint("HandlerLeak")
    private Handler handler=new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            int i = view_pager.getCurrentItem();
            i++;
            setSelectedPoint(i%list.size());
            view_pager.setCurrentItem(i);
            sendEmptyMessageDelayed(0,1000);
        }
    };
    private List<ImageView> list_img;
    private List<String> list;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();

        list = new ArrayList<>();
        list.add("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1505151721118&di=649c9a43aed72fbc4d99ec1a031510c6&imgtype=0&src=http%3A%2F%2Fimg.zcool.cn%2Fcommunity%2F015c7d574b9f8f6ac72525aee98351.jpg");
        list.add("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1505151956771&di=0eb6f306991d24b67a13ceb336f80102&imgtype=0&src=http%3A%2F%2Fi0.hdslb.com%2Fbfs%2Farchive%2F00613def3f1beb7a35ae136341be2b589bc46a2d.jpg_320x200.jpg");
        list.add("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1505151847685&di=c7a4b5d08ec43fa629bcb690039a7629&imgtype=0&src=http%3A%2F%2Fattimg.dospy.com%2Fimg%2Fday_080625%2F20080625_2e91a10c444877e88827vri2ZKdGMvQo.jpg");
        list.add("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1505151825129&di=70bf74b87d8a15cb91a2d79f15ed0eaf&imgtype=0&src=http%3A%2F%2Fattimg.dospy.com%2Fimg%2Fday_081016%2F20081016_fee215664d5740e56c13E2YB8giERFEX.jpg");
        list.add("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1505746504&di=930c4d677a02328a142d6fa85ed14580&imgtype=jpg&er=1&src=http%3A%2F%2Fattimg.dospy.com%2Fimg%2Fday_090113%2F20090113_6ac58b42bea94f0b318e1B6BZb5lPZl5.jpg");

        initDoc();

        MyAdapter myAdapter = new MyAdapter(list, this);
        view_pager.setAdapter(myAdapter);

        handler.sendEmptyMessageDelayed(0,1000);
    }


    private void initView() {
        view_pager = (ViewPager) findViewById(R.id.view_pager);
        linear_layout = (LinearLayout) findViewById(R.id.linear_layout);
    }

    private void initDoc() {
        list_img = new ArrayList<>();

        for (int i=0;i<list.size();i++){
            ImageView imgpoint = new ImageView(this);
            imgpoint.setScaleType(ImageView.ScaleType.FIT_XY);
            if (i==0){
                imgpoint.setImageResource(R.drawable.red_shape);
            }else {
                imgpoint.setImageResource(R.drawable.w_shape);
            }

            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(40, 40);
            params.setMargins(0,0,20,0);
            linear_layout.addView(imgpoint,params);
            list_img.add(imgpoint);
        }
    }

    public void setSelectedPoint(int index){
        for (int i=0;i<list_img.size();i++){
            if (i==index){
                list_img.get(i).setImageResource(R.drawable.red_shape);
            }else {
                list_img.get(i).setImageResource(R.drawable.w_shape);
            }
        }
    }
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值