Android视图运行错误的标签

时间:2016-08-22 06:56:15

标签: android-fragments

如果我点击第二个标签,它会运行第三个标签的onCreateView()方法。我不想运行第三个Tab。因为在第三个Tab中似乎是Toast。没必要。

FragmentActivity调用了TestActivity

FragmentActivity:

public class TestActivity extends FragmentActivity {
private ViewPager _mViewPager;
private ViewPagerAdapter _adapter;

    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.test);
    setUpView();
    setTab();
}

    private void setUpView(){
        _mViewPager = (ViewPager) findViewById(R.id.viewPager);
        _adapter = new ViewPagerAdapter(_mViewPager.getContext(),getSupportFragmentManager());
        _mViewPager.setAdapter(_adapter);
        _mViewPager.setCurrentItem(0);
        _mViewPager.destroyDrawingCache();
        _mViewPager.clearAnimation();
}
}

ViewPagerAdapter类:

public class ViewPagerAdapter extends FragmentPagerAdapter {
private Context _context;

public ViewPagerAdapter(Context context, FragmentManager fm) {
    super(fm);
    _context=context;
}
@Override
public Fragment getItem(int position) {
    Fragment f = new Fragment();
    switch(position){
        case 0:
            f= Test1Activity.newInstance(_context);
            break;
        case 1:
            f= Test2Activity.newInstance(_context);
            break;
        case 2:
            f= Test3Activity.newInstance(_context);
            break;
    }
    return f;
}
@Override
public int getCount() {
    return 3;
}}

Test1Activity类:

public class Test1Activity extends Fragment {
public static Fragment newInstance(Context context) {
    Test1Activity f = new Test1Activity();
    return f;
}

 @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
    root = (ViewGroup) inflater.inflate(R.layout.viewpage_test1, null);
    root.destroyDrawingCache();
    root.clearAnimation();
}}

因此,如果我单击Tab Fragment中的Test1Activity,它将运行Test2Activity的onCreateView()方法。我该如何解决这个问题?

0 个答案:

没有答案