27
2017
09

ViewPager+Fragment(小圆点)

package test.bwie.com.myyuekao1;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.widget.ImageView;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

private ImageView[] images;
private ViewPager mVp;

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


}

private void initView() {
    mVp = (ViewPager) findViewById(R.id.vp);

    ImageView i1 = (ImageView) findViewById(R.id.img1);
    ImageView i2= (ImageView) findViewById(R.id.img2);

    images  = new ImageView[]{i1,i2};
    mVp.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {
        @Override
        public Fragment getItem(int position) {
            Fragment f =null;

            switch (position){
                case 0:f = new Fragment1();
                    break;
                case 1:f = new Fragment2();
            }
            return f;
        }

        @Override
        public int getCount() {
            return 2;
        }
    });



    mVp.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

        }

        @Override
        public void onPageSelected(int position) {
            for(int i = 0 ; i<images.length; i ++){
                if(i==position) {
                    images[i].setImageResource(R.mipmap.point_selected);
                }else{
                    images[i].setImageResource(R.mipmap.point_mormal);
                }
            }
        }

        @Override
        public void onPageScrollStateChanged(int state) {

        }
    });
}

}
这里写图片描述

上一篇:APP登录界面(网页仿制版) 下一篇:STC89C51单片机串口程序下载失败总结