ViewPager无线轮播加Shape绘制小圆点

时间:2022-06-02 20:43:29
  • 主布局
<android.support.v4.view.ViewPager  android:id="@+id/main_vp" android:layout_width="fill_parent" android:layout_height="fill_parent" />
    <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:layout_alignParentBottom="true" android:gravity="center" android:layout_marginBottom="20dp" >
        <View android:id="@+id/view1" android:layout_width="10dip" android:layout_height="10dip" android:layout_marginLeft="5dp" android:layout_marginRight="5dp" />
        <View android:id="@+id/view2" android:layout_width="10dip" android:layout_height="10dip" android:layout_marginLeft="5dp" android:layout_marginRight="5dp" android:background="@drawable/view1" />
        <View android:id="@+id/view3" android:layout_width="10dip" android:layout_height="10dip" android:layout_marginLeft="5dp" android:layout_marginRight="5dp" android:background="@drawable/view1" />
    </LinearLayout>
  • View绘制圆点布局
//选中前
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval" >  
    <corners android:radius="5dip" />
    <solid android:color="#33ff00" />

</shape>
//选中后
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval" >  
    <corners android:radius="5dip" />
    <solid android:color="#33ff00" />

</shape>
  • 轮播子布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="100dp" android:layout_height="100dp" android:background="@drawable/ad" android:orientation="vertical" >
</LinearLayout>
  • MainActivity代码
public class MainActivity extends Activity {

    private List<View> list;
    private List<View> listDot;
    private ViewPager vp;

    private int oldposition = 0;
    private int currentItem;

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

        addLayout();
        info();
    }

    private void addLayout() {
        list = new ArrayList<View>();

        View v1=View.inflate(this, R.layout.vp1, null);
        View v2=View.inflate(this, R.layout.vp2, null);
        View v3=View.inflate(this, R.layout.vp3, null);

        v1.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Toast.makeText(MainActivity.this, "点击了...", 0).show();
            }
        });
        list.add(v1);
        list.add(v2);
        list.add(v3);
    }

    private void info() {
        vp = (ViewPager) findViewById(R.id.main_vp);
        View v1=findViewById(R.id.view1);
        View v2=findViewById(R.id.view2);
        View v3=findViewById(R.id.view3);
        listDot = new ArrayList<View>();

        listDot.add(v1);
        listDot.add(v2);
        listDot.add(v3);

        vp.setAdapter(new MyAdapter());
        vp.setOnPageChangeListener(new OnPageChangeListener() {

            @Override
            public void onPageSelected(int arg0) {
                // TODO Auto-generated method stub
                //修改上一个位置
                listDot.get(oldposition).setBackgroundResource(R.drawable.view1);
                //修改当前位置
                listDot.get(arg0).setBackgroundResource(R.drawable.view2);
                //赋值
                oldposition = arg0;
                currentItem = arg0;

            }

            @Override
            public void onPageScrolled(int arg0, float arg1, int arg2) {
                // TODO Auto-generated method stub

            }

            @Override
            public void onPageScrollStateChanged(int arg0) {
                // TODO Auto-generated method stub

            }
        });
    }
    class MyAdapter extends PagerAdapter{

        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return list.size();
        }

        @Override
        public boolean isViewFromObject(View arg0, Object arg1) {
            // TODO Auto-generated method stub
            return arg0 == arg1;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            // TODO Auto-generated method stub

            container.removeView(list.get(position));
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            // TODO Auto-generated method stub


            container.addView(list.get(position));
            return list.get(position);
        }

    }
    @Override
    protected void onStart() {
        // TODO Auto-generated method stub
        super.onStart();
        ScheduledExecutorService s = Executors.newSingleThreadScheduledExecutor();

        s.scheduleWithFixedDelay(new ViewPagerTask(), 2, 2, TimeUnit.SECONDS);

    }
     class ViewPagerTask implements Runnable{

        @Override
        public void run() {
            // TODO Auto-generated method stub
            currentItem = (currentItem+1) % list.size();
            handler.obtainMessage().sendToTarget();
        }
    }
     private Handler handler=new Handler(){
         public void handleMessage(android.os.Message msg) {
             vp.setCurrentItem(currentItem);
         }
     };
     protected void onStop() {
         super.onStop();
     };
}