我遇到了RecyclerView
GridLayoutManager
onClick()
无效ListView
的情况。与public class MoviesGridAdapter extends RecyclerView.Adapter<MoviesGridAdapter.PosterViewHolder> {
private Context context;
private List<Movies> movieItems = new ArrayList<>();
CustomItemClickListener listener;
public MoviesGridAdapter(Context context, List<Movies> movieItems) {
this.context = context;
this.movieItems = movieItems;
}
@Override
public PosterViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.movies_grid_view_item, parent, false);
final MoviesGridAdapter.PosterViewHolder vh = new MoviesGridAdapter.PosterViewHolder(view);
view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
final Context zContext = context;
Intent intent = new Intent(zContext, MoviesDetailActivity.class);
Movies movies = movieItems.get(vh.getAdapterPosition());
intent.putExtra("Items Selection", movies.getPosterPath());
intent.putExtra("Items Selection 1", movies.getTitle());
intent.putExtra("Items Selection 2", movies.getReleaseDate());
intent.putExtra("Items Selection 3", movies.getVoteAverage());
intent.putExtra("Items Selection 4", movies.getOverview());
zContext.startActivity(intent);
}
});
return vh;
}
public void setMoviesData(List<Movies> moviesData) {
movieItems = moviesData;
notifyDataSetChanged();
}
@Override
public void onBindViewHolder(PosterViewHolder holder, final int position) {
Context mcontext = context;
Movies movies = movieItems.get(position);
String posterUrlPath = movies.getPosterPath();
Picasso.with(mcontext)
.load(Constants.MOVIES_POSTER_URL + posterUrlPath)
.into(holder.poster);
}
@Override
public int getItemCount() {
return movieItems.size();
}
public class PosterViewHolder extends RecyclerView.ViewHolder {
ImageView poster;
public PosterViewHolder(final View itemView) {
super(itemView);
poster = (ImageView) itemView.findViewById(R.id.movies_image_view_retrofit);
}
}
}
类似,当我点击/单击GridView中的图片时,onClick不会返回单个项目的详细信息。当我点击电影的海报时,它应该只显示有关特定电影的详细信息,而是在滚动视图中显示多个电影细节。
请看下面的代码:
public class MoviesFragment extends Fragment {
private static final int COLUMN_COUNT = 2;
private List<Movies> movieItems = new ArrayList<>();
private RecyclerView recyclerView;
private MoviesGridAdapter adapter;
private Subscription subscription;
public MoviesFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.movies_fragment_content, container, false);
GridLayoutManager layoutManager = new GridLayoutManager(getContext(), COLUMN_COUNT);
recyclerView = (RecyclerView) view.findViewById(R.id.movies_recycler_view);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(layoutManager);
adapter = new MoviesGridAdapter(getActivity(), movieItems);
recyclerView.setAdapter(adapter);
return view;
}
@Override
public void onStart() {
super.onStart();
if (movieItems != null) {
adapter.setMoviesData(movieItems);
}
loadDate();
}
private void loadDate() {
MoviesAPI api = RetrofitManager.getMoviesClient().create(MoviesAPI.class);
subscription = api.getPopularMovies(APIKeys.MOVIES_DB_API_KEY)
.observeOn(AndroidSchedulers.mainThread())
.subscribeOn(Schedulers.io())
.subscribe(
new Action1<MoviesResponse>() {
@Override
public void call(MoviesResponse response) {
movieItems = response.getMovies();
adapter.setMoviesData(response.getMovies());
}
}, new Action1<Throwable>() {
@Override
public void call(Throwable throwable) {
}
}
);
}
}
以下是片段:
{{1}}
谢谢大家的关注。我正在寻找的解决方案是:当我从网格视图中点击图片时,我需要有关特定电影的详细信息,如title,overview,releaseDate等。
答案 0 :(得分:2)
使您的模型可以设置或逐个发送您的模型字段,但使用不同的关键字。您在此处添加了具有不同值的相同关键字:
intent.putExtra("Items Selection", movies.getPosterPath());
intent.putExtra("Items Selection", movies.getTitle());
intent.putExtra("Items Selection", movies.getReleaseDate());
intent.putExtra("Items Selection", movies.getVoteAverage());
intent.putExtra("Items Selection", movies.getOverview());
这是一个指导如何制作模型parcelable的教程链接。之后,您可以按照以下步骤更新适配器:
public class MoviesGridAdapter extends RecyclerView.Adapter<MoviesGridAdapter.PosterViewHolder> {
private Context context;
private List<Movies> movieItems = new ArrayList<>();
CustomItemClickListener listener;
public MoviesGridAdapter(Context context, List<Movies> movieItems) {
this.context = context;
this.movieItems = movieItems;
}
@Override
public PosterViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.movies_grid_view_item, parent, false);
return new PosterViewHolder(view);
}
public void setMoviesData(List<Movies> moviesData) {
movieItems = moviesData;
notifyDataSetChanged();
}
@Override
public void onBindViewHolder(PosterViewHolder holder, final int position) {
Movies movie = movieItems.get(position);
String posterUrlPath = movie.getPosterPath();
Picasso.with(mcontext)
.load(Constants.MOVIES_POSTER_URL + posterUrlPath)
.into(holder.poster);
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(context, MoviesDetailActivity.class);
intent.putExtra("movie", movie)
context.startActivity(intent);
}
});
}
@Override
public int getItemCount() {
return movieItems.size();
}
public class PosterViewHolder extends RecyclerView.ViewHolder {
ImageView poster;
public PosterViewHolder(View itemView) {
super(itemView);
poster = (ImageView) itemView.findViewById(R.id.movies_image_view_retrofit);
}
}
}
答案 1 :(得分:1)
请勿在{{1}}中设置点击监听器。而是在onCreateViewHolder
方法