我在使用recyclerview时遇到了一些麻烦:
public class FiltersAdapter extends RecyclerView.Adapter{
private Context context;
private List<String> genres;
private ArrayList<ArrayList<String>> genreListValues;
private Example values;
boolean[] checked;
private final int TYPE_ITEM1 = 0;
private final int TYPE_ITEM2 = 1;
FiltersAdapter(Context context, Example values, List<String> genres){
this.context = context;
this.genres = genres;
this.values = values;
checked = new boolean[values.getValues().size()];
}
@Override
public int getItemViewType(int position) {
if (position==0)
return TYPE_ITEM1;
return TYPE_ITEM2;
}
// private boolean isPositionHeader(int position) {
//
// }
@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
if(i == TYPE_ITEM1){
View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.filter_title_list_item, viewGroup, false);
return new FilterTitleViewHolder(v);
}else if (i == TYPE_ITEM2){
View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.filter_list_item, viewGroup, false);
return new FilterCheckBoxViewHolder(v);
}
throw new RuntimeException("there is no type that matches the type " + i + " + make sure your using types correctly");
}
@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int i) {
genreListValues = getGenreListValues();
switch (viewHolder.getItemViewType()){
case TYPE_ITEM1:
((FilterTitleViewHolder) viewHolder).title.setText(genres.get(viewHolder.getItemViewType()));
break;
case TYPE_ITEM2:
for(int j = 0; j < genreListValues.size(); j++)
((FilterCheckBoxViewHolder) viewHolder).cbFilter.setText(genreListValues.get(j).get(i));
}
}
@Override
public int getItemCount() {
return values.getValues().size();
}
public static class FilterCheckBoxViewHolder extends RecyclerView.ViewHolder{
CheckBox cbFilter;
public FilterCheckBoxViewHolder(@NonNull View itemView) {
super(itemView);
cbFilter = itemView.findViewById(R.id.cb_filter);
}
}
public static class FilterTitleViewHolder extends RecyclerView.ViewHolder{
TextView title;
public FilterTitleViewHolder(@NonNull View itemView) {
super(itemView);
title = itemView.findViewById(R.id.tv_title_item);
}
}
private ArrayList<ArrayList<String>> getGenreListValues(){
genreListValues = new ArrayList<>();
ArrayList<String> genreValues = new ArrayList<>();
ArrayList<String> yearsValues = new ArrayList<>();
ArrayList<String> directorsValues = new ArrayList<>();
for(int j = 0; j<values.getValues().size(); j++){
for(int k = 0; k<values.getValues().get(j).getGenre().size(); k++){
if(!genreValues.contains(values.getValues().get(j).getGenre().get(k)))
genreValues.add(values.getValues().get(j).getGenre().get(k));
}
}
for (int j = 0; j<values.getValues().size(); j++){
if(!yearsValues.contains(values.getValues().get(j).getYear()))
yearsValues.add(values.getValues().get(j).getYear());
}
for (int j = 0; j<values.getValues().size(); j++){
String director = values.getValues().get(j).getDirector().trim();
if(!directorsValues.contains(director))
directorsValues.add(values.getValues().get(j).getDirector());
}
Collections.sort(genreValues);
Collections.sort(yearsValues);
Collections.sort(directorsValues);
directorsValues.remove(0);
genreListValues.add(genreValues);
genreListValues.add(yearsValues);
genreListValues.add(directorsValues);
return genreListValues;
}
}
类型来自课堂:
public class FiltersActivity extends AppCompatActivity {
RecyclerView recyclerView;
private List<String> genreList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_filters);
recyclerView = findViewById(R.id.rc_filters);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(linearLayoutManager);
recyclerView.setHasFixedSize(true);
final MoviesViewModel model = ViewModelProviders.of(this).get(MoviesViewModel.class);
genreList = new ArrayList<>();
genreList.add("Genre");
genreList.add("Years");
genreList.add("Directors");
model.getMovies().observe(this, new Observer<Example>() {
@Override
public void onChanged(@Nullable Example example) {
FiltersAdapter adapter = new FiltersAdapter(FiltersActivity.this, example, genreList);
recyclerView.setAdapter(adapter);
}
});
}
}
它总是通过indexofbound崩溃...即使我在不粉碎recyclerview时也执行逻辑,它仅发布一个标题和一个复选框列表(必须为3个标题和3个复选框列表)。
我知道我需要对getItemViewType使用isPositionHeader方法。但是我不知道它如何在我的代码中使用。请帮我。谢谢
对不起,我的英语!