欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

fragment切换

程序员文章站 2022-05-14 21:13:20
...

TwoActivity

public class TwoActivity extends FragmentActivity {

private Button btn_zhuxiao;
private SharedPreferences sharedPreferences;
private ViewPager viewPager;
private TextView text_1;
private TextView text_2;
private TextView text_3;
private RadioGroup group;
private List<Fragment> list;

@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_two);
    btn_zhuxiao = (Button) findViewById(R.id.btn_zhuxiao);

    viewPager = (ViewPager) findViewById(R.id.viewPager);
    text_1 = (TextView) findViewById(R.id.text_1);
    text_2 = (TextView) findViewById(R.id.text_2);
    text_3 = (TextView) findViewById(R.id.text_3);
    group = (RadioGroup) findViewById(R.id.group);
    sharedPreferences = getSharedPreferences("1606B", MODE_PRIVATE);
    // 注销
    ZhuXIao();
    // 创建容器并添加
    list = new ArrayList<Fragment>();
    list.add(new AFragment());
    list.add(new BFragment());
    list.add(new CFragment());
    viewPager.setAdapter(new FragmentPagerAdapter(
            getSupportFragmentManager()) {

        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return list.size();
        }

        @Override
        public Fragment getItem(int arg0) {
            // TODO Auto-generated method stub
            return list.get(arg0);
        }
    });
    group.setOnCheckedChangeListener(new OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            // TODO Auto-generated method stub
            switch (checkedId) {
            case R.id.radio_1:
                viewPager.setCurrentItem(0);
                break;
            case R.id.radio_2:
                viewPager.setCurrentItem(1);
                break;
            case R.id.radio_3:
                viewPager.setCurrentItem(2);
                break;

            default:
                break;
            }
        }
    });
    viewPager.setOnPageChangeListener(new OnPageChangeListener() {

        @Override
        public void onPageSelected(int arg0) {
            // TODO Auto-generated method stub
            switch (arg0) {
            case 0:
                group.check(R.id.radio_1);
                text_1.setBackground(getResources().getDrawable(R.color.red));
                text_2.setBackground(getResources().getDrawable(R.color.green));
                text_3.setBackground(getResources().getDrawable(R.color.green));
                break;
            case 1:
                group.check(R.id.radio_2);
                text_1.setBackground(getResources().getDrawable(R.color.green));
                text_2.setBackground(getResources().getDrawable(R.color.red));
                text_3.setBackground(getResources().getDrawable(R.color.green));
                break;
            case 2:
                group.check(R.id.radio_3);
                text_1.setBackground(getResources().getDrawable(R.color.green));
                text_2.setBackground(getResources().getDrawable(R.color.green));
                text_3.setBackground(getResources().getDrawable(R.color.red));
                break;

            default:
                break;
            }
        }

        @Override
        public void onPageScrolled(int arg0, float arg1, int arg2) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onPageScrollStateChanged(int arg0) {
            // TODO Auto-generated method stub

        }
    });

}

/**
 * 注销
 */
private void ZhuXIao() {
    btn_zhuxiao.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            Editor editor = sharedPreferences.edit();
            editor.clear();
            editor.commit();
            startActivity(new Intent(TwoActivity.this, MainActivity.class));
            finish();
        }
    });
}

}


fragment切换
fragment切换
fragment切换