0

私はViewPager、RecyclerView、およびContentProviderの基本設定を実装しました。問題なくデータを照会できるようですが、ViewPagerタブでRecyclerViewを使用してコンテンツを表示することができません。RecyclerViewがViewPagerタブにデータを入力しない

完全なコードはここにあります - https://github.com/sudhirkhanger/PopularMovies

public class FavoriteFragment extends Fragment { 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
          Bundle savedInstanceState) { 
     View rootView = inflater.inflate(R.layout.fragment_movie_list, container, false); 

     // Set column size to 2 for default and portrait 
     // and 3 for landscape orientations 
     int column = Integer.parseInt(getString(R.string.grid_portrait)); 
     if (getResources().getConfiguration().orientation == 1) { 
      column = Integer.parseInt(getString(R.string.grid_portrait)); 
     } else if (getResources().getConfiguration().orientation == 2) { 
      column = Integer.parseInt(getString(R.string.grid_landscape)); 
     } 

     mRecyclerView = (RecyclerView) rootView.findViewById(R.id.recyclerview); 
     mRecyclerView.setHasFixedSize(true); 
     mRecyclerView.setLayoutManager(new GridLayoutManager(getActivity(), column)); 

     ArrayList<Movie> mMovieArrayList = new ArrayList<Movie>(); 

     ContentResolver resolver = getActivity().getContentResolver(); 

     Cursor cursor = 
       resolver.query(MovieContract.MovieEntry.CONTENT_URI, 
         null, 
         null, 
         null, 
         null); 

     if (cursor != null) { 
      if (cursor.moveToFirst()) { 
       do { 
        String title = cursor.getString(cursor.getColumnIndex(MovieContract.MovieEntry.TITLE)); 
        String movie_id = cursor.getString(cursor.getColumnIndex(MovieContract.MovieEntry.MOVIE_ID)); 
        String poster = cursor.getString(cursor.getColumnIndex(MovieContract.MovieEntry.POSTER)); 
        String backdrop = cursor.getString(cursor.getColumnIndex(MovieContract.MovieEntry.BACKDROP)); 
        String overview = cursor.getString(cursor.getColumnIndex(MovieContract.MovieEntry.OVERVIEW)); 
        String vote_average = cursor.getString(cursor.getColumnIndex(MovieContract.MovieEntry.VOTE_AVERAGE)); 
        String release_date = cursor.getString(cursor.getColumnIndex(MovieContract.MovieEntry.DATE)); 

        Movie movie = new Movie(title, release_date, poster, 
          vote_average, overview, backdrop, movie_id); 
        mMovieArrayList.add(movie); 
       } while (cursor.moveToNext()); 
      } 
     } 

     if (cursor != null) 
      cursor.close(); 

     for (Movie movie : mMovieArrayList) { 
      Log.d("FavoriteFragment List", movie.getTitle()); 
     } 

     mMovieAdapter = new MovieAdapter(getActivity(), mMovieArrayList); 
     mMovieAdapter.notifyDataSetChanged(); 
     mRecyclerView.setAdapter(mMovieAdapter); 
     return rootView; 
    } 

    public static FavoriteFragment newInstance() { 
     Bundle args = new Bundle(); 
     FavoriteFragment fragment = new FavoriteFragment(); 
     fragment.setArguments(args); 
     return fragment; 
    } 

    @Override 
    public void onResume() { 
     super.onResume(); 
     mRecyclerView.setAdapter(mMovieAdapter); 
     mMovieAdapter.notifyDataSetChanged(); 
    } 

    RecyclerView mRecyclerView; 
    MovieAdapter mMovieAdapter; 
} 

そのArrayListのデータが含まれているの下にあなたが見ることができるように。

04-25 22:02:36.216 4718-4718/com.sudhirkhanger.app.popularmoviesstageone W/FragmentManager: moveToState: Fragment state for PopularFragment{d7cf0b6 #1 id=0x7f0c006b} not updated inline; expected state 3 found 2 
04-25 22:02:37.757 4718-4718/com.sudhirkhanger.app.popularmoviesstageone D/FavoriteFragment List: Whiplash 
04-25 22:02:37.757 4718-4718/com.sudhirkhanger.app.popularmoviesstageone D/FavoriteFragment List: Batman v Superman: Dawn of Justice 
04-25 22:02:37.757 4718-4718/com.sudhirkhanger.app.popularmoviesstageone D/FavoriteFragment List: Star Wars: The Force Awakens 
04-25 22:02:37.757 4718-4718/com.sudhirkhanger.app.popularmoviesstageone D/FavoriteFragment List: The Shawshank Redemption 
04-25 22:02:37.757 4718-4718/com.sudhirkhanger.app.popularmoviesstageone D/FavoriteFragment List: The Godfather 
04-25 22:02:37.757 4718-4718/com.sudhirkhanger.app.popularmoviesstageone D/FavoriteFragment List: Interstellar 
04-25 22:02:37.757 4718-4718/com.sudhirkhanger.app.popularmoviesstageone D/FavoriteFragment List: Captain America: Civil War 
04-25 22:02:37.757 4718-4718/com.sudhirkhanger.app.popularmoviesstageone D/FavoriteFragment List: The Jungle Book 
04-25 22:02:37.757 4718-4718/com.sudhirkhanger.app.popularmoviesstageone D/FavoriteFragment List: The Revenant 
04-25 22:02:37.757 4718-4718/com.sudhirkhanger.app.popularmoviesstageone D/FavoriteFragment List: Jurassic World 
04-25 22:02:37.757 4718-4718/com.sudhirkhanger.app.popularmoviesstageone D/FavoriteFragment List: Mad Max: Fury Road 

答えて

0
<ImageView 
     android:id="@+id/imageview" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:adjustViewBounds="true" 
     android:scaleType="centerCrop"/> 

ImageViewの高さはwrap_contentに設定されています。画像がない場合は、特にRelativeLayoutには表示が見えなくなります。 LinearLayoutがある場合、この問題は発生しません。

DetailsFragment.javaに入力ミスがあるため、画像パスをfinal String poster = parcelableExtra.getReleaseDate();に間違って設定しました。

複数の修正があります。 LinearLayout

    1. スイッチはImageViewのための修正の高さを設定します。
    2. 画像が壊れていないことを確認してください。
  • 0

    私は間違っているかもしれませんが、あなたはonViewCreatedでアダプタを設定する必要があると思います。

    0

    まず、onCreateViewのすべてをonViewCreatedに移動します。ちょうどrootViewを返してください。それは必要ではありませんが、良いコーディング方法であり、後でライフサイクルのバグを修正しようとするときに役立ちます。また、onResumeメソッドを削除してください。助けてくれることは何もしていません。右下の次の行に

    mMovieAdapter = new MovieAdapter(getActivity(), mMovieArrayList); 
    mRecyclerView.setAdapter(mMovieAdapter); 
    

    第二に、これらの行を移動し

    ArrayList<Movie> mMovieArrayList = new ArrayList<Movie>(); 
    

    を次に、あなたはすべてのデータを使用してリストを埋めた後、伝えるためにこのラインを呼び出しますリストが変更されたことをリサイクルビュー:

    mMovieAdapter.notifyDataSetChanged(); 
    

    すべて言ったと行って、あなたはこの必要がありますので:

    public class FavoriteFragment extends Fragment { 
    
        RecyclerView mRecyclerView; 
        MovieAdapter mMovieAdapter; 
    
        @Override 
        public View onCreateView(LayoutInflater inflater, ViewGroup container, 
              Bundle savedInstanceState) { 
         return inflater.inflate(R.layout.fragment_movie_list, container, false); 
        } 
    
        @Override 
        public void onViewCreated(View v, Bundle b) { 
         // Set column size to 2 for default and portrait 
         // and 3 for landscape orientations 
         int column = Integer.parseInt(getString(R.string.grid_portrait)); 
         if (getResources().getConfiguration().orientation == 1) { 
          column = Integer.parseInt(getString(R.string.grid_portrait)); 
         } else if (getResources().getConfiguration().orientation == 2) { 
          column = Integer.parseInt(getString(R.string.grid_landscape)); 
         } 
    
         mRecyclerView = (RecyclerView) rootView.findViewById(R.id.recyclerview); 
         mRecyclerView.setHasFixedSize(true); 
         mRecyclerView.setLayoutManager(new GridLayoutManager(getActivity(), column)); 
    
         ArrayList<Movie> mMovieArrayList = new ArrayList<Movie>(); 
         mMovieAdapter = new MovieAdapter(getActivity(), mMovieArrayList); 
         mRecyclerView.setAdapter(mMovieAdapter); 
    
         /* do all of your cursor stuff to populate the mMovieArrayList */ 
         ...... 
    
         // notify adapter to recycle 
         mMovieAdapter.notifyDataSetChanged(); 
        } 
    
        public static FavoriteFragment newInstance() { 
         Bundle args = new Bundle(); 
         FavoriteFragment fragment = new FavoriteFragment(); 
         fragment.setArguments(args); 
         return fragment; 
        } 
    } 
    
    +0

    ありがとうございましたチャドが問題を解決しませんでした。 –

    0

    を私はプロジェクトを見ているとした

    • 私はonCreateViewHolderonBindViewHolderにブレークポイントを入れて、彼らのでMovieAdapterは大丈夫ですヒットしました。
    • クエリが正常であることを確認することはできませんが、投稿したログを確認することで問題はないと思われます。

    実際の問題...多分

    問題がlist_item.xmlレイアウトということであるように思わ事! RelativeLayoutの代わりにLinearLayoutに変更しました。

    list_item.xml:

    <?xml version="1.0" encoding="utf-8"?> 
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
           android:layout_width="match_parent" 
           android:layout_height="wrap_content" 
           android:orientation="vertical" 
           android:padding="1dp"> 
    
        <ImageView 
         android:id="@+id/imageview" 
         android:layout_width="match_parent" 
         android:layout_height="wrap_content" 
         android:adjustViewBounds="true" 
         android:scaleType="centerCrop"/> 
    
        <TextView 
         android:id="@+id/list_title" 
         android:layout_width="match_parent" 
         android:layout_height="@dimen/list_title_size" 
         android:layout_alignBottom="@id/imageview" 
         android:background="@color/black" 
         android:ellipsize="end" 
         android:gravity="center" 
         android:maxLines="1" 
         android:textColor="@color/white" 
         android:textSize="@dimen/list_title_text_size"/> 
    </LinearLayout> 
    

    証拠!:

    evidence

    +0

    レイアウトに問題がある可能性があることを指摘してくれてありがとう。この問題の核心は、ImageViewが 'wrap_content'に設定されていることです。入力エラーのために画像リンクが壊れました。画像がない場合、ImageViewはまったく表示されません。それは何が起こっているのですか? –

    関連する問題