Viewpager与小圆点

//activity_main.xml布局

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:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.bwie.dongshaohua0913.MainActivity">

    <android.support.v4.view.ViewPager
        android:layout_width="match_parent"
        android:layout_height="300dp"
        android:id="@+id/viewpager"/>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
         android:text="Hello World!"
        android:textColor="@android:color/holo_red_dark"
        android:id="@+id/textview"/>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:id="@+id/linear"
        android:gravity="center"
        android:layout_alignBottom="@+id/viewpager">LinearLayout>
RelativeLayout>

//layout_item.xml

xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
  <View
      android:layout_width="8dp"
      android:layout_height="8dp"
      android:layout_marginLeft="3dp"
      android:id="@+id/view"
      android:background="@drawable/shape2"/>
LinearLayout>

//shape1.xml 自定义小圆点

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

shape>

//MainActivity


public class MainActivity extends AppCompatActivity {
    LinearLayout linear;
    ViewPager pager;
    TextView text;
    List imglist=new ArrayList();
    List  viewlist=new ArrayList();
    List strlist=new ArrayList();
    int imgcount=0;
    int count=0;
    List list;
    Handler hanlder=new Handler()
    {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            pager.setCurrentItem(imgcount);
            text.setText(strlist.get((imgcount%strlist.size()))+"");
            viewlist.get(count).setBackgroundResource(R.drawable.shape1);
            viewlist.get(imgcount%viewlist.size()).setBackgroundResource(R.drawable.shape2);

             count=imgcount%viewlist.size();
        }
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //初始化控件获取id
        linear= (LinearLayout) findViewById(R.id.linear);
        pager= (ViewPager) findViewById(R.id.viewpager);
        text= (TextView) findViewById(R.id.textview);
        //getImg();
        new AsyncTask()
        {

            @Override
            protected String doInBackground(String... strings) {

                String gsonstr=new NewUtils().getGson();

                return gsonstr;
            }

            @Override
            protected void onPostExecute(String s) {
                super.onPostExecute(s);
                Gson gson=new Gson();
                Bean bean=gson.fromJson(s,Bean.class);
                list=bean.getNewslist(); 
                  getImg();//加载图片
                  GetYn();//加载小圆点
Log. i( "TAG", viewlist.size()+ ""); pager.setAdapter( new MyAdapter()); //int a=Integer.MAX_VALUE/2-Integer.MAX_VALUE/2%imglist.size(); pager.setCurrentItem( 1000); viewlist.get( 0).setBackgroundResource(R.drawable. shape1); Timer timer= new Timer(); timer.schedule( new TimerTask() { @Override public void run() { imgcount= pager.getCurrentItem()+ 1; hanlder.sendEmptyMessage( 0); } }, 2000, 3000); } }.execute(); } public class MyAdapter extends PagerAdapter { @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((View) object); } @Override public Object instantiateItem(ViewGroup container, int position) { ImageView iview = imglist.get(position % imglist.size()); container.addView(iview); return iview; }} //加载图片 public void getImg() { for ( int i= 0;i< list.size();i++) { ImageView imageView= new ImageView(MainActivity. this); ImageLoader. getInstance().displayImage( list.get(i).getPicUrl(),imageView); imglist.add(imageView); } } //加载小圆点 public void GetYn() { for ( int i= 0;i< imglist.size();i++) { View view= LayoutInflater. from(MainActivity. this).inflate(R.layout. layout_item, null); View v=view.findViewById(R.id. view); viewlist.add(view); linear.addView(view); } }}
 
  
//

你可能感兴趣的:(Viewpager与小圆点)