2012-04-08 64 views
2

我正在開發新的應用程序,我將標籤視圖用作父級佈局。我使用TabHost在我的應用程序中顯示3個選項卡。每個選項卡都有單獨的包含ListView的Activity。這工作正常。當你點擊ListView中的一個項目時,它目前會加載一個全新的活動全屏離開TabHost。我想在TabHost中加載這些活動。我想在從列表視圖中調用另一個活動後保留tabview。如何從tabview中的列表活動開始新的活動

謝謝你們的迴應。這裏是我的代碼,我需要你的幫助。

################ HelloTabWidget

//此類使用3選項卡顯示選項卡視圖 - Customer,Company和City。

public class HelloTabWidget extends TabActivity { 
    //public class HelloTabWidget extends ActivityGroup { 
     public void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.main); 

      Resources res = getResources(); 


     TabHost tabHost = getTabHost(); 
     TabHost.TabSpec spec; 
     Intent intent; 
     intent = new Intent().setClass(this, CustomerTabView.class); 
     spec = tabHost 
       .newTabSpec("Customer") 
       .setIndicator("Customer", 
         res.getDrawable(R.drawable.ic_tab_Customer)) 
       .setContent(intent); 
     tabHost.addTab(spec); 

     intent = new Intent().setClass(this, CompanyTabView.class); 
     spec = tabHost 
       .newTabSpec("Company") 
       .setIndicator("Company", 
         res.getDrawable(R.drawable.ic_tab_Company)) 
       .setContent(intent); 
     tabHost.addTab(spec); 

     intent = new Intent().setClass(this, CityTabView.class); 
     spec = tabHost 
       .newTabSpec("City") 
       .setIndicator("City", res.getDrawable(R.drawable.ic_tab_City)) 
       .setContent(intent); 
     tabHost.addTab(spec); 

     tabHost.setCurrentTab(0); 
    } 
} 
################ CustomerTabView

//用戶名該類顯示列表視圖。點擊列表中的任何項目,它應該打開客戶詳細信息頁面,保持相同的標籤頁視圖。

public class CustomerTabView extends ListActivity { 
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     String[] category = getResources().getStringArray(
       R.array.category_array); 
     setListAdapter(new ArrayAdapter<String>(this, R.drawable.list_items, 
       category)); 
     ListView lv = getListView(); 
     lv.setTextFilterEnabled(true); 

     lv.setOnItemClickListener(new OnItemClickListener() { 
      public void onItemClick(AdapterView<?> parent, View view, 
        int position, long id) { 

       //Need this logic where I can retain the tab view and call new activity class for customerdetails view.     

       Intent intent; 
       intent = new Intent(CustomerTabView.this, 
         C_DetailActivity.class); 
       startActivity(intent); 
       finish(); 
      } 

     }); 
    } 
} 
################ C_DetailActivity

在從customertabview任何項目的點擊,這一活動類獲取調用它顯示了客戶的詳細信息。

public class C_DetailActivity extends Activity { 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     TextView textview = new TextView(this); 
     textview.setText("This is the Customer Details view"); 
     setContentView(textview); 
    } 
} 

調用C_DetailActivity下課後,標籤視圖中消失。我想保留主標籤視圖。 所以需要這樣的邏輯,我可以保留選項卡視圖,並呼籲新的活動類爲CustomerDetails查看

+0

保持簡單。我不會這樣做。 – Blundell 2012-04-08 15:42:30

+0

請張貼您的代碼?這應該是一個簡單的實現。 – 2012-04-08 16:39:32

+0

你還需要什麼?你有意圖嗎? – 2012-04-08 18:12:53

回答

1
ListView lv = (ListView) findViewById(R.id.myListView); 
lv.setOnItemClickListener(new OnItemClickListener() { 
    public void onItemClick(AdapterView parent, View v, int position, long id) { 
     LocalActivityManager manager = getLocalActivityManager(); 
     String currentTag = tabHost.getCurrentTabTag(); 
     manager.destroyActivity(currentTag, true); 
     manager.startActivity(currentTag, new Intent(this, newClass.class)); 
    } 
}); 

沒有測試,但是這樣的事情應該工作。如果不是,我會幫你解決它。

+0

謝謝,我在 – user1320395 2012-04-08 17:44:53

+0

上面添加了我的代碼,並且如果將OnItemClickListener替換爲我的代碼,它會工作嗎? – 2012-04-08 19:00:57

+0

getLocalActivityManager不適用於ListActivity。任何想法如何保留選項卡視圖的詳細頁面 – user1320395 2012-04-09 00:40:24