简体   繁体   English

被android.support.v4.app.FragmentActivity和android.app.FragmentTransaction混淆

[英]Confused by android.support.v4.app.FragmentActivity and android.app.FragmentTransaction

Im a little bit confused by the support package android.support.v4.app because i implemented a class extending the android.support.v4.app.FragmentActivity and noticed that it has methods which are expecting android.app.FragmentTransaction as a parameter. 我对支持包android.support.v4.app有点困惑,因为我实现了一个扩展android.support.v4.app.FragmentActivity的类,并注意到它具有将android.app.FragmentTransaction作为参数的方法。 Why arent they expecting android.support.v4.app.FragmentTransaction instead? 为什么他们不期待android.support.v4.app.FragmentTransaction呢?

I still didnt get the difference between Fragment , DialogFragment , FragmentManager etc. from the support package compared to the regular packages. 与常规包相比,我仍然没有从支持包中得到FragmentDialogFragmentFragmentManager等之间的区别。 When do i have to use what? 我什么时候必须使用什么?

I implemented for example a DialogFragment from the regular package and use it in all my acitvities when i have to make an alert to the user. 例如,我从常规包中实现了一个DialogFragment ,并在必须向用户发出警报时在所有活动中使用它。 Unfortunately now i have to write antother DialogFragment implementing the supertype from the support package, because i got some fragments which are using the implementations from the support package. 不幸的是,现在我不得不从支持包中编写另DialogFragment实现超类型的DialogFragment ,因为我得到了一些正在使用支持包中的实现的片段。 I cant change them because they arent from me. 我无法更改它们,因为它们不适合我。

public class DiagramsActivity extends FragmentActivity implements TabListener, OnPageChangeListener {
    private ViewPager viewPager = null;
    private FragmentPagerAdapter fragmentPager = null;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        this.fragmentPager = new DiagramsFragmentPagerAdapter(this.getSupportFragmentManager());

        this.setContentView(R.layout.activity_diagrams); 

        this.viewPager = (ViewPager) this.findViewById(R.id.diagrams_pager);
        this.viewPager.setOnPageChangeListener(this);
        this.viewPager.setAdapter(this.fragmentPager);
        this.actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
        this.actionBar.addTab(actionBar.newTab().setText(this.getResources().getString(R.string.text_line)).setTabListener(this));
        this.actionBar.addTab(actionBar.newTab().setText(this.getResources().getString(R.string.text_bar)).setTabListener(this));
        this.actionBar.addTab(actionBar.newTab().setText(this.getResources().getString(R.string.text_scatter)).setTabListener(this));
        this.actionBar.addTab(actionBar.newTab().setText(this.getResources().getString(R.string.text_pie)).setTabListener(this));
    }

    @Override
    public void onTabSelected(Tab tab, android.app.FragmentTransaction fragmentTransaction) {
        this.viewPager.setCurrentItem(tab.getPosition());
    }

    @Override
    public void onTabUnselected(Tab tab, android.app.FragmentTransaction fragmentTransaction) {}

    @Override
    public void onTabReselected(Tab tab, android.app.FragmentTransaction fragmentTransaction) {}

    @Override
    public void onPageScrollStateChanged(int state) {}

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

    @Override
    public void onPageSelected(int position) {
        this.actionBar.setSelectedNavigationItem(position);
    }

    private void performChooseDates() {
        FragmentTransaction fragmentTransaction = this.getSupportFragmentManager().beginTransaction();
        MeasureDataSelectionDialogFragment measureDataGetSelectionDialogFragment = new MeasureDataSelectionDialogFragment();

        measureDataGetSelectionDialogFragment.setCallback(this);
        measureDataGetSelectionDialogFragment.show(fragmentTransaction, null);
    }
}

The FragmentActivity is from the support package. FragmentActivity来自支持包。 The methods from the TabListener are expecting android.app.FragmentTransaction so does my method performChooseDates() . TabListener的方法都需要android.app.FragmentTransaction所以我的方法performChooseDates() So i have an (from my point of view) ugly mix of support package and regular package. 因此,从我的角度来看,我的支持包和常规包很难混合使用。 the Viewpager mentioned in the code and the FragmentPagerAdapter are also from the support package because they dont exist in the regular package. 代码中提到的Viewpager和FragmentPagerAdapter也来自支持包,因为它们不存在于常规包中。

All the apps I write need to support right back to Android 2.3 and this is the easiest way to do it. 我编写的所有应用都需要直接支持Android 2.3,这是最简单的方法。 If you're supporting 11+ then stick to android.app.Fragment . 如果您支持11+,请坚持使用android.app.Fragment

android.support.v4.app.Fragment is the Fragment class in the android support library, which is a compatibility package that allows you to use some of the newer features of Android on older versions of Android . android.support.v4.app.Fragment是android支持库中的Fragment类,它是一个兼容性软件包,允许您在旧版 Android上使用Android的某些新功能。 android.support.v4.app.Fragment is a library that you can use to get backwards-compatibility for older API version. android.support.v4.app.Fragment是一个库,您可以使用该库来获得旧版API的向后兼容性。

android.app.Fragment is the Fragment class in the native version of the Android SDK. android.app.Fragment是Android SDK本机版本中的Fragment类。 It was introduced in API 11 . 它是在API 11中引入的。 If you want to make your app use fragments, and want to target devices before API 11, you must use android.support.v4.app.Fragment . 如果要使您的应用使用片段,并希望在API 11之前定位设备,则必须使用android.support.v4.app.Fragment If you're only targeting devices running API 11 or above, you can use android.app.Fragment . 如果您仅定位运行API 11或更高版本的设备,则可以使用android.app.Fragment

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 android.support.v4.app.FragmentActivity扩展了什么? - What does android.support.v4.app.FragmentActivity extend? android.support.v4.app.FragmentActivity无法解析 - android.support.v4.app.FragmentActivity can not be resolved 无法将ContraceptiveFragment中的ContraceptiveFragment()应用于(android.support.v4.app.FragmentActivity) - ContraceptiveFragment( ) in ContraceptiveFragment cannot be applied to (android.support.v4.app.FragmentActivity) “TAG”在“android.support.v4.app.FragmentActivity”中有私有访问权限 - 'TAG' has private access in 'android.support.v4.app.FragmentActivity' “无法找到类'android.support.v4.app.FragmentActivity'”错误 - “Could not find class 'android.support.v4.app.FragmentActivity'” Error 无法解析构造函数'GridLayoutManager(android.support.v4.app.FragmentActivity) - Cannot resolve constructor 'GridLayoutManager(android.support.v4.app.FragmentActivity) android.support.v4.app.FragmentActivity缺少操作栏 - android.support.v4.app.FragmentActivity missing action bar 无法在ViewModelProviders中解析android.support.v4.app.FragmentActivity的方法 - Cannot resolve method of android.support.v4.app.FragmentActivity in ViewModelProviders 无法将clcik上的android.app.Application强制转换为android.support.v4.app.FragmentActivity - android.app.Application cannot be cast to android.support.v4.app.FragmentActivity on clcik Android Studio 0.2.2 + ActionBarSherlock设置:找不到类“ android.support.v4.app.FragmentActivity” - Android Studio 0.2.2 + ActionBarSherlock settings: Could not find class 'android.support.v4.app.FragmentActivity'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM