我已经实现了ViewPager,RecyclerView和ContentProvider的基本设置。我似乎能够毫无问题地查询数据,但我无法在ViewPager选项卡中使用RecyclerView显示内容。
此处提供完整代码 - 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 :(得分:0)
我可能错了,但我认为您需要在onViewCreated中设置适配器。
答案 1 :(得分: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;
}
}
答案 2 :(得分:0)
我正在看项目并且:
MovieAdapter
没问题,因为我在onCreateViewHolder
和onBindViewHolder
中设置了断点并且它们被击中了。似乎问题在于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>
证据:
答案 3 :(得分: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();
这是错误的。
有多种修复方法。