[英]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. 与常规包相比,我仍然没有从支持包中得到
Fragment
, DialogFragment
和FragmentManager
等之间的区别。 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.