gpt4 book ai didi

android - 我想保存选项卡的状态

转载 作者:行者123 更新时间:2023-11-30 02:39:14 25 4
gpt4 key购买 nike

我在我的应用程序中创建了三个选项卡,并在每个选项卡中打开了子 Activity 。现在我想要的是我想保存选项卡的状态。例如如果我正在处理 TAB-1 的子 Activity 并按 TAB-2。然后如果返回 TAB-1。它从同一个子 Activity 而不是初始 Activity 开始。请帮忙

这是我的标签代码。

SuppressWarnings("deprecation")
public class TabHostActivity extends TabActivity implements OnTabChangeListener
{
private static final String[] TABS = { "HomeGroupActivity", "AboutGroupActivity", "ContactGroupActivity" };
private static final String[] TAB_NAMES = { "Home", "About", "Contact"};
public static TabHost tabs ;
public static TabWidget tabWidget ;
protected Bitmap roundedImage;
public boolean checkTabsListener = false;


public void onCreate(Bundle icicle)
{
super.onCreate(icicle);
setContentView(R.layout.activity_tab_host);

Bitmap roundedImage = BitmapFactory.decodeResource(getResources(),R.drawable.ic_tab_background);
roundedImage = getRoundedCornerBitmap(roundedImage,3);

tabs = getTabHost();

tabWidget = tabs.getTabWidget();

tabs.setOnTabChangedListener(this);

for (int i = 0; i < TABS.length; i++)
{
TabHost.TabSpec tab = tabs.newTabSpec(TABS[i]);

//Asociating Components
ComponentName oneActivity = new ComponentName("com.example.tabs", "com.example.tabs." + TABS[i]);
Intent intent = new Intent().setComponent(oneActivity);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
tab.setContent(intent);
//Setting the Indicator
MyTabIndicator myTab = new MyTabIndicator(this, TAB_NAMES[i],(i+1), roundedImage);
tab.setIndicator(myTab);
tabs.addTab(tab);
}

checkTabsListener = true;

for(int i=0;i<tabs.getTabWidget().getChildCount();i++)
{
tabs.getTabWidget().getChildAt(i).setBackgroundColor(Color.TRANSPARENT);
}


tabs.getTabWidget().getChildAt(0).setBackgroundResource(R.drawable.ic_tab_background);

//Maintaining Clicks

// Home Tab Click

tabWidget.getChildAt(0).setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
if(HomeGroupActivity.HomeGroupStack != null && HomeGroupActivity.HomeGroupStack.mIdList.size()>1)
{
HomeGroupActivity.HomeGroupStack.getLocalActivityManager().removeAllActivities();
HomeGroupActivity.HomeGroupStack.mIdList.clear();
HomeGroupActivity.HomeGroupStack.mIntents.clear();
HomeGroupActivity.HomeGroupStack.mAnimator.removeAllViews();
HomeGroupActivity.HomeGroupStack.startChildActivity("CareGroupActivity", new Intent(HomeGroupActivity.HomeGroupStack, HomeActivity.class));

}

tabWidget.setCurrentTab(0);
tabs.setCurrentTab(0);
tabs.getTabWidget().getChildAt(0).setBackgroundResource(R.drawable.ic_tab_background);
}
});


// About tab Click

tabWidget.getChildAt(1).setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
if(AboutGroupActivity.AboutGroupStack != null && AboutGroupActivity.AboutGroupStack.mIdList.size()>0)
{
AboutGroupActivity.AboutGroupStack.getLocalActivityManager().removeAllActivities();
AboutGroupActivity.AboutGroupStack.mIdList.clear();
AboutGroupActivity.AboutGroupStack.mIntents.clear();
AboutGroupActivity.AboutGroupStack.mAnimator.removeAllViews();
AboutGroupActivity.AboutGroupStack.startChildActivity("TrackingGroupActivity", new Intent(AboutGroupActivity.AboutGroupStack, About.class));
}

tabWidget.setCurrentTab(1);
tabs.setCurrentTab(1);
tabs.getTabWidget().getChildAt(1).setBackgroundResource(R.drawable.ic_tab_background);
}
});

// Contact tab click

tabWidget.getChildAt(2).setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
if(ContactGroupActivity.ContactGroupStack != null && ContactGroupActivity.ContactGroupStack.mIdList.size()>0)
{

ContactGroupActivity.ContactGroupStack.getLocalActivityManager().removeAllActivities();
ContactGroupActivity.ContactGroupStack.mIdList.clear();
ContactGroupActivity.ContactGroupStack.mIntents.clear();
ContactGroupActivity.ContactGroupStack.mAnimator.removeAllViews();
ContactGroupActivity.ContactGroupStack.startChildActivity("DashboardGroupActivity",
new Intent(ContactGroupActivity.ContactGroupStack, Contact.class));
}

tabWidget.setCurrentTab(2);
tabs.setCurrentTab(2);
tabs.getTabWidget().getChildAt(2).setBackgroundResource(R.drawable.ic_tab_background);
}
});


}


public class MyTabIndicator extends LinearLayout
{
public MyTabIndicator(Context context, String label, int tabId, Bitmap bgImg)
{
super(context);
LinearLayout tab = null;
TextView tv;
this.setGravity(Gravity.CENTER);

if(tabId == 1)
{
tab = (LinearLayout) LayoutInflater.from(context).inflate(R.layout.home_tab, null);
tv = (TextView)tab.findViewById(R.id.tab_label);
tv.setText(label);
}



else if(tabId == 2)
{
tab = (LinearLayout) LayoutInflater.from(context).inflate(R.layout.about_tab, null);
tv = (TextView)tab.findViewById(R.id.tab_label);
tv.setText(label);
}
else if(tabId == 3)
{
tab = (LinearLayout) LayoutInflater.from(context).inflate(R.layout.contact_tab, null);
tv = (TextView)tab.findViewById(R.id.tab_label);
tv.setText(label);
}

this.addView(tab, new LinearLayout.LayoutParams(320/4,55));
}
}



public void onTabChanged(String tabId)
{
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(tabs.getApplicationWindowToken(), 0);

for(int i=0; i<tabs.getTabWidget().getChildCount(); i++)
{
if(tabId.equalsIgnoreCase(TABS[i]))
{
tabs.getTabWidget().getChildAt(i).setBackgroundResource(R.drawable.ic_tab_background);
}
else
{
tabs.getTabWidget().getChildAt(i).setBackgroundColor((Color.TRANSPARENT));
}
}
}

public static Bitmap getRoundedCornerBitmap(Bitmap bitmap,float roundPxRadius)
{
Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Config.ARGB_8888);
Canvas canvas = new Canvas(output);

final int color = 0xff424242;
final Paint paint = new Paint();
final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
final RectF rectF = new RectF(rect);
final float roundPx =roundPxRadius;

paint.setAntiAlias(true);
canvas.drawARGB(0, 0, 0, 0);
paint.setColor(color);
canvas.drawRoundRect(rectF, roundPx, roundPx, paint);

paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
canvas.drawBitmap(bitmap, rect, rect, paint);

return output;
}

public void onResume()
{
super.onResume();


//ReConstructing TabViews
reDesignTabViews();
}

public void onPause()
{
super.onPause();
}




/**
* Method used to re constructing the Views at tab bar. This solves tabs disappearing issue.
*/
public void reDesignTabViews()
{
MyTabIndicator myIndicator;


//Construction of tab views....
for(int i=0 ; i< tabWidget.getChildCount() ; i++)
{
myIndicator = (MyTabIndicator) tabWidget.getChildAt(i);
myIndicator.removeAllViews();

switch (i)
{

case 0:
myIndicator.addView((LinearLayout) LayoutInflater.from(getApplicationContext()).inflate(R.layout.home_tab, null));
break;
case 1:
myIndicator.addView((LinearLayout) LayoutInflater.from(getApplicationContext()).inflate(R.layout.about_tab, null));
break;
case 2:
myIndicator.addView((LinearLayout) LayoutInflater.from(getApplicationContext()).inflate(R.layout.contact_tab, null));
break;

}
}
}

最佳答案

public void onTabChanged(String tabId) {      
tabs.clearAllTabs();
for(int i = 0; i < TABS.length; i++) {
//Add here same code you´ve got up there but with the same intent for each tabSpec
}
setCurrentTabByTag (tabId); // OR, if you know the id, select current by id.
}

关于android - 我想保存选项卡的状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26009299/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com