我遇到了RecyclerView
與GridLayoutManager
不適用於onClick()
的情況。像ListView
,當我點擊/點擊GridView中的圖片時,onClick不會返回單個項目的詳細信息。當我點擊一部電影的海報時,它應該只顯示關於特定電影的詳細信息,而是在一個滾動視圖中顯示多個電影的詳細信息。Android RecyclerView GridLayoutManager onClick現在正在工作
看看下面的代碼:
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;
// return new PosterViewHolder(view);
}
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;
// Context zContext = context;
public PosterViewHolder(final View itemView) {
super(itemView);
// zContext = itemView.getContext();
poster = (ImageView) itemView.findViewById(R.id.movies_image_view_retrofit);
// itemView.setOnClickListener(this);
}
/*@Override
public void onClick(View view) {
Movies movies = movieItems.get(getAdapterPosition());
Intent intent = new Intent(zContext, MoviesDetailActivity.class);
intent.putExtra("Intent", movies);
zContext.startActivity(intent);
}*/
}
}
而且下面是片段:你們的關注
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) {
}
}
);
}
}
感謝。我正在尋找的解決方案是:當我點擊網格視圖中的圖片時,我需要關於諸如標題,概述,發佈日期等特定電影的詳細信息。
不能。沒有爲我工作。我嘗試了你的建議。 'onClick'本身就很好。我需要像'ListIView'中的'OnItemSelectedClick'方法。 – Ronan
對不起,它應該是onBindViewHolder中的holder.itemView.setOnClickListener。元素的視圖保存在ViewHolder的itemView字段中,因此您可以通過將元素設置爲itemView來將偵聽器添加到元素中。 –
'holder.mView.setOnClickListener(新View.OnClickListener(){ @Override 公共無效的onClick(查看視圖){ 意圖意圖=新意圖(上下文,MovieDetailsActivity.class); intent.putExtra( 「標題」, movies.getTitle()); intent.putExtra(「Overview」,movies.getOverview()); context.startActivity(intent); } });' 這是行不通的。當我點擊時,它意圖展示該活動,但它在NestedScrollView中列出了20秒。我只想要關於一部電影的細節。 – Ronan