public class HomeActivity extends AppCompatActivity {

    public ViewPager mVpager;
    public ArrayList<View> mAList;
    private MyPagerAdapter mAdapter;
    private LinearLayout mRLayout1;
    private LinearLayout mRLayout2;
    private LinearLayout mRLayout3;
    private TextView mHomeTextView1;
    private TextView mHomeTextView2;
    private TextView mHomeTextView3;
    private TextView mTvUser;
    private ImageView mImageView1;
    private ImageView mImageView2;
    private ImageView mImageView3;
    int i;
    private String mUserCode;
    private String mUserName;

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

        //TextView etVersionName = findViewById(R.id.versionName);
        //etVersionName.append(ApkVersion.getVerName(this));
        //权限申请
        if (ContextCompat.checkSelfPermission(HomeActivity.this, android.Manifest.
                permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(HomeActivity.this, new
                    String[]{android.Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
        }

        //本地读取登录用户名--测试时关掉
        SharedPreferences pref = getSharedPreferences("LoginData", MODE_PRIVATE);
        mUserCode = pref.getString("userCode", "");
        mUserName = pref.getString("userName", "");
        mTvUser = findViewById(R.id.user);
        mVpager = findViewById(R.id.viewPage);
        mAList = new ArrayList<View>();
        LayoutInflater li = getLayoutInflater();
        mAList.add(li.inflate(R.layout.home_vpager1, null, false));
        mAList.add(li.inflate(R.layout.home_vpager2, null, false));
        mAList.add(li.inflate(R.layout.home_vpager3, null, false));
        mAdapter = new MyPagerAdapter(mAList);
        mVpager.setAdapter(mAdapter);
        mVpager.setCurrentItem(1);
        //获取控件id
        mRLayout1 = findViewById(R.id.homeLayout1);
        mRLayout1.setOnClickListener(new rLayout1ClickListener());
        mRLayout2 = findViewById(R.id.homeLayout2);
        mRLayout2.setOnClickListener(new rLayout2ClickListener());
        mRLayout3 = findViewById(R.id.homeLayout3);
        mRLayout3.setOnClickListener(new rLayout3ClickListener());
        mHomeTextView1 = findViewById(R.id.tView1);
        mHomeTextView2 = findViewById(R.id.tView2);
        mHomeTextView3 = findViewById(R.id.tView3);
        mImageView1 = findViewById(R.id.imageView1);
        mImageView2 = findViewById(R.id.imageView2);
        mImageView3 = findViewById(R.id.imageView3);
        //默认主页选中色
        slideChangeColor(1);
        //滑动监听
        mVpager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageSelected(int arg0) {
                // arg0是当前选中的页面的Position
                //Log.e(TAG, "onPageSelected------>"+arg0);
            }
            @Override
            public void onPageScrolled(int arg0, float arg1, int arg2) {
                // arg0 :当前页面,及你点击滑动的页面;arg1:当前页面偏移的百分比;arg2:当前页面偏移的像素位置
                //Log.e(TAG, "onPageScrolled------>arg0:"+arg0+"\nonPageScrolled------>arg1:"+arg1+"\nonPageScrolled------>arg2:"+arg2);
            }
            @Override
            public void onPageScrollStateChanged(int arg0) {
                i = mVpager.getCurrentItem();
                //arg0 ==1的时表示正在滑动,arg0==2的时表示滑动完毕了,arg0==0的时表示什么都没做。
                if (arg0 == 0) {
                    //Log.e(TAG, "onPageScrollStateChanged------>0");
                } else if (arg0 == 1) {
                    //Log.e(TAG, "onPageScrollStateChanged------>1");
                } else if (arg0 == 2) {
                    slideChangeColor(i);
                }
            }
        });
    }

    //点击底部各Layout变色
    class rLayout1ClickListener implements View.OnClickListener {
        @Override
        public void onClick(View v) {
            mVpager.setCurrentItem(0);
            //根据资源文件读取颜色
            slideChangeColor(0);
        }
    }
    class rLayout2ClickListener implements View.OnClickListener {
        @Override
        public void onClick(View v) {
            mVpager.setCurrentItem(1);
            slideChangeColor(1);
        }
    }
    class rLayout3ClickListener implements View.OnClickListener {
        @Override
        public void onClick(View v) {
            mVpager.setCurrentItem(2);
            slideChangeColor(2);
            mTvUser.setText(mUserCode + " " + mUserName);
        }
    }

    //滑动底部图片和文字变色
    private void slideChangeColor(int i) {
        if (i == 0) {
            mHomeTextView1.setTextColor(Color.parseColor("#1296db"));
            mImageView1.setImageDrawable(getResources().getDrawable(R.drawable.volume2));
            mHomeTextView2.setTextColor(Color.parseColor("#cdcdcd"));
            mImageView2.setImageDrawable(getResources().getDrawable(R.drawable.home1));
            mHomeTextView3.setTextColor(Color.parseColor("#cdcdcd"));
            mImageView3.setImageDrawable(getResources().getDrawable(R.drawable.person1));
        } else if (i == 1) {
            mHomeTextView2.setTextColor(Color.parseColor("#1296db"));
            mImageView2.setImageDrawable(getResources().getDrawable(R.drawable.home2));
            mHomeTextView1.setTextColor(Color.parseColor("#cdcdcd"));
            mImageView1.setImageDrawable(getResources().getDrawable(R.drawable.volume1));
            mHomeTextView3.setTextColor(Color.parseColor("#cdcdcd"));
            mImageView3.setImageDrawable(getResources().getDrawable(R.drawable.person1));
        } else if (i == 2) {
            mHomeTextView3.setTextColor(Color.parseColor("#1296db"));
            mImageView3.setImageDrawable(getResources().getDrawable(R.drawable.person2));
            mHomeTextView2.setTextColor(Color.parseColor("#cdcdcd"));
            mImageView2.setImageDrawable(getResources().getDrawable(R.drawable.home1));
            mHomeTextView1.setTextColor(Color.parseColor("#cdcdcd"));
            mImageView1.setImageDrawable(getResources().getDrawable(R.drawable.volume1));
        }
    }
}