本文整理汇总了Java中android.support.v4.app.FragmentTransaction类的典型用法代码示例。如果您正苦于以下问题:Java FragmentTransaction类的具体用法?Java FragmentTransaction怎么用?Java FragmentTransaction使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
FragmentTransaction类属于android.support.v4.app包,在下文中一共展示了FragmentTransaction类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: setCurrentItem
import android.support.v4.app.FragmentTransaction; //导入依赖的package包/类
public void setCurrentItem(int itemId) {
String name = makeFragmentName(containerId, itemId);
Fragment fragment = manager.findFragmentByTag(name);
FragmentTransaction fragmentTransaction = manager.beginTransaction();
if (fragment != null) {
fragmentTransaction.attach(fragment);
if (fragment.isHidden()) {
fragmentTransaction.show(fragment);
}
} else {
fragment = getItem(itemId);
fragmentTransaction.add(containerId, fragment, name);
}
if (fragment != current && current != null) {
fragmentTransaction.hide(current);
}
current = fragment;
fragmentTransaction.commit();
}
示例2: displayBooks
import android.support.v4.app.FragmentTransaction; //导入依赖的package包/类
/**
* Show fragments listing books.
* @param addToBackStack add to back stack or not
*/
public static void displayBooks(FragmentManager fragmentManager, boolean addToBackStack) {
if (isFragmentDisplayed(fragmentManager, BooksFragment.FRAGMENT_TAG) != null) {
return;
}
Fragment fragment = BooksFragment.getInstance();
FragmentTransaction t = fragmentManager
.beginTransaction()
.setTransition(FRAGMENT_TRANSITION)
.replace(R.id.single_pane_container, fragment, BooksFragment.FRAGMENT_TAG);
if (addToBackStack) {
t.addToBackStack(null);
}
t.commit();
}
示例3: selectFragment
import android.support.v4.app.FragmentTransaction; //导入依赖的package包/类
/**选择并显示fragment
* @param position
*/
public void selectFragment(int position) {
if (currentPosition == position) {
if (needReload == false && fragments[position] != null && fragments[position].isVisible()) {
Log.w(TAG, "selectFragment currentPosition == position" +
" >> fragments[position] != null && fragments[position].isVisible()" +
" >> return; ");
return;
}
}
if (needReload || fragments[position] == null) {
fragments[position] = getFragment(position);
}
//全局的fragmentTransaction因为already committed 崩溃
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.hide(fragments[currentPosition]);
if (fragments[position].isAdded() == false) {
fragmentTransaction.add(R.id.flBaseTabFragmentContainer, fragments[position]);
}
fragmentTransaction.show(fragments[position]).commit();
this.currentPosition = position;
}
示例4: showFragmentV11
import android.support.v4.app.FragmentTransaction; //导入依赖的package包/类
private void showFragmentV11(@IdRes int containerId, String showTag, String hideTag) {
android.app.FragmentManager fragmentManager = getFragmentManager();
android.app.FragmentTransaction transaction = fragmentManager.beginTransaction();
AnimationHelper.setUpRotate3dAnimator(transaction);
android.app.Fragment showFragment = fragmentManager.findFragmentByTag(showTag);
if (showFragment == null) {
showFragment = SampleFragmentV11.newInstance("SampleFragmentV11\n" + showTag);
transaction.add(containerId, showFragment, showTag);
} else {
transaction.show(showFragment);
}
android.app.Fragment hideFragment = fragmentManager.findFragmentByTag(hideTag);
if (hideFragment != null) {
transaction.hide(hideFragment);
}
transaction.commit();
}
示例5: initCitySelect
import android.support.v4.app.FragmentTransaction; //导入依赖的package包/类
private void initCitySelect(){
citySelectFragment = CitySelectFragment.newInstance(new CitySelectFragment.OnSelectCity() {
@Override
public void onSelectCity(String disCode) {
districtCode = disCode;
pullToRefreshLayout.showView(ViewStatus.LOADING_STATUS);
if(list.isEmpty()){
pullToRefreshLayout.showView(ViewStatus.LOADING_STATUS);
}
getP().refresh(searchText,disCode);
}
});
FragmentManager fm = getSupportFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.add(R.id.city_fragment_container, citySelectFragment);
ft.commit();
}
示例6: onTabClicked
import android.support.v4.app.FragmentTransaction; //导入依赖的package包/类
/**
* onTabClicked
*
* @param view
*/
public void onTabClicked(View view) {
switch (view.getId()) {
case R.id.btn_conversation:
index = 0;
break;
case R.id.btn_address_list:
index = 1;
break;
case R.id.btn_setting:
index = 2;
break;
}
if (currentTabIndex != index) {
FragmentTransaction trx = getSupportFragmentManager().beginTransaction();
trx.hide(fragments[currentTabIndex]);
if (!fragments[index].isAdded()) {
trx.add(R.id.fragment_container, fragments[index]);
}
trx.show(fragments[index]).commit();
}
mTabs[currentTabIndex].setSelected(false);
// set current tab as selected.
mTabs[index].setSelected(true);
currentTabIndex = index;
}
示例7: hackRecreateFragment
import android.support.v4.app.FragmentTransaction; //导入依赖的package包/类
/**
* Trick:
* Manually recreate fragment when corresponding data changed
*/
public void hackRecreateFragment(int position) {
final FragmentTransaction transaction = mFragmentManager.beginTransaction();
final String tag = makeFragmentTag(mContainerId, getItemId(position));
final Fragment oldFrg = mFragmentManager.findFragmentByTag(tag);
if (oldFrg != null) {
final Fragment newFrg = getItem(position);
transaction.remove(oldFrg);
transaction.add(mContainerId, newFrg, tag);
if (oldFrg != mCurrentPrimaryItem) {
newFrg.setMenuVisibility(false);
newFrg.setUserVisibleHint(false);
} else {
newFrg.setMenuVisibility(true);
newFrg.setUserVisibleHint(true);
}
}
transaction.commitNowAllowingStateLoss();
}
示例8: addFragment
import android.support.v4.app.FragmentTransaction; //导入依赖的package包/类
/**
* 添加下一个Fragment,隐藏当前的Fragment
* @param frameLayoutId
* @param fragment
*/
protected void addFragment(int frameLayoutId,Fragment fragment) {
if (fragment != null) {
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
if (fragment.isAdded()) {
if (mFragment != null) {
transaction.hide(mFragment).show(fragment);
} else {
transaction.show(fragment);
}
} else {
if (mFragment != null) {
transaction.hide(mFragment).add(frameLayoutId, fragment);
} else {
transaction.add(frameLayoutId, fragment);
}
}
mFragment = fragment;
transaction.commit();
}
}
示例9: switchFragment
import android.support.v4.app.FragmentTransaction; //导入依赖的package包/类
private void switchFragment(NavButton oldNavButton, NavButton selectNav) {
FragmentTransaction transaction = mFragmentManager.beginTransaction();
if (oldNavButton != null) {
if (oldNavButton.getFragment() != null) {
transaction.detach(oldNavButton.getFragment());
}
}
if (selectNav != null) {
if (selectNav.getFragment() != null) {
transaction.attach(selectNav.getFragment());
} else {
// Fragment.instantiate工具方法,再也不用自己newInstance().
Fragment fragment = Fragment.instantiate(mContext, selectNav.getClx().getName(), null);
transaction.add(mContainerId, fragment, selectNav.getNavBtnTag());
selectNav.setFragment(fragment);
}
}
transaction.commit();
}
示例10: showAPIDrawer
import android.support.v4.app.FragmentTransaction; //导入依赖的package包/类
/**
* Toggle API drawer
*/
public void showAPIDrawer(boolean b) {
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.setCustomAnimations(R.anim.slide_in_left, R.anim.slide_out_left, R.anim.slide_out_left,
R.anim.slide_out_left);
if (b) {
webviewFragment = new APIWebviewFragment();
Bundle bundle = new Bundle();
bundle.putString("url", "http://localhost:8585/reference.html");
webviewFragment.setArguments(bundle);
ft.add(R.id.fragmentWebview, webviewFragment).addToBackStack(null);
editorFragment.getView().animate().translationX(-50).setDuration(500).start();
} else {
editorFragment.getView().animate().translationX(0).setDuration(500).start();
ft.remove(webviewFragment);
}
ft.commit();
}
示例11: showFragment
import android.support.v4.app.FragmentTransaction; //导入依赖的package包/类
protected void showFragment(int containerId, Fragment from, Fragment to, String tag) {
FragmentManager supportFragmentManager = getSupportFragmentManager();
FragmentTransaction transaction = supportFragmentManager.beginTransaction();
if (!to.isAdded()) { // 先判断是否被add过
if (tag != null) {
transaction.hide(from).add(containerId, to, tag);
} else {
transaction.hide(from).add(containerId, to);
}
// 隐藏当前的fragment,add下一个到Activity中
} else {
transaction.hide(from).show(to); // 隐藏当前的fragment,显示下一个
}
transaction.commit();
}
示例12: clearOldFragment
import android.support.v4.app.FragmentTransaction; //导入依赖的package包/类
@SuppressWarnings("RestrictedApi")
private void clearOldFragment() {
FragmentTransaction transaction = mFragmentManager.beginTransaction();
List<Fragment> fragments = mFragmentManager.getFragments();
if (transaction == null || fragments == null || fragments.size() == 0)
return;
boolean doCommit = false;
for (Fragment fragment : fragments) {
if (fragment != this && fragment != null) {
transaction.remove(fragment);
doCommit = true;
}
}
if (doCommit)
transaction.commitNow();
}
示例13: restoreValuesInit
import android.support.v4.app.FragmentTransaction; //导入依赖的package包/类
private void restoreValuesInit(List<RecyclerViewItem> items) {
ButtonView2 bv = new ButtonView2();
bv.setSummary(getString(R.string.restore_summary));
bv.setButtonText(getString(R.string.restore_button));
bv.setOnItemClickListener(new ButtonView2.OnItemClickListener(){
@Override
public void onClick(RecyclerViewItem item) {
Calibration.restoreCalibrationStockValues(getActivity());
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.replace(R.id.content_frame, new ScreenFragment()).commit();
}
});
items.add(bv);
}
示例14: switchFrag
import android.support.v4.app.FragmentTransaction; //导入依赖的package包/类
private void switchFrag(BaseFragment fragment){
setActionBarTitleAgain(fragment.getTitle());
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
if (!fragments.get(fragment)){
transaction.add(R.id.main_content, fragment);
fragments.put(fragment, true);
}
for (BaseFragment baseFragment : fragments.keySet()) {
if (baseFragment != null) transaction.hide(baseFragment);
}
transaction.show(fragment);
transaction.commit();
View.OnClickListener action_end = fragment.getMenuAction_end();
if (action_end != null){
initActionBar(ACTIONBAR_TYPE_CUSTOM, R.drawable.umi_ic_menu_white, R.drawable.ic_filter_list_white_24dp);
actionBarEndAction(action_end);
}else {
initActionBar(ACTIONBAR_TYPE_CUSTOM, R.drawable.umi_ic_menu_white, null);
}
}
示例15: onCreate
import android.support.v4.app.FragmentTransaction; //导入依赖的package包/类
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
| View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
getWindow().setStatusBarColor(Color.TRANSPARENT);
}
setContentView(R.layout.activity_main);
ButterKnife.bind(this);
setSupportActionBar(mToolbar);
ControlPanel.get().attachView(mLayoutPanel);
getSupportFragmentManager().beginTransaction()
.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE)
.replace(R.id.fragment_container, new HomeFragment())
.commit();
getSupportFragmentManager().addOnBackStackChangedListener(new FragmentManager.OnBackStackChangedListener() {
@Override
public void onBackStackChanged() {
if (getSupportFragmentManager().getBackStackEntryCount() == 0) {
ControlPanel.get().expand(mLayoutPanel);
}
}
});
}