2013-10-01 49 views
2

我試圖用PagerSlidingTabStrip而不是TabView實現viewpager。 viewpager有三個選項卡,其中每個listview顯示一個事件列表。這三個標籤被稱爲過去,今晚和未來。當片段被替換時無法加載PagerSlidingTabStrip中的選項卡

我已經設置了滑塊作爲github頁面提示:

public View onCreateView(LayoutInflater inflater, ViewGroup container, 
      Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 

     View v = inflater.inflate(R.layout.all_events_main_strip, container, false);  

     // Set up the ViewPager, attaching the adapter and setting up a listener for when the 
     // user swipes between sections. 
     pager = (ViewPager) v .findViewById(R.id.pager_main); 
     tabs = (PagerSlidingTabStrip) v.findViewById(R.id.tabs); 
     adapter = new MyPagerAdapter(getFragmentManager()); 

     pager.setAdapter(adapter); 
     tabs.setViewPager(pager); 

     // Set Present tab as default 
     pager.setCurrentItem(1); 
     return v; 
    } 

當應用程序啓動的主要活動是第一次這個片斷將一切的偉大工程。 3個列表視圖的3個可滑動選項卡。 (參見代碼段)

這裏的問題是

我注意到,當我按下後退按鈕,並再次更換片段,以重新打開viewpager,中間沒有按標籤不顯示任何列表視圖。如果我向左或向右滾動,其他選項卡中的內容將被加載並顯示,但當前選項卡保持空白。 當我調試ToNightEvents時ListFragment根本沒有被調用。 你們有什麼建議可以解決這個問題嗎?

代碼:

的代碼結構如下:在onCreateView我已經添加了一個OnDestroyView方法來刪除片段後,但沒有奏效...然後在fragmentPagerAdapter每一頁在getItem方法中稱爲片段。最後,在代碼結束時,您可以在其中看到一個列表視圖是通過的AsyncTask填充三個ListFragment類

public class FragmentAllEvents extends Fragment 
{ 
    private static final String TAG_UID = "uid"; 
    private static final String TAG_LOGO = "logo"; 
    private static final String TAG_POKUID = "pokuid"; 

    static ArrayList<HashMap<String, String>> userList; 
    ArrayList<HashMap<String, String>> userListTotal; 
    private final Handler handler = new Handler(); 
    HashMap<String, String> userSelected; 
    EventsFunctions eventsFunctions; 
    UserFunctions userFunctions; 
    static ListView lv; 
    ActionBar actionBar; 
    MyPagerAdapter adapter; 
    ViewPager pager; 
    PagerSlidingTabStrip tabs; 
    private Drawable oldBackground = null; 
    private int currentColor = 0xFF666666; 
    //Context context = this; 

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

     // Set up the action bar. 
     actionBar = getActivity().getActionBar(); 
     actionBar.setHomeButtonEnabled(true); 

    } 

    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
      Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 

     View v = inflater.inflate(R.layout.all_events_main_strip, container, false); 
     pager = (ViewPager) v .findViewById(R.id.pager_main); 
     tabs = (PagerSlidingTabStrip) v.findViewById(R.id.tabs); 
     adapter = new MyPagerAdapter(getFragmentManager()); 

     pager.setAdapter(adapter); 

     tabs.setViewPager(pager); 

     pager.setCurrentItem(1); 
     return v; 
    } 

    @Override 
    public void onDestroyView() 
    { 
     super.onDestroyView(); 
     getActivity().getSupportFragmentManager().beginTransaction().remove(this).commit(); 
    } 

    public static class MyPagerAdapter extends FragmentPagerAdapter 
    { 

     public MyPagerAdapter(FragmentManager fm) 
     { 
      super(fm); 
     } 


     @Override 
     public Fragment getItem(int i) 
     { 
      switch (i) 
      { 
       case 0: 
        return new PastEvents(); 

       case 1: 
        return new ToNightEvents(); 

       case 2: 
        return new FutureEvents(); 

       /*default: 
        // The other sections of the app are dummy placeholders. 

        return new ToNightEvents(); 
        */ 
      } 
      return null; 
     } 


     /** 
     * A fragment that launches past events list. 
     */ 
     public static class PastEvents extends ListFragment implements 
     PullToRefreshAttacher.OnRefreshListener 
     { 
      private ListView pastList; 
      private PullToRefreshAttacher mPullToRefreshAttacher; 
      ProgressBar progress; 
      String tabTime; 

      @Override 
      public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) 
      { 
       View pastView = inflater.inflate(R.layout.pastlist, container, false); 
       progress = (ProgressBar) pastView.findViewById(R.id.loading_spinner_past); 
       tabTime="past"; 
       pastList = (ListView) pastView.findViewById(android.R.id.list); 

       // Now get the PullToRefresh attacher from the Activity. An exercise to the reader 
       // is to create an implicit interface instead of casting to the concrete Activity 
       mPullToRefreshAttacher = ((Home) getActivity()).getPullToRefreshAttacher(); 

       // Now set the ScrollView as the refreshable view, and the refresh listener (this) 
       mPullToRefreshAttacher.addRefreshableView(pastList, this); 

       new AsyncLoadEvents(getActivity(), progress, pastList, mPullToRefreshAttacher).execute(tabTime); 
       return pastView; 
      } 


      @SuppressWarnings("unchecked") 
      @Override 
      public void onListItemClick(ListView listView, View view, int position, long id) 
      { 
       super.onListItemClick (listView, view, position, id); 
       HashMap<String, String> map = (HashMap<String, String>) getListView().getItemAtPosition(position); 
       //Log.e("AttendList Report", "Clicked list item: " + position +" Content: \n" + map.get(TAG_ID).toString()); 
       Log.e("PastList Report", "Clicked list item: " + position +" Event's content: \n" + map.get(TAG_UID).toString()); 
       Intent intent = new Intent(getActivity(), SingleEventActivity.class); 

       intent.putExtra("pokuid",map.get(TAG_POKUID)); // Maybe remove attribute toString(); 
       intent.putExtra("uid", map.get(TAG_UID)); 
       intent.putExtra("logo",map.get(TAG_LOGO)); 
       getActivity().startActivity(intent); 
      } 

      @Override 
      public void onRefreshStarted(View view) 
      { 
       new AsyncLoadEvents(getActivity(), progress, pastList, mPullToRefreshAttacher).execute(tabTime);     
      } 

     } 

     /** 
     * A fragment that launches future event list. 
     */ 
     public static class FutureEvents extends ListFragment implements 
     PullToRefreshAttacher.OnRefreshListener 
     { 
      private ListView futureList; 
      private PullToRefreshAttacher mPullToRefreshAttacher; 
      ProgressBar progress; 
      String tabTime; 

      @Override 
      public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) 
      { 
       View futureView = inflater.inflate(R.layout.futurelist, container, false); 
       progress = (ProgressBar) futureView.findViewById(R.id.loading_spinner_future); 
       tabTime = "future"; 
       futureList = (ListView) futureView.findViewById(android.R.id.list); //change to attendlist if needed 


       // Now get the PullToRefresh attacher from the Activity. An exercise to the reader 
       // is to create an implicit interface instead of casting to the concrete Activity 
       mPullToRefreshAttacher = ((Home) getActivity()).getPullToRefreshAttacher(); 

       // Now set the ScrollView as the refreshable view, and the refresh listener (this) 
       mPullToRefreshAttacher.addRefreshableView(futureList, this); 

       new AsyncLoadEvents(getActivity(), progress, futureList, mPullToRefreshAttacher).execute(tabTime); 
       return futureView; 
      } 

      @SuppressWarnings("unchecked") 
      @Override 
      public void onListItemClick(ListView listView, View view, int position, long id) 
      { 
       super.onListItemClick (listView, view, position, id); 

       HashMap<String, String> map = (HashMap<String, String>) getListView().getItemAtPosition(position); 
       Log.e("PastList Report", "Clicked list item: " + position +" Event's content: \n" + map.get(TAG_UID).toString()); 
       Intent intent = new Intent(getActivity(), SingleEventActivity.class); 


       intent.putExtra("pokuid",map.get(TAG_POKUID)); // Maybe remove attribute toString(); 
       intent.putExtra("uid", map.get(TAG_UID)); 
       intent.putExtra("logo",map.get(TAG_LOGO)); 
       getActivity().startActivity(intent); 

      } 

      @Override 
      public void onRefreshStarted(View view) 
      { 
       new AsyncLoadEvents(getActivity(), progress, futureList, mPullToRefreshAttacher).execute(tabTime);    
      } 
     } 

     /** 
     * A fragment that launches future event list. 
     */ 
     public static class ToNightEvents extends ListFragment implements 
     PullToRefreshAttacher.OnRefreshListener 
     { 
      private ListView tonightList; 
      private PullToRefreshAttacher mPullToRefreshAttacher; 
      ProgressBar progress; 
      String tabTime; 

      @Override 
      public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) 
      { 
       View tonightView = inflater.inflate(R.layout.tonightlist, container, false); 
       progress = (ProgressBar) tonightView.findViewById(R.id.loading_spinner_tonight); 
       tabTime = "tonight"; 
       tonightList = (ListView) tonightView.findViewById(android.R.id.list); //change to attendlist if needed 

       // Now get the PullToRefresh attacher from the Activity. An exercise to the reader 
       // is to create an implicit interface instead of casting to the concrete Activity 
       mPullToRefreshAttacher = ((Home) getActivity()).getPullToRefreshAttacher(); 

       // Now set the ScrollView as the refreshable view, and the refresh listener (this) 
       mPullToRefreshAttacher.addRefreshableView(tonightList, this); 

       new AsyncLoadEvents(getActivity(), progress, tonightList, mPullToRefreshAttacher).execute(tabTime); 
       return tonightView; 
      } 


      @SuppressWarnings("unchecked") 
      @Override 
      public void onListItemClick(ListView listView, View view, int position, long id) 
      { 
       super.onListItemClick (listView, view, position, id); 

       HashMap<String, String> map = (HashMap<String, String>) getListView().getItemAtPosition(position); 
       Log.e("PastList Report", "Clicked list item: " + position +" Event's content: \n" + map.get(TAG_UID).toString()); 
       Intent intent = new Intent(getActivity(), SingleEventActivity.class); 


       intent.putExtra("pokuid",map.get(TAG_POKUID)); // Maybe remove attribute toString(); 
       intent.putExtra("uid", map.get(TAG_UID)); 
       intent.putExtra("logo",map.get(TAG_LOGO)); 
       getActivity().startActivity(intent); 

      } 

      @Override 
      public void onRefreshStarted(View view) 
      { 
       new AsyncLoadEvents(getActivity(), progress, tonightList, mPullToRefreshAttacher).execute(tabTime);    
      } 
     } 

     public String[] titles= 
     { 
      "Past", 
      "Tonight", 
      "Future" 
     }; 

     @Override 
     public int getCount() 
     { 
      return titles.length; 
     } 

     @Override 
     public CharSequence getPageTitle(int position) 
     { 
      return titles[position]; 
     } 
    } 
} 
+0

我有同樣的問題。你解決了嗎? – SparX

+0

我也有同樣的問題,請更新? –

+0

@SparX你是否解決了問題請更新 –

回答

0

以下固定對我來說:

  • 添加OnBackStackChangedListener的片段經理。
  • 在onBackStackChanged方法中,獲取對ViewPager和PagerSlidingTabStrip的引用(在您的示例中爲pager和tabs)。如果相應的片段是當前活動,請執行下列操作:

    pager.invalidate(); 
    tabs.invalidate(); 
    pager.setAdapter(new MyPagerAdapter(getFragmentManager())); 
    tabs.setViewPager(pager); 
    
5

,如果你在一個Activity這將正常工作,但我猜你是在一個片段,因爲你發佈的代碼是方法onCreateView ()。問題是你正在嘗試使用Activity的FragmentManager,你應該使用Fragment的FragmentManager。這個FragmentManager不是你需要的。試試這個:

adapter = new MyPagerAdapter(getChildFragmentManager()); 

我想我是有點晚了,但如果有人碰巧在這裏同樣的錯誤是解決=)

Q.

+0

輝煌,工作愉快! –

+0

非常感謝:) @Qiwii – Mithran

相關問題