use of android.support.v4.app.FragmentManager in project GT by Tencent.
the class GTMainActivity method setTabSelection.
/**
* 根据传入的index参数来设置选中的tab页。
*
* @param index
* 每个tab页对应的下标。
*/
private synchronized void setTabSelection(int index) {
// 每次选中之前先清楚掉上次的选中状态
clearSelection();
// 开启一个Fragment事务
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction transaction = fragmentManager.beginTransaction();
// 先隐藏掉所有的Fragment,以防止有多个Fragment显示在界面上的情况
//TODO
hideFragments(transaction);
switch(index) {
case 0:
// 当点击了AUT tab时,改变控件的图片和文字颜色
autImage.setImageResource(R.drawable.tab_selected_border);
autText.setTextColor(Color.WHITE);
if (autFragment == null) {
autFragment = new GTAUTFragment();
transaction.add(R.id.content, autFragment, "a");
} else {
transaction.show(autFragment);
}
break;
case 1:
// 当点击了参数 tab时,改变控件的图片和文字颜色
paramImage.setImageResource(R.drawable.tab_selected_border);
paramText.setTextColor(Color.WHITE);
if (paramFragment == null) {
paramFragment = new GTParamTopFragment();
transaction.add(R.id.content, paramFragment, "b");
} else {
transaction.show(paramFragment);
paramFragment.onShow(true);
}
break;
case 2:
// 当点击了耗时 tab时,改变控件的图片和文字颜色
perfImage.setImageResource(R.drawable.tab_selected_border);
perfText.setTextColor(Color.WHITE);
if (perfFragment == null) {
perfFragment = new GTPerfFragment();
transaction.add(R.id.content, perfFragment, "c");
} else {
transaction.show(perfFragment);
}
break;
case 3:
// 当点击了日志 tab时,改变控件的图片和文字颜色
logImage.setImageResource(R.drawable.tab_selected_border);
logText.setTextColor(Color.WHITE);
if (logFragment == null) {
logFragment = new GTLogFragment();
transaction.add(R.id.content, logFragment, "d");
} else {
transaction.show(logFragment);
}
break;
case 4:
default:
// 当点击了插件 tab时,改变控件的图片和文字颜色
pluginImage.setImageResource(R.drawable.tab_selected_border);
pluginText.setTextColor(Color.WHITE);
if (pluginFragment == null) {
pluginFragment = new GTPluginFragment();
transaction.add(R.id.content, pluginFragment, "e");
} else {
transaction.show(pluginFragment);
}
break;
}
/*
* 直接使用commit()可能会出错:
* IllegalStateException: Can not perform this action after onSaveInstanceState:\
*
* @see http://developer.android.com/reference/android/app/FragmentTransaction.html#commitAllowingStateLoss()
*
* 大致意思是说我使用的 commit方法是在Activity的onSaveInstanceState()之后调用的,这样会出错,因为onSaveInstanceState
* 方法是在该Activity即将被销毁前调用,来保存Activity数据的,如果在保存玩状态后再给它添加Fragment就会出错。解决办法就
* 是把commit()方法替换成 commitAllowingStateLoss()就行了,其效果是一样的。
*/
// transaction.commit();
transaction.commitAllowingStateLoss();
}
use of android.support.v4.app.FragmentManager in project GT by Tencent.
the class GTMainActivity method removeFragments.
/*
* 在Activity重新加载时,要先清除遗留的Fragment
*/
private void removeFragments() {
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction transaction = fragmentManager.beginTransaction();
Fragment fragment = fragmentManager.findFragmentByTag("a");
if (fragment != null) {
transaction.remove(fragment);
}
fragment = fragmentManager.findFragmentByTag("b");
if (fragment != null) {
transaction.remove(fragment);
}
fragment = fragmentManager.findFragmentByTag("c");
if (fragment != null) {
transaction.remove(fragment);
}
fragment = fragmentManager.findFragmentByTag("d");
if (fragment != null) {
transaction.remove(fragment);
}
fragment = fragmentManager.findFragmentByTag("e");
if (fragment != null) {
transaction.remove(fragment);
}
transaction.commitAllowingStateLoss();
}
use of android.support.v4.app.FragmentManager in project mounts2sd by SpazeDog.
the class ActivityTabController method tabSwitchFragment.
private void tabSwitchFragment(Integer tabId) {
FragmentManager fragmentManager = getSupportFragmentManager();
Boolean isAdded = fragmentManager.findFragmentByTag("" + tabId) != null;
if (!isAdded || mTabFragments.get(tabId).isDetached()) {
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
for (int id : mTabFragments.keySet()) {
if (id != tabId && fragmentManager.findFragmentByTag("" + id) != null && !mTabFragments.get(id).isDetached()) {
fragmentTransaction.detach(mTabFragments.get(id));
}
}
if (isAdded && mTabFragments.get(tabId).isDetached()) {
fragmentTransaction.attach(mTabFragments.get(tabId));
} else if (!isAdded) {
fragmentTransaction.add(R.id.fragment_frame, mTabFragments.get(tabId), "" + tabId);
}
fragmentTransaction.commit();
} else {
tabUpdateButtons();
}
}
use of android.support.v4.app.FragmentManager in project storymaker by StoryMaker.
the class AccountsActivity method addChooseAccountFragment.
public void addChooseAccountFragment(Bundle bundle) {
FragmentManager fragManager = getSupportFragmentManager();
FragmentTransaction fragTrans = fragManager.beginTransaction();
List<Account> accounts = new ArrayList<>();
final AuthTable authTable = new AuthTable();
String[] siteAvailableNames = Account.CONTROLLER_SITE_NAMES;
String[] siteAvailableKeys = Account.CONTROLLER_SITE_KEYS;
Auth auth;
for (int i = 0; i < siteAvailableKeys.length; i++) {
auth = authTable.getAuthDefault(this, siteAvailableKeys[i]);
if (auth == null) {
accounts.add(new Account(-1, siteAvailableNames[i], siteAvailableKeys[i], "", "", null, false, false));
} else {
accounts.add(auth.convertToAccountObject());
}
}
caFragment = new ChooseAccountFragment();
caFragment.setArguments(bundle);
caFragment.setLoginIntent(new Intent(this, ConnectAccountActivity.class));
// FIXME we should probably make Account object parcelable and pass this through the bundle
caFragment.setAccountsList(accounts);
caFragment.setOnEventListener(new SiteController.OnEventListener() {
@Override
public void onSuccess(Account account) {
Auth auth = authTable.getAuthDefault(getApplicationContext(), account.getSite());
//if auth doesn't exist in db
if (auth == null) {
auth = new Auth(getApplicationContext(), -1, account.getName(), account.getSite(), null, null, null, null, null);
auth.insert();
}
//set id of account based on returned id of auth insert
account.setId(auth.getId());
auth.setCredentials(account.getCredentials());
auth.setData(account.getData());
auth.setUserName(account.getUserName());
auth.setExpires(null);
authTable.updateLastLogin(getApplicationContext(), account.getSite(), auth.getUserName());
auth.update();
}
@Override
public void onFailure(Account account, String failureMessage) {
Auth auth = authTable.getAuthDefault(getApplicationContext(), account.getSite());
if (auth != null) {
//TODO set variables here
auth.setCredentials(account.getCredentials());
auth.setUserName(account.getName());
auth.setData(account.getData());
// FIXME this is a hack to get isValid to fail, probably should be a setFailed() in auth that marks that we are busted
auth.setExpires(new Date());
auth.update();
}
}
@Override
public void onRemove(Account account) {
authTable.delete(getApplicationContext(), account.getId());
}
});
fragTrans.add(R.id.fragmentLayout, caFragment);
fragTrans.commit();
}
use of android.support.v4.app.FragmentManager in project HoloEverywhere by Prototik.
the class FragmentBreadCrumbs method updateCrumbs.
void updateCrumbs() {
FragmentManager fm = mActivity.getSupportFragmentManager();
int numEntries = fm.getBackStackEntryCount();
int numPreEntries = getPreEntryCount();
int numViews = mContainer.getChildCount();
for (int i = 0; i < numEntries + numPreEntries; i++) {
BackStackEntry bse = i < numPreEntries ? getPreEntry(i) : fm.getBackStackEntryAt(i - numPreEntries);
if (i < numViews) {
View v = mContainer.getChildAt(i);
Object tag = v.getTag();
if (tag != bse) {
for (int j = i; j < numViews; j++) {
mContainer.removeViewAt(i);
}
numViews = i;
}
}
if (i >= numViews) {
final View item = mInflater.inflate(R.layout.fragment_bread_crumb_item, this, false);
final TextView text = (TextView) item.findViewById(R.id.title);
text.setText(bse.getBreadCrumbTitle());
text.setTag(bse);
if (i == 0) {
item.findViewById(R.id.left_icon).setVisibility(View.GONE);
}
mContainer.addView(item);
text.setOnClickListener(mOnClickListener);
}
}
int viewI = numEntries + numPreEntries;
numViews = mContainer.getChildCount();
while (numViews > viewI) {
mContainer.removeViewAt(numViews - 1);
numViews--;
}
for (int i = 0; i < numViews; i++) {
final View child = mContainer.getChildAt(i);
child.findViewById(R.id.title).setEnabled(i < numViews - 1);
if (mMaxVisible > 0) {
child.setVisibility(i < numViews - mMaxVisible ? View.GONE : View.VISIBLE);
final View leftIcon = child.findViewById(R.id.left_icon);
leftIcon.setVisibility(i > numViews - mMaxVisible && i != 0 ? View.VISIBLE : View.GONE);
}
}
}
Aggregations