2014-09-24 3 views
1

응용 프로그램에서 세 개의 탭을 만들고 각 탭 내에서 하위 활동을 열었습니다. 이제 내가 원하는 것은 탭의 상태를 저장하고 싶다는 것입니다. 예 : TAB-1의 자녀 활동을 위해 TAB-2를 누르면됩니다. TAB-1로 돌아 가면 초기 활동 대신 동일한 하위 활동에서 시작됩니다. 제발 도와주세요탭의 상태를 저장하고 싶습니다.

여기 내 코드 코드가 있습니다.

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; 

    } 
    } 
} 
+0

확인을 내가 원하는, 다음 ... 나는 당신이 달성하고자하는 것을 제대로 이해하지는 "이제 내가 원하는 것이된다 Tab의 상태를 저장하십시오. 예를 들어, TAB-1의 하위 작업을 수행하고 TAB-2 "를 누르십시오. 내가 아는 한 각각의 활동은 내부 상태를 onStop에 저장하고 onStart를 복원하는 책임이 있습니다. – eduyayo

+0

@eduyayo Tab-1의 두 번째 하위 활동에서 Tab-2로 휴가를 전환하면 각 탭 안에 여러 하위 활동이 있습니다. Tab-1로 돌아 오면 두 번째 하위 활동이 초기 하위 활동 – user3679400

+0

대신 동일한 데이터로 표시되어야합니다. 사용자가 tab1을 클릭했지만 여전히 활동 2를 표시합니까? – eduyayo

답변

0
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. 
} 

이나 뭐 ....

+0

끝까지 작동합니까? – user3679400

+0

오류가 발생했습니다. 내 코드 – user3679400

+0

에 내 코드를 붙여 넣는 동안 내 코드가 "프로퍼티"코드가 아니기 때문에 작동하지 않습니다. 아마 심지어 컴파일되지 않을 것입니다. BTW, 당신이 탭의 내용을 유지하여 사용자를 혼란스럽게하고있어 통지. – eduyayo

1
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. 
} 
관련 문제