0

Ich mache eine App und die Hauptaktivität hat ein Tablayout, das mit einem Viewpager verbunden ist. Dieser Viewpager hat drei Fragmente.ViewPager zeigt keine Fragmente an

Ich weiß, dass der Viewpager-Adapter 3 Fragmente verwaltet (die richtige Menge), weil ich Logging-Anweisungen verwendet habe. Außerdem verwende ich asyncTasks, um Daten in die Fragmente zu laden, und es wird korrekt ausgeführt. Wenn die Daten geladen sind, füllt sie eine Recyclerview. Ich habe Logging-Anweisungen verwendet und weiß, dass jedes Fragment eine Recyclerview mit 20 Einträgen hat, was korrekt ist.

Aber aus irgendeinem Grund zeigt mein Viewpager die Fragmente nicht an, auch wenn ich getItemCount() darauf anrufe, bekomme ich die richtige Anzahl von Fragmenten.

Können Sie mir helfen herauszufinden, warum die Fragmente erstellt, aber nicht angezeigt werden?

Vielen Dank.

Das ist mein 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); 
    } 
} 

Dies ist das Fragment, das der viewpager 3 Mal instanziiert soll:

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); 
     } 
    } 
} 

Pager-Adapter:

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"); 
     } 
    } 
} 

Nur für den Fall: diese sind die XML-Dateien für die Aktivität und das Fragment:

<?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" /> 

Antwort

1

Ihre Ansicht zurück, die Sie in Fragmente aufblasen,

@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

Sie haben völlig Recht. Vielen vielen Dank. –

+0

Als kleiner Tipp, ich habe viele Leute gesehen, die mit einer Modifikation innerhalb des Fragments kämpfen. In diesem Fall müssen Sie nur rootView in den Typ des View-Objekts umwandeln, das Sie instanziieren möchten, z. B. für ein TextView: myText = (TextView) rootView.findViewById (R.id ...); dann verarbeite die gewünschte Modifikation wie myText.setText ("...") .. zum Beispiel – Amesys

Verwandte Themen