gpt4 book ai didi

java - 从 Android Studio 中的 fragment 中单击 recyclerview 中的cardview

转载 作者:行者123 更新时间:2023-12-02 04:25:15 24 4
gpt4 key购买 nike

我正在创建一个电影应用程序用于学习目的。我在单击卡片 View 以将我从 fragment 引导至另一个 Activity 时遇到问题。

目前,我有一个 fragment ,可以通过改造显示来自 api 的电影。用于处理处理的适配器和用于单击事件的接口(interface)。

这是适配器

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);
}

下面是 fragment 类

 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;
}
}
}

我也直接从fragment中的cardview中点击,但失败了

它应该至少举杯作为测试。

谢谢。

最佳答案

请尝试这个。

@Override
public void onItemClick(int adapterPosition, View view) {
switch (view.getId()){
case R.id.movie_cardview_linearlayout:
Toast.makeText(getActivity(), "Card View Clicked", Toast.LENGTH_SHORT).show();
break;
}
}

关于java - 从 Android Studio 中的 fragment 中单击 recyclerview 中的cardview,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56610469/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com