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();这是错误的。

有多个修复程序。

  1. 切换到的LinearLayout
  2. 设置ImageView的修复的高度。
  3. 确保图像没有损坏。
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

我一直在寻找项目和:

  • MovieAdapter是好的,因为我把断点onCreateViewHolderonBindViewHolder他们被击中。
  • 我无法验证查询是否正常,但可能通过查看您发布的日志记录来确定。

的实际问题......也许

,这似乎是问题是,list_item.xml布局的东西!我将其更改为LinearLayout而不是RelativeLayout,现在它可以工作。

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'。由于输入错误,图片链接被破坏。如果没有Image,则ImageView将不会显示。这就是发生了什么事。 –