zoukankan      html  css  js  c++  java
  • viewpager加fragment可滑动加radio跟随滑动

    public class MainActivity extends AppCompatActivity implements RadioGroup.OnCheckedChangeListener, ViewPager.OnPageChangeListener {
        private ArrayList<Fragment> arrayList;
        private FragmentManager manager;
        private RadioGroup radioGroup;
        private RadioButton[] radioButtons;
        private ViewPager viewPager;


        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            viewPager= (ViewPager) findViewById(R.id.main_vp);
            radioGroup= (RadioGroup) findViewById(R.id.main_rg);
            manager=getSupportFragmentManager();
            arrayList=new ArrayList<>();
            Fragment1 fragment1=new Fragment1();
            Fragment2 fragment2=new Fragment2();
            Fragment3 fragment3=new Fragment3();
            Fragment4 fragment4=new Fragment4();
            Fragment5 fragment5=new Fragment5();
            arrayList.add(fragment1);
            arrayList.add(fragment2);
            arrayList.add(fragment3);
            arrayList.add(fragment4);
            arrayList.add(fragment5);
            Main_Adapter adapter=new Main_Adapter(manager,arrayList);
            viewPager.setAdapter(adapter);
            radioButtons=new RadioButton[radioGroup.getChildCount()];
            for (int i = 0; i < radioButtons.length; i++) {
                radioButtons[i]= (RadioButton) radioGroup.getChildAt(i);
            }
            radioButtons[0].setChecked(true);
            radioGroup.setOnCheckedChangeListener(this);
            viewPager.setOnPageChangeListener(this);
        }

        @Override
        public void onCheckedChanged(RadioGroup radioGroup, int checkedId) {
            for (int j = 0; j < radioButtons.length; j++) {
                if (radioButtons[j].getId()==checkedId){
                    viewPager.setCurrentItem(j);
                }

            }
        }

        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

        }

        @Override
        public void onPageSelected(int position) {
            radioButtons[position].setChecked(true);
        }

        @Override
        public void onPageScrollStateChanged(int state) {

        }
    }

  • 相关阅读:
    邻接矩阵学习
    Spark on Yarn年度知识整理
    HBASE解析
    Storm的数据处理编程单元:Bolt 学习整理
    jsch ssh服务器调用Linux命令或脚本的小问题
    storm的数据源编程单元Spout学习整理
    Java 正则表达式详细使用
    【转】如何成为一个技术全面的架构师
    【转】Linux netstat命令详解,高级面试必备
    【转】Servlet 生命周期、工作原理
  • 原文地址:https://www.cnblogs.com/la66/p/7883585.html
Copyright © 2011-2022 走看看