0

我正在製作應用程序,主活動有一個tablayout連接到viewpager。這個viewpager有三個片段。ViewPager不顯示片段

我知道viewpager適配器管理3個片段(正確的數量),因爲我使用了Logging語句。此外,我使用asyncTasks將數據加載到碎片中,並且它正確完成。數據加載時,它會填充回收站視圖。我使用了日誌記錄,並且我知道每個片段都有一個包含20個條目的回收站,這是正確的。

但由於某種原因,我的viewpager不顯示片段,即使當我調用getItemCount()時,我也會得到正確數量的片段。

你能幫我弄清楚爲什麼碎片正在被創建但不顯示嗎?

謝謝。

這是我的MainActivity:

public class MovieListActivity extends AppCompatActivity { 

    public static final String POPULAR = "popular"; 
    public static final String TOP = "top"; 
    public static final String FAVORITES = "favorites"; 
    private static final String QUERY_TYPE = "query type"; 

    private RecyclerView mRecyclerView; 
    /** 
    * Whether or not the activity is in two-pane mode, i.e. running on a tablet 
    * device. 
    */ 
    private boolean mTwoPane; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_movie_list); 

     Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
     setSupportActionBar(toolbar); 
     toolbar.setTitle(getTitle()); 

     setupTabLayout(); 

     FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab); 
     fab.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG) 
         .setAction("Action", null).show(); 
      } 
     }); 

     if (findViewById(R.id.movie_detail_container) != null) { 
      // The detail container view will be present only in the 
      // large-screen layouts (res/values-w900dp). 
      // If this view is present, then the 
      // activity should be in two-pane mode. 
      mTwoPane = true; 
     } 
    } 

    private void setupTabLayout() { 
     TabLayout tabLayout = (TabLayout) findViewById(R.id.tablayout); 
     ViewPager viewPager = (ViewPager) findViewById(R.id.viewpager); 

     tabLayout.addTab(tabLayout.newTab().setText(POPULAR)); 
     tabLayout.addTab(tabLayout.newTab().setText(TOP)); 
     tabLayout.addTab(tabLayout.newTab().setText(FAVORITES)); 

     List<Fragment> fragments = new ArrayList<>(); 
     for (int i=0;i<3;i++) { 
      Bundle bundle = new Bundle(); 
      bundle.putInt(QUERY_TYPE, i); 
      fragments.add(ListFragment 
        .instantiate(this,ListFragment.class.getName(),bundle)); 
     } 
     viewPager.setAdapter(new MoviePagerAdapter(getSupportFragmentManager(), fragments)); 
     tabLayout.setupWithViewPager(viewPager); 
    } 
} 

這就是viewpager應該實例3倍的片段:

public class ListFragment extends Fragment { 

    private static final String QUERY_TYPE = "query type"; 
    public static final int POPULAR = 0; 
    public static final int HIGHEST_RATED = 1; 
    public static final int FAVORITES = 2; 


    private RecyclerView mRecyclerView; 
    private int mListType; 


    public ListFragment() {} 

    @Override 
    public void onCreate(@Nullable Bundle savedInstanceState) { 
     mListType = this.getArguments().getInt(QUERY_TYPE); 
     super.onCreate(savedInstanceState); 
    } 

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

     View rootView = inflater.inflate(R.layout.movie_list,container,false); 

     mRecyclerView = (RecyclerView) rootView.findViewById(R.id.movie_list); 
     assert mRecyclerView != null; 
     setupRecyclerView(mRecyclerView); 

     if (mListType == HIGHEST_RATED || mListType == POPULAR) { 
      requestMovies(); 
     } 

     return super.onCreateView(inflater, container, savedInstanceState); 
    } 


    private void setupRecyclerView(@NonNull RecyclerView recyclerView) { 
     recyclerView.setAdapter(new MovieAdapter(getActivity())); 
    } 

    void setMovies(Movie[] movies) { 
     MovieAdapter movieAdapter = (MovieAdapter)mRecyclerView.getAdapter(); 
     movieAdapter.setMovies(movies); 
     movieAdapter.notifyDataSetChanged(); 
    } 

    private void requestMovies() { 
     if (mRecyclerView.getAdapter().getItemCount() == 0) { 
      new FetchMoviesTask(this).execute(POPULAR); 
     } 
    } 
} 

尋呼機適配器:

public class MoviePagerAdapter extends FragmentStatePagerAdapter { 

    private List<Fragment> mFragments; 

    public MoviePagerAdapter(FragmentManager fm, List<Fragment> mFragments) { 
     super(fm); 
     this.mFragments = mFragments; 
    } 

    @Override 
    public Fragment getItem(int position) { 
     return this.mFragments.get(position); 
    } 

    @Override 
    public int getCount() { 
     return mFragments==null? 0 : mFragments.size(); 
    } 

    @Override 
    public CharSequence getPageTitle(int position) { 
     switch (position) { 
      case ListFragment.POPULAR: 
       return MovieListActivity.POPULAR; 
      case ListFragment.HIGHEST_RATED: 
       return MovieListActivity.TOP; 
      case ListFragment.FAVORITES: 
       return MovieListActivity.FAVORITES; 
      default: 
       throw new IllegalArgumentException("Requesting a fragment that doesn't exist"); 
     } 
    } 
} 

以防萬一:這些是活動和片段的xml文件:

<?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=".MovieListActivity"> 

<android.support.design.widget.AppBarLayout 
    android:id="@+id/app_bar" 
    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" 
     app:popupTheme="@style/AppTheme.PopupOverlay" /> 

    <android.support.design.widget.TabLayout 
     android:id="@+id/tablayout" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content"/> 

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

<android.support.v4.view.ViewPager 
    android:id="@+id/viewpager" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    app:layout_behavior="@string/appbar_scrolling_view_behavior"> 

</android.support.v4.view.ViewPager> 

<android.support.design.widget.FloatingActionButton 
    android:id="@+id/fab" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_gravity="bottom|end" 
    android:layout_margin="@dimen/fab_margin" 
    android:src="@android:drawable/ic_dialog_email" /> 

<?xml version="1.0" encoding="utf-8"?> 
<android.support.v7.widget.RecyclerView 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:id="@+id/movie_list" 
    android:name="com.example.madelenko.app.moviegami.MovieListFragment" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    app:layoutManager="GridLayoutManager" 
    app:spanCount="2" 
    tools:context="com.example.madelenko.app.moviegami.MovieListActivity" 
    tools:listitem="@layout/movie_list_content" /> 

回答

1

返回您在片段虛增您的看法,

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

    View rootView = inflater.inflate(R.layout.movie_list,container,false); 
    return rootView ; 
} 
+0

你是絕對正確的。非常非常感謝你。 –

+0

作爲一個小技巧,我看到很多人在片段中的修改中掙扎。在這種情況下,你只需要將rootView投入你想要實例化它的View對象類型,例如對於TextView:myText =(TextView)rootView.findViewById(R.id ...);然後處理您需要的修改,如myText.setText(「...」)..例如 – Amesys