|
| 1 | +package com.github.xubo.ceilinglayout.sample; |
| 2 | + |
| 3 | +import android.graphics.Color; |
| 4 | +import android.support.v4.app.FragmentManager; |
| 5 | +import android.support.v4.view.ViewPager; |
| 6 | +import android.support.v7.app.ActionBar; |
| 7 | +import android.support.v7.app.AppCompatActivity; |
| 8 | +import android.os.Bundle; |
| 9 | +import android.util.DisplayMetrics; |
| 10 | +import android.util.Log; |
| 11 | +import android.widget.ImageView; |
| 12 | +import android.widget.RadioButton; |
| 13 | +import android.widget.RadioGroup; |
| 14 | + |
| 15 | +public class MainActivity extends AppCompatActivity { |
| 16 | + FragmentManager fragmentManager; |
| 17 | + ImageView main_image_iv; |
| 18 | + SliderRadioGroup main_srg; |
| 19 | + RadioButton main_tab1_rb; |
| 20 | + RadioButton main_tab2_rb; |
| 21 | + ViewPager main_content_vp; |
| 22 | + |
| 23 | + MyPagerAdapter myPagerAdapter; |
| 24 | + |
| 25 | + @Override |
| 26 | + protected void onCreate(Bundle savedInstanceState) { |
| 27 | + super.onCreate(savedInstanceState); |
| 28 | + setContentView(R.layout.activity_main); |
| 29 | + Utils.setColorLight(this, Color.WHITE); |
| 30 | + ActionBar actionBar = getSupportActionBar(); |
| 31 | + if (actionBar != null) { |
| 32 | + actionBar.hide(); |
| 33 | + } |
| 34 | + fragmentManager = getSupportFragmentManager(); |
| 35 | + |
| 36 | + //id |
| 37 | + main_image_iv = findViewById(R.id.main_image_iv); |
| 38 | + main_srg = findViewById(R.id.main_srg); |
| 39 | + main_tab1_rb = findViewById(R.id.main_tab1_rb); |
| 40 | + main_tab2_rb = findViewById(R.id.main_tab2_rb); |
| 41 | + main_content_vp = findViewById(R.id.main_content_vp); |
| 42 | + |
| 43 | + main_image_iv.getLayoutParams().height = Utils.getScreenWidth(this) * 540 / 1920; |
| 44 | + |
| 45 | + myPagerAdapter = new MyPagerAdapter(fragmentManager); |
| 46 | + main_content_vp.setAdapter(myPagerAdapter); |
| 47 | + main_content_vp.setOffscreenPageLimit(2); |
| 48 | + main_content_vp.addOnPageChangeListener(new ViewPager.OnPageChangeListener() { |
| 49 | + @Override |
| 50 | + public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { |
| 51 | + main_srg.move(position, positionOffset); |
| 52 | + } |
| 53 | + |
| 54 | + @Override |
| 55 | + public void onPageSelected(int position) { |
| 56 | + switch (position) { |
| 57 | + case 0: |
| 58 | + main_tab1_rb.setChecked(true); |
| 59 | + break; |
| 60 | + case 1: |
| 61 | + main_tab2_rb.setChecked(true); |
| 62 | + break; |
| 63 | + default: |
| 64 | + main_tab1_rb.setChecked(true); |
| 65 | + break; |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + @Override |
| 70 | + public void onPageScrollStateChanged(int state) { |
| 71 | + |
| 72 | + } |
| 73 | + }); |
| 74 | + main_srg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { |
| 75 | + @Override |
| 76 | + public void onCheckedChanged(RadioGroup group, int checkedId) { |
| 77 | + switch (checkedId) { |
| 78 | + case R.id.main_tab1_rb: |
| 79 | + main_content_vp.setCurrentItem(0); |
| 80 | + break; |
| 81 | + case R.id.main_tab2_rb: |
| 82 | + main_content_vp.setCurrentItem(1); |
| 83 | + break; |
| 84 | + default: |
| 85 | + main_content_vp.setCurrentItem(0); |
| 86 | + break; |
| 87 | + } |
| 88 | + } |
| 89 | + }); |
| 90 | + } |
| 91 | +} |
0 commit comments