2012-11-19 77 views
1

我正在使用标签栏。每个选项卡中有三个以上的屏幕,当我按下选定选项卡的选项卡按钮时,它应该转到该选项卡的第一个屏幕,但不会显示。
这是我的tabar代码。标签栏:点击进入主屏幕

public class TabUi extends TabActivity { 
    private static final String TAG_DEALS = "DEAL"; 
    private static final String TAG_SEARCH = "SEARCH"; 
    private static final String TAG_BOOKMARKS = "ALERTS"; 
    private static final String TAG_FRIENDS = "BUZZ"; 
    private static final String TAG_MORE = "MORE"; 
    /* 
    * private static final String TAG_DEALS = ""; private static final String 
    * TAG_SEARCH = ""; private static final String TAG_BOOKMARKS = ""; private 
    * static final String TAG_FRIENDS = ""; private static final String 
    * TAG_MORE =""; 
    */ 
    protected LocationManager locationManager; 
    Location location; 
    private static final long MINIMUM_DISTANCE_CHANGE_FOR_UPDATES = 1; // in 
                     // Meters 
    private static final long MINIMUM_TIME_BETWEEN_UPDATES = 1000; // in 
                    // Milliseconds 

    // private TabHost host = null; 

    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, DealsActivityGroup.class); 
     spec = tabHost.newTabSpec(TAG_DEALS) 
       .setIndicator(TAG_DEALS, res.getDrawable(R.drawable.deal_icon)) 
       .setContent(intent); 
     tabHost.addTab(spec); 

     intent = new Intent().setClass(this, SearchActivityGroup.class); 
     spec = tabHost 
       .newTabSpec(TAG_SEARCH) 
       .setIndicator(TAG_SEARCH, 
         res.getDrawable(R.drawable.search_icon)) 
       .setContent(intent); 
     tabHost.addTab(spec); 

     intent = new Intent().setClass(this, BookmarksActivityGroup.class); 
     spec = tabHost 
       .newTabSpec(TAG_BOOKMARKS) 
       .setIndicator(TAG_BOOKMARKS, 
         res.getDrawable(R.drawable.bookmark_icon)) 
       .setContent(intent); 
     tabHost.addTab(spec); 

     intent = new Intent().setClass(this, FriendsActivityGroup.class); 
     spec = tabHost 
       .newTabSpec(TAG_FRIENDS) 
       .setIndicator(TAG_FRIENDS, 
         res.getDrawable(R.drawable.friend_icon)) 
       .setContent(intent); 
     tabHost.addTab(spec); 

     intent = new Intent().setClass(this, MoreActivity.class); 
     spec = tabHost.newTabSpec(TAG_MORE) 
       .setIndicator(TAG_MORE, res.getDrawable(R.drawable.more_icon)) 
       .setContent(intent); 
     tabHost.addTab(spec); 
    } 

    /*@Override 
    protected void onResume() { 
     // TODO Auto-generated method stub 
     super.onResume(); 

    }*/ 

} 

这是选项卡活动组代码:

public class DealsActivityGroup extends ActivityGroup { 

    public static DealsActivityGroup group; 
    private ArrayList<View> history; 
    private ProgressDialog pDialog; 
    String Url; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     this.history = new ArrayList<View>(); 
     group = this; 

    } 

    @Override 
    protected void onResume() { 
     // TODO Auto-generated method stub 
     super.onResume(); 
     // new DealsFirst().execute(); 
     View view = getLocalActivityManager().startActivity(
       "Dealsfirstactivity", 
       new Intent(DealsActivityGroup.this, Dealsfirstactivity.class) 
         .addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)) 
       .getDecorView(); 

     replaceView(view); 
    } 

    public void replaceView(View v) { 
     // Adds the old one to history 
     history.add(v); 
     v.setFocusable(true); 
     v.setFocusableInTouchMode(true); 
     v.requestFocus(); 

     // Changes this Groups View to the new View. 
     setContentView(v); 
    } 

    public void back() { 

     System.out.println("History size: " + history.size()); 
     if (history.size() > 1) { 
      history.remove(history.size() - 1); 
      View v = history.get(history.size() - 1); 
      System.out.println("If size: " + history.size()); 
      v.setFocusable(true); 
      v.setFocusableInTouchMode(true); 
      v.requestFocus(); 
      System.out.println("View : " + v); 
      setContentView(v); 

     } else 
      // System.out.println("History size: "+ history.size()); 
      this.finish(); 
    } 

    @Override 
    public void onBackPressed() { 
     DealsActivityGroup.group.back(); 
     return; 
    } 

    public class DealsFirst extends AsyncTask<String, String, String> { 

     /** 
     * Before starting background thread Show Progress Dialog 
     * */ 
     @Override 
     protected void onPreExecute() { 
      super.onPreExecute(); 
      // pDialog = new ProgressDialog(Dealsfirstactivity.this); 
      // pDialog.setMessage("Please wait!.."); 
      // pDialog.setIndeterminate(false); 
      // pDialog.setCancelable(true); 
      // pDialog.show(); 
     } 

     protected String doInBackground(String... args) { 
      if (StorageManager.Instance().GetdealscategoryData() == null) { 
       ServiceHelper.Instance().Dealscategorylist(); 
      } 
      return null; 

     } 

     /** 
     * After completing background task Dismiss the progress dialog 
     * **/ 
     protected void onPostExecute(String result) { 
      // dismiss the dialog once done 
      super.onPostExecute(result); 

      if (StorageManager.Instance().GetdealscategoryData() != null 
        && StorageManager.Instance().GetdealscategoryData().size() > 0) { 
       new Dealslistfirst().execute(); 

      } 
     } 

    } 

     public class Dealslistfirst extends AsyncTask<String, String, String> { 

      /** 
      * Before starting background thread Show Progress Dialog 
      * */ 
      @Override 
      protected void onPreExecute() { 
       super.onPreExecute(); 
       pDialog = new ProgressDialog(getDialogContext()); 
       pDialog.setMessage("Please wait!.."); 
       pDialog.setIndeterminate(false); 
       pDialog.setCancelable(true); 
       pDialog.show(); 
      } 

      protected String doInBackground(String... args) { 

       ServiceHelper.Instance().dealsforyoulist(Url); 
       return null; 

      } 

      /** 
      * After completing background task Dismiss the progress dialog 
      * **/ 
      protected void onPostExecute(String result) { 
       // dismiss the dialog once done 
       super.onPostExecute(result); 

       if (StorageManager.Instance().GetdealsData() != null 
         && StorageManager.Instance().GetdealsData().size() > 0) { 

        // runOnUiThread(action) 

       } else if (StorageManager.Instance().GetdealsData() == null) { 

       } 
       if (pDialog.isShowing()) { 
        pDialog.dismiss(); 
       } 

       View view = getLocalActivityManager().startActivity(
         "Dealsfirstactivity", 
         new Intent(DealsActivityGroup.this, 
           Dealsfirstactivity.class) 
           .addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)) 
         .getDecorView(); 

       replaceView(view); 
      } 

     } 

     private Context getDialogContext() { 
      Context context; 
      if (getParent() != null) 
       context = getParent(); 
      else 
       context = this; 
      return context; 
     } 
    } 

回答

1

TabActivity可以indentify当你切换标签(变化从一个标签到另一个),但如果您选择当前选项卡无法识别。

我与标签栏的工作。我在每个标签 中有三个以上的屏幕,当我按下所选标签的标签按钮时,它应该转到该标签的第一个 屏幕,但它不会去。

对于您需要实现TabActivity onTouchListener

请为这个问题推荐此链接。 get notified when current tab is selected

0

放“FLAG_ACTIVITY_CLEAR_TOP”标志在你的根活动的意图

Intent intent; 

     intent = new Intent().setClass(this, DealsActivityGroup.class); 
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
     spec = tabHost.newTabSpec(TAG_DEALS) 
       .setIndicator(TAG_DEALS, res.getDrawable(R.drawable.deal_icon)) 
       .setContent(intent); 
     tabHost.addTab(spec); 

做相同的标签的所有根意图和更换标签后,您将获得标签的第一个屏幕做这个 ,你有实现选项卡ontouch监听器并将此意图联系事件