小圆点代码

 //小圆点一起动
        pager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {

                for (int i = 0; i < piont.size(); i++) {

                    if (i==position){
                        piont.get(i).setImageResource(R.drawable.shape_selected);
                    } else {
                        piont.get(i).setImageResource(R.drawable.shape_un);
                    }
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

    }
//加入小圆点
    private void addpiont() {
        for (int i = 0; i < 3; i++) {
             ImageView imageView = new ImageView(this);
            if (i == 0) {

                imageView.setImageResource(R.drawable.shape_selected);
            } else {
                imageView.setImageResource(R.drawable.shape_un);
            }
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
            params.setMargins(0, 0, 15, 0);
            lin_main.addView(imageView, params);
            piont.add(imageView);
        }

}

小圆点的页面代码

<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.zhonghe.MainActivity">

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

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

你可能感兴趣的:(小圆点代码)