1

我使用的是ViewPager與FragmentPagerAdapter。每個頁面都是同一個Fragment類的一個實例,我總是使用固定數量的7個頁面(從今天開始的一週中的幾天)。調試顯示所有碎片都已創建並在創建時擁有正確的數據。有趣的是,雖然該適配器在0,1,2 ... 6,片段的onCreateView被調用的順序6,0,1,2,... 5條爲了創造片段。的Android ViewPager與FragmentPageAdapter - 只有最後一個片段頁面可見

當運行應用程序和打開這個活動,第一個片段頁是空白,然後我可以通過標籤上滑行並看到所有的頁面都是除了最後一個空白。但是,最後一頁沒有顯示來自最後一天的數據,它顯示的是一天之前的數據,是倒數第二組數據。

我複製另一個ViewPager/FragmentPagerAdapter,我有這樣的作品,雖然是一個只有3頁,使用不同片段的每一頁。

我沒有看到什麼我做錯了,我一直在尋找了一整天,還沒有找到答案。在這裏很多類似的問題通過使用FragmentStatePagerAdapter而不是FragmentPagerAdapter來解決。我已經嘗試過,它的行爲與目前完全相同。

這是活動主辦ViewPager:

public class ForecastFaveRowClickedActivity extends AppCompatActivity { 

    String distName = ""; 
    public List<ForecastEntry> forecastEntries = new ArrayList<>(); 
    ForecastPagerAdapter adapterViewPager; 


    @Override 
    protected void onCreate(Bundle savedInstanceState) { 

     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_forecast_detail);     

     // get the districts from the main activity 
     Bundle bundle = getIntent().getExtras();    
     distName = bundle.getString("districtName"); 
     if (bundle.containsKey("forecastData")) { 
      forecastEntries = bundle.getParcelableArrayList("forecastData"); 
     }         

     Collections.sort(forecastEntries); 


     // set up viewpager and display the first tab 

     // ViewPager for tabs 
     ViewPager viewPager = (ViewPager) findViewById(R.id.forecast_pager); 
     adapterViewPager = new ForecastPagerAdapter(getSupportFragmentManager(), ForecastFaveRowClickedActivity.this); 
     viewPager.setOffscreenPageLimit(7); 

     // set the tab titles based on the forecast list for this district    
     String[] days = new String[7]; 
     int count = 0; 
     for (ForecastEntry forecastEntry : forecastEntries) { 
      days[count] = forecastEntry.forecastDay.substring(0,3); 
      count++; 
     } 
     adapterViewPager.setTabTitles(days); 

     viewPager.setAdapter(adapterViewPager); 
     //viewPager.setCurrentItem(0); // set to today's tab to start with    

     // give the tab layout to the viewpager 
     TabLayout tabLayout = (TabLayout) findViewById(R.id.forecast_sliding_date_tabs); 
     tabLayout.setupWithViewPager(viewPager); 

    } 

    // class for view pager adapter 

    public class ForecastPagerAdapter extends FragmentPagerAdapter { 
     private int NUM_ITEMS = 7; 
     private String tabTitles[] = new String[7]; 
     private Context context;    

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

     public ForecastPagerAdapter(FragmentManager fragmentManager, Context context) { 
      super(fragmentManager); 
      this.context = context; 
     } 

     // returns total # of pages 
     @Override 
     public int getCount() { 
      return NUM_ITEMS; 
     } 

     // returns the fragment to display for that page 
     @Override 
     public Fragment getItem(int position) { 
      Fragment frag = new Fragment(); 
      ForecastEntry forecastEntry; 
      Log.v("Fragment.getItem", Integer.toString(position)); 
      forecastEntry = ((ForecastFaveRowClickedActivity)context).forecastEntries.get(position); 

      frag = FragmentForecastDay.newInstance(position, forecastEntry);      
      return frag;      
     } 

     // returns the page title for the top indicator 
     @Override 
     public CharSequence getPageTitle(int position) { 
      return tabTitles[position]; 
     } 

     public void setTabTitles(String[] days) { 
      tabTitles = Arrays.copyOf(days, days.length); 
     }  
    }   
} 

這是該活動的佈局:

<?xml version="1.0" encoding="utf-8"?> 
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android" 
               xmlns:app="http://schemas.android.com/apk/res-auto" 
               xmlns:tools="http://schemas.android.com/tools" 
               android:layout_width="match_parent" 
               android:layout_height="match_parent" 
               android:fitsSystemWindows="true" 
               tools:context="com.districtoverview.ForecastFaveRowClickedActivity"> 


    <android.support.design.widget.AppBarLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:theme="@style/AppTheme.AppBarOverlay"> 

     <android.support.v7.widget.Toolbar 
      android:id="@+id/toolbar" 
      android:layout_width="match_parent" 
      android:layout_height="?attr/actionBarSize" 
      android:background="?attr/colorPrimary" 
      android:titleTextColor="@color/colorWhite" 
      android:elevation="4dp" 
      android:theme="@style/AppTheme.AppBarOverlay" 
      app:popupTheme="@style/AppTheme.PopupOverlay" /> 
    </android.support.design.widget.AppBarLayout>    

    <LinearLayout 
     xmlns:android="http://schemas.android.com/apk/res/android" 
     xmlns:app="http://schemas.android.com/apk/res-auto" 
     xmlns:tools="http://schemas.android.com/tools" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:paddingBottom="6dp" 
     android:paddingTop="6dp" 
     app:layout_behavior="@string/appbar_scrolling_view_behavior" 
     tools:context="com.districtoverview.ForecastFaveRowClickedActivity" 
     tools:showIn="@layout/activity_forecast_detail" 
     android:orientation="vertical"> 

     <android.support.design.widget.TabLayout 
      android:id="@+id/forecast_sliding_date_tabs" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_gravity="top" 
      android:paddingBottom="2dp" 
      android:paddingTop="2dp" 
      app:tabMode="scrollable" 
      app:tabGravity="fill" 
      style="@style/customForecastTabLayout" /> 

     <android.support.v4.view.ViewPager 
      xmlns:android="http://schemas.android.com/apk/res/android" 
      android:id="@+id/forecast_pager" 
      android:layout_width="match_parent" 
      android:layout_height="0px" 
      android:layout_weight="1" 
      android:background="@android:color/white" 
      tools:context="com.districtoverview.ForecastFaveRowClickedActivity"> 
     </android.support.v4.view.ViewPager>     
    </LinearLayout> 

</android.support.design.widget.CoordinatorLayout> 

這是片段類的代碼。

public class FragmentForecastDay extends Fragment { 

    String label; 
    int page; 
    ForecastEntry forecastEntry; 

    public static FragmentForecastDay newInstance(int page, ForecastEntry forecastEntry) {    
     FragmentForecastDay fragment = new FragmentForecastDay(); 
     Bundle args = new Bundle(); 
     args.putParcelable("forecastEntry", forecastEntry); 
     args.putInt("page", page); 
     fragment.setArguments(args);  
     return fragment; 
    } 


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

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

     page = getArguments().getInt("page", 0); 
     forecastEntry = getArguments().getParcelable("forecastEntry"); 

     return view;    
    } 


    @Override 
    public void onViewCreated(View view, Bundle savedInstanceState) { 

     super.onViewCreated(view, savedInstanceState); 

     TextView headerDistNameTV = (TextView) getActivity().findViewById(R.id.dist_name_header_text); 
     headerDistNameTV.setText(parentActivity.distName); 

     TextView headerForecastDateTV = (TextView) getActivity().findViewById(R.id.forecast_date_header_text);    
     headerForecastDateTV.setText(forecastEntry.forecastDate); 

     TextView headerTotalTechsTV = (TextView) getActivity().findViewById(R.id.total_techs_header_text);      headerTotalTechsTV.setText(Double.toString(forecastEntry.totalTechs)); 

     TextView headerDayShiftTV = (TextView) getActivity().findViewById(R.id.day_shift_header_text); 
     headerDayShiftTV.setText("Day Shift"); 

     TextView dayExpectedTechsTV = (TextView) getActivity().findViewById(R.id.day_shift_expected_text);    
     String dayExpectedTechs = "Expected " + Double.toString(forecastEntry.expectedTechs); 
     dayExpectedTechsTV.setText(dayExpectedTechs);     

     TextView headerAfterHoursTV = (TextView) getActivity().findViewById(R.id.after_hours_header_text); 
     headerAfterHoursTV.setText("After Hours"); 

     TextView afterHoursExpectedTechsTV = (TextView) getActivity().findViewById(R.id.day_shift_expected_text);  
     String afterHoursExpectedTechs = "Expected " + Double.toString(forecastEntry.afterHoursExpected); 
     afterHoursExpectedTechsTV.setText(afterHoursExpectedTechs);      
    }   
} 

這是片段的佈局:

<?xml version="1.0" encoding="utf-8"?> 

    <TableLayout 
     xmlns:android="http://schemas.android.com/apk/res/android" 
     android:id="@+id/forecast_day_table_layout" 
     android:orientation="vertical" 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_weight="0.90" 
     android:paddingTop="4dp" 
     android:paddingBottom="4dp" 
     android:paddingLeft="4dp" 
     android:paddingRight="4dp" 
     android:weightSum="1"> 
     <TableRow 
      android:id="@+id/row_dist_name_header" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_weight="0.50"> 
      <TextView 
       android:id="@+id/dist_name_header_text" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:textColor="#000" 
       android:textAppearance="?android:attr/textAppearanceLarge" 
       android:paddingBottom="5dp" /> 
     </TableRow> 
     <TableRow 
      android:id="@+id/row_date_header" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_weight="0.50"> 
      <TextView 
       android:id="@+id/forecast_date_header_text" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:textColor="#000" 
       android:textAppearance="?android:attr/textAppearanceLarge" 
       android:paddingBottom="5dp" /> 
     </TableRow> 
     <TableRow 
      android:id="@+id/row_total_techs_header" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_weight="0.50"> 
      <TextView 
       android:id="@+id/total_techs_header_text" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:textColor="#000" 
       android:textAppearance="?android:attr/textAppearanceLarge" 
       android:paddingBottom="5dp" /> 
     </TableRow> 
     <TableRow 
      android:id="@+id/row_total_techs" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_weight="0.50"> 
      <TextView 
       android:id="@+id/total_techs_text" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:textColor="#000" 
       android:textAppearance="?android:attr/textAppearanceLarge" 
       android:paddingBottom="5dp" /> 
     </TableRow> 
     <TableRow 
      android:id="@+id/row_day_shift_header" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_weight="0.50"> 
      <TextView 
       android:id="@+id/day_shift_header_text" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:textColor="#000" 
       android:textAppearance="?android:attr/textAppearanceLarge" 
       android:paddingBottom="5dp" /> 
     </TableRow> 
     <TableRow 
      android:id="@+id/row_day_shift_expected" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_weight="0.50"> 
      <TextView 
       android:id="@+id/day_shift_expected_text" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:textColor="#000" 
       android:textAppearance="?android:attr/textAppearanceLarge" 
       android:paddingBottom="5dp" /> 
     </TableRow>   
     <TableRow 
      android:id="@+id/row_after_hours_header" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_weight="0.50"> 
      <TextView 
       android:id="@+id/after_hours_header_text" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:textColor="#000" 
       android:textAppearance="?android:attr/textAppearanceLarge" 
       android:paddingBottom="5dp" /> 
     </TableRow> 
     <TableRow 
      android:id="@+id/row_after_hours_expected" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_weight="0.50"> 
      <TextView 
       android:id="@+id/after_hours_expected_text" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:textColor="#000" 
       android:textAppearance="?android:attr/textAppearanceLarge" 
       android:paddingBottom="5dp" /> 
     </TableRow> 
    </TableLayout> 

回答

1

這是由於我錯誤地使用getActivity()而不是視圖造成的。我從另一組滑動標籤中複製了代碼,這些標籤位於主要活動中,但是這個集合是在一個片段中。我需要引用父片段的視圖。

因此,例如這樣的:

(TextView) getActivity().findViewById(R.id.dist_name_header_text); 

本來應該是這樣的:

(TextView) view.findViewById(R.id.dist_name_header_text); 

一旦我做了這個變化的看法尋呼機使用的所有子片段正確顯示。

+0

帽子關閉,浪費我的8-10小時誤導搜索, –

0

一個潛在的問題可能是所有的七個片段不viewpager的creation.so您的活動代碼的時間正確創建將如圖6-8所示

//設置viewpager並顯示第一選項卡

// ViewPager for tabs 
    ViewPager viewPager = (ViewPager) findViewById(R.id.forecast_pager); 
    adapterViewPager = new ForecastPagerAdapter(getSupportFragmentManager(), ForecastFaveRowClickedActivity.this); 

    viewPager.setOffscreenPageLimit(7); 
+0

我感動的是排隊,因爲你已經在這裏顯示,但不幸的是它有對應用程序的行爲沒有任何影響。 – JDC

相關問題