Я создаю приложение для фильма в учебных целях.У меня проблема с щелчком по просмотру карты, чтобы направить меня к другой активности из фрагмента.
В настоящее время у меня есть фрагмент, который показывает фильмы из API с модификацией.адаптер для обработки и интерфейс для события щелчка.
Это адаптер
public class MoviesAdapter extends RecyclerView.Adapter<MoviesAdapter.ViewHolder> {
private String IMAGE_BASE_URL = "https://image.tmdb.org/t/p/w500/";
private List<Movie> movieList;
private OnRecyclerViewItemClickListener onRecyclerViewItemClickListener;
public MoviesAdapter(List<Movie> movieList){
this.movieList = movieList;
}
@NonNull
@Override
public MoviesAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.movie_card, viewGroup, false);
return new MoviesAdapter.ViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull MoviesAdapter.ViewHolder movieViewHolder, int i) {
movieViewHolder.bind(movieList.get(i));
}
@Override
public int getItemCount() {
return movieList.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
ImageView poster;
TextView movietitle, moviereleasedate, movierating;
LinearLayout linearLayout;
public ViewHolder(@NonNull View itemView) {
super(itemView);
poster = itemView.findViewById(R.id.cardview_image);
movietitle = itemView.findViewById(R.id.cardview_movietitle);
moviereleasedate = itemView.findViewById(R.id.cardview_releasedate);
movierating = itemView.findViewById(R.id.cardview_rating);
linearLayout = itemView.findViewById(R.id.movie_cardview_linearlayout);
}
public void bind(Movie movie){
movietitle.setText(movie.getTitle());
moviereleasedate.setText(movie.getReleaseDate().split("-")[0]);
movierating.setText(String.valueOf(movie.getRating()));
Picasso.get()
.load(IMAGE_BASE_URL+movie.getPosterPath())
.resize(96,128)
.placeholder(R.color.colorNotSelected)
.error(R.drawable.ic_launcher_foreground)
.into(poster);
linearLayout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (onRecyclerViewItemClickListener != null)
onRecyclerViewItemClickListener.onItemClick(getAdapterPosition(),v);
}
});
}
}
public void setOnRecyclerViewItemClickListener(OnRecyclerViewItemClickListener onRecyclerViewItemClickListener){
this.onRecyclerViewItemClickListener = onRecyclerViewItemClickListener;
}
}
Ниже представлен интерфейс для щелчка
public interface OnRecyclerViewItemClickListener {
void onItemClick(int adapterPosition, View view);
}
Ниже приведен класс фрагмента
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
final View view = inflater.inflate(R.layout.fragment_now_playing, container, false);
//Initialization of layout
ll = view.findViewById(R.id.movie_cardview_linearlayout);
recyclerView = view.findViewById(R.id.recyclerView_now_playing_fragment);
final LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL,false);
recyclerView.setLayoutManager(linearLayoutManager);
//Getting retrofit client
final Service service = Client.getClient().create(Service.class);
Call<MoviesResponse> call = service.getNowPlayingMovies(BuildConfig.THE_MOVIE_DB_API_TOKEN);
//getting data
call.enqueue(new Callback<MoviesResponse>() {
@Override
public void onResponse(Call<MoviesResponse> call, Response<MoviesResponse> response) {
if (response.isSuccessful()){
List<Movie> movies = response.body().getMovies();
if (movies.size()>0){
final MoviesAdapter moviesAdapter = new MoviesAdapter(movies);
// Click does not work
// If I did it from activity I just put context of activity as an argument
moviesAdapter.setOnRecyclerViewItemClickListener(//);
recyclerView.setAdapter(moviesAdapter);
}
}
}
@Override
public void onFailure(Call<MoviesResponse> call, Throwable t) {
Log.e(TAG, "onFailure: ", t);
}
});
return view;
}
@Override
public void onItemClick(int adapterPosition, View view) {
switch (view.getId()){
case R.id.main_cardview:
Toast.makeText(getActivity(), "Card View Clicked", Toast.LENGTH_SHORT).show();
break;
}
}
}
Я также щелкаю непосредственно из карты в фрагменте, но она не удалась
В качестве теста должен быть хотя бы тост.
Спасибо.