2013-02-04 131 views
2

我想要在滑動視圖片段(名爲BROWSE)中有選項卡片段。當訪問BROWSE時,完成到遠程服務器的連接,檢索數據並在選項卡內的gridview中顯示數據,當單擊不同的選項卡時,建立新的連接,並且使用新數據填充GridView。Android - 碎片內的片段 - 保留狀態

問題是,我無法保存標籤的狀態,我不想每次都建立連接,只有當標籤首次被點擊時纔會建立與數據庫的連接,如果標籤是再次點擊我想讓gridview保存狀態。

下面是瀏覽片段的onCreate方法,在這種方法中,我初始化加載器來從遠程服務器中的數據:

 @Override 
     public void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 




      if (t == 0) { 
      Log.v("BROWSE","++++++++ Inside onCreate t = 0 ++++++++"); 

      tweets = Collections.emptyList(); 
      // This is our REST action. 

      Uri twitterSearchUri = new Uri.Builder() 
      .scheme("http") 
      .authority(ip) 
      .path("browse.php") 
      .appendQueryParameter("SpecialOffer", "0") 
      .build(); 

       Bundle params = new Bundle(); 
      // params.putString("q", "android"); 
       params.get("salim slem"); 
       // These are the loader arguments. They are stored in a Bundle because 
       // LoaderManager will maintain the state of our Loaders for us and 
       // reload the Loader if necessary. This is the whole reason why 
       // we have even bothered to implement RESTLoader. 
       Bundle args = new Bundle(); 
       args.putParcelable(ARGS_URI, twitterSearchUri); 
       args.putParcelable(ARGS_PARAMS, params); 


      //For loading Images  
      //--------------------------------------------------  

       options = new DisplayImageOptions.Builder() 
       .showStubImage(R.drawable.stub_image) 
       .showImageForEmptyUri(R.drawable.image_for_empty_url) 
       .cacheInMemory() 
       .cacheOnDisc() 
       .bitmapConfig(Bitmap.Config.RGB_565) 
       .build(); 


      //--------------------------------------------------  



       // Initialize the Loader. 
       getSupportLoaderManager().initLoader(LOADER_TWITTER_SEARCH, args, this); 

      } 

      if(t == 1) { 

       Log.v("BROWSE","++++++++ Inside onCreate t = 1 ++++++++"); 

       tweets = Collections.emptyList(); 
       // This is our REST action. 

       Uri twitterSearchUri = new Uri.Builder() 
       .scheme("http") 
       .authority(ip) 
       .path("browse.php") 
       .appendQueryParameter("SpecialOffer", "1") 
       .build(); 

        Bundle params = new Bundle(); 
       // params.putString("q", "android"); 
        params.get("salim slem"); 
        // These are the loader arguments. They are stored in a Bundle because 
        // LoaderManager will maintain the state of our Loaders for us and 
        // reload the Loader if necessary. This is the whole reason why 
        // we have even bothered to implement RESTLoader. 
        Bundle args = new Bundle(); 
        args.putParcelable(TAB1_ARGS_URI, twitterSearchUri); 
        args.putParcelable(TAB1_ARGS_PARAMS, params); 


       //For loading Images  
       //--------------------------------------------------  

        options = new DisplayImageOptions.Builder() 
        .showStubImage(R.drawable.stub_image) 
        .showImageForEmptyUri(R.drawable.image_for_empty_url) 
        .cacheInMemory() 
        .cacheOnDisc() 
        .bitmapConfig(Bitmap.Config.RGB_565) 
        .build(); 

       //--------------------------------------------------  

        // Initialize the Loader. 
        getSupportLoaderManager().initLoader(TAB1_LOADER_TWITTER_SEARCH, args, this); 
       } 
     } 

這裏是沿創建標籤的onCreateView方法與網格視圖

@Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
      Bundle savedInstanceState) { 

     Log.v("BROWSE", "Inside OnCreateView"); 

     View view = inflater.inflate(R.layout.gridview, container, false); 

     gridView = (GridView) view.findViewById(R.id.gridViewImage); 

     gridView.setAdapter(new ImageTextAdapter(view.getContext(), LOADING)); 

     // When an item in the gridview is clicked 
     gridView.setOnItemClickListener(new OnItemClickListener() { 

      @Override 
      public void onItemClick(AdapterView<?> parent, View v, 
        int position, long id) { 
       FragmentPosition = 2; 
       Log.v("BROWSE: POSITION OF ITEM IN GRIDVIEW","## "+position+" ##"); 
       DialogFragment newFragment = new BrowseDialogFragment(position); 
       newFragment.show(getSupportFragmentManager(), "missiles"); 

      } 
     }); 

     // creating tabs 
     mTabHost = (TabHost) view.findViewById(R.id.testtabhost1); 
     mTabHost.setup(); 
     mTabHost.addTab(mTabHost.newTabSpec("tab_test1").setIndicator("TAB 1").setContent(R.id.gridViewImage)); 
     mTabHost.addTab(mTabHost.newTabSpec("tab_test2").setIndicator("TAB 2").setContent(R.id.gridViewImage)); 
     mTabHost.addTab(mTabHost.newTabSpec("tab_test3").setIndicator("TAB 3").setContent(R.id.gridViewImage)); 
     mTabHost.setCurrentTab(0); 



     mTabHost.setOnTabChangedListener(new OnTabChangeListener() { 


      @Override 
      public void onTabChanged(String tabId) { 

      int i = mTabHost.getCurrentTab(); 
      Log.i("@@@@@@@@ ANN CLICK TAB NUMBER", "------" + i); 

      if (i ==0) { 
       Bundle tempBundle = new Bundle(); 
       t = 0; 
       onCreate(tempBundle); 
     } 

      if (i ==1) { 
       Bundle tempBundle = new Bundle();  
       t = 1; 
       onCreate(tempBundle); 
       } 

       } 
      }); 

     return view; 
    } 

我想我的問題是,我不能給onCreate從onCreateView「savedInstanceState」。我的問題是,我怎樣才能保存在選項卡中的狀態?也許使用給予onCreate的包,但我還沒有找到如何去做!

預先感謝您的幫助!

這就是我說的是UI:

enter image description here

,這時候一個標籤再次按下UI:

enter image description here

回答

0

嘗試使用數據庫中該案件。它的pritty容易認識到: tutorial

所以,你應該做的:

  1. 獲取遠程數據
  2. 將它保存在SQLite的DB
  3. 創建了Methode,檢查如何舊數據在數據庫中。
  4. 例如,如果數據大於10分鐘,則應採用遠程數據並更新表。

我希望這會有所幫助。

此致

狩獵

0

的savedInstanceState變量被傳遞給onViewCreated方法,該方法後onCreateView立即調用。

你應該儘量只充氣佈局和onCreateView(GridView控件,mTabHost等)初始化視野,並在onViewCreated

恢復狀態(或設置默認狀態)