我在我的android应用程序中嵌套了RecyclerViews,我一直在努力实现OnClickListener和OnFocusChangeListener方法。我有以下3个文件来处理我的设置:
MainActivity
public class MainActivity extends AppCompatActivity {
private RecyclerView mGenreRecycler;
private GenreAdapter mGenreAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Bind the views of our RecyclerView
bindViews();
//Set up the Genre RecyclerView
setUpGenreRecycler();
}
private void bindViews() {
// Bind Genre recycler
mGenreRecycler = findViewById(R.id.genre_recycler);
}
private void setUpGenreRecycler () {
mGenreRecycler.setLayoutManager(newLinearLayoutManager(this,
LinearLayoutManager.VERTICAL,false));
// For smooth scrolling
mGenreRecycler.setNestedScrollingEnabled(false);
mGenreAdapter = new GenreAdapter(this, Genre.GenreList);
mGenreRecycler.setAdapter(mGenreAdapter);
}
}
GenreAdapter(RecyclerView垂直方向的第一级)
public class GenreAdapter extends RecyclerView.Adapter<GenreAdapter.GenreVH> {
private static int INITIAL_PREFETCH_COUNT = 3;
private Context context;
private List<Genre> genreList;
private LayoutInflater layoutInflater;
private SparseArray<GenreInnerAdapter> genreInnerAdapters;
private SparseArray<List<Movie>> genreWiseMovies;
private RecyclerView.RecycledViewPool recyclerViewPool;
public GenreAdapter(Context context, final List<Genre> genreList) {
this.context = context;
this.genreList = genreList;
this.layoutInflater = LayoutInflater.from(context);
recyclerViewPool = new RecyclerView.RecycledViewPool();
// Initialize all adapters
this.genreInnerAdapters = new SparseArray<>(genreList.size());
for (Genre genre: genreList) {
genreInnerAdapters.put(genre.getId(),new GenreInnerAdapter(context, newArrayList<Movie()));
}
// Send request for movie list
this.genreWiseMovies = new SparseArray<>(genreList.size());
populateGenreWiseMovies();
}
private void populateGenreWiseMovies() {
ApiClient.TheMovieDBApiInterface service = ApiClient.getTheMovieDBApiInterface();
for (final Genre genre:genreList) {
Call<DiscoverResult> call = service.
getTopMoviesByGenre(context.getString(R.string.api_key),
Constants.QueryDefaultVal.LANG,
Constants.QueryDefaultVal.REGION, Constants.QueryDefaultVal.SORT_BY,
Constants.QueryDefaultVal.CERT_COUNTRY, Constants.QueryDefaultVal.PAGE_NO,
2019, Collections.singletonList(genre.getId()));
call.enqueue(new Callback<DiscoverResult>() {
@Override
public void onResponse(Call<DiscoverResult> call, Response<DiscoverResult> response)
{
List<Movie> movieList = response.body().getResults();
genreWiseMovies.put(genre.getId(),movieList);
genreInnerAdapters.get(genre.getId()).setMovieList(movieList);
genreInnerAdapters.get(genre.getId()).notifyDataSetChanged();
Log.d("MainActivity", "movie list size for genre " +
genre.getName() + ":" + movieList.size());
}
@Override
public void onFailure(Call<DiscoverResult> call, Throwable t) {
Log.e("GenreAdapter", "unable to get movies by genre: " +
genre.getName() + " error: " + t.getMessage());
}
});
}
}
@Override
public GenreVH onCreateViewHolder(ViewGroup parent, int viewType) {View view = layoutInflater.inflate(R.layout.genere_unit, parent, false);
GenreVH genreVH = new GenreVH(view);
genreVH.genreInnerRecycler.setRecycledViewPool(recyclerViewPool);
genreVH.genreInnerRecycler.setNestedScrollingEnabled(false);
return genreVH;
}
@Override
public void onBindViewHolder(GenreVH holder, int position) {
/* Set Genre Heading */
holder.genreHeading.setText(genreList.get(position).getName());
/* set layout manager to inner recycler*/
if(holder.genreInnerRecycler.getLayoutManager() == null){
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(context,
LinearLayoutManager.HORIZONTAL, false);
linearLayoutManager.setInitialPrefetchItemCount(INITIAL_PREFETCH_COUNT);
holder.genreInnerRecycler.setLayoutManager(linearLayoutManager);
}
/* set adapter to inner recycler */
holder.genreInnerRecycler.setAdapter(genreInnerAdapters.get(genreList.get(position).getId()));
}
@Override
public int getItemCount() {
return genreList.size();
}
public class GenreVH extends RecyclerView.ViewHolder {
private TextView genreHeading;
private RecyclerView genreInnerRecycler;
public GenreVH(View itemView) {
super(itemView);
genreHeading = itemView.findViewById(R.id.genre_heading);
genreInnerRecycler = itemView.findViewById(R.id.genre_inner_recycler);
}
}
}
GenreInnerAdapter(嵌套的RecyclerView方向为水平)
public class GenreInnerAdapter extends RecyclerView.Adapter<GenreInnerAdapter.GenreInnerVH> {
private Context context;
private List<Movie> movieList;
private LayoutInflater layoutInflater;
private String imageBaseUrl = "https://image.tmdb.org/t/p/w500/";
public GenreInnerAdapter(Context context, List<Movie> movieList) {
this.context = context;
this.movieList = movieList;
this.layoutInflater = LayoutInflater.from(context);
}
public void setMovieList(List<Movie> movieList){
this.movieList = movieList;
}
@Override
public GenreInnerVH onCreateViewHolder(ViewGroup parent, int viewType) {
View view = layoutInflater.inflate(R.layout.genere_inner_unit, parent, false);
return new GenreInnerVH(view);
}
@Override
public void onBindViewHolder(GenreInnerVH holder, int position) {
String bannerImageUrl = imageBaseUrl + movieList.get(position).getPosterPath();
Glide.with(context)
.load(bannerImageUrl)
.fallback(R.drawable.genre_image_fallback)
.error(R.drawable.genre_image_fallback)
.diskCacheStrategy(DiskCacheStrategy.ALL)
.into(holder.movieBanner);
}
@Override
public int getItemCount() {
return movieList.size();
}
public class GenreInnerVH extends RecyclerView.ViewHolder {
private ImageView movieBanner;
private TextView movieTitle;
public GenreInnerVH(View itemView) {
super(itemView);
movieBanner = itemView.findViewById(R.id.genre_movie_image);
movieTitle = itemView.findViewById(R.id.genre_movie_title);
}
}
}
我想返回具有焦点或被单击的视图的movieTitle内容。有人可以为如何实现此目的提供建议,也可以修改代码以使其难以理解其实现方式吗?