我是android的新手,我想在更改LayoutManger时更改用于Recylcer视图的xml文件
例如(我为学习做一个小项目,是一个pokedex)
但是当我将recyclerView的布局更改为网格布局时,我想使用此布局
这是我的适配器:
public class MyDataAdapter extends RecyclerView.Adapter<MyDataAdapter.PokemonViewHolder> {
private List<PokemonViewModel> pokemonViewModelList;
private PokemonActionInterface pokemonActionInterface;
private Boolean viewchoice = true;
public Boolean getViewchoice() {
return viewchoice;
}
public void setViewchoice(Boolean viewchoice) {
this.viewchoice = viewchoice;
}
// Provide a suitable constructor (depends on the kind of dataset)
public MyDataAdapter(List<PokemonViewModel> dataset,PokemonActionInterface pokemonActionInterface) {
pokemonViewModelList = dataset;
this.pokemonActionInterface = pokemonActionInterface;
}
/*public void bindViewModels(List<PokemonViewModel> pokemonViewModelList) {
this.pokemonViewModelList.clear();
this.pokemonViewModelList.addAll(pokemonViewModelList);
notifyDataSetChanged();
}*/
// Create new views (invoked by the layout manager)
@Override
public MyDataAdapter.PokemonViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
// create a new view
View v = LayoutInflater.from(parent.getContext())
.inflate(getView(), parent, false);
PokemonViewHolder pokemonViewHolder = new PokemonViewHolder(v);
return pokemonViewHolder;
}
public int getView(){
if (this.getViewchoice()) {
return R.layout.item_recyclerview_grid;
}
return R.layout.item_recyclerview;
}
// Replace the contents of a view (invoked by the layout manager)
@Override
public void onBindViewHolder(final PokemonViewHolder holder, int position) {
String idString = String.valueOf(pokemonViewModelList.get(position).getId());
holder.idTextView.setText(idString);
holder.nameTextView.setText(pokemonViewModelList.get(position).getName());
holder.iconImageView.setImageResource(R.drawable.rayquaza);
RequestOptions options = new RequestOptions()
.centerCrop()
.placeholder(R.mipmap.ic_launcher_round)
.error(R.mipmap.ic_launcher_round);
holder.pokemonButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
pokemonActionInterface.onPokemonClicked(holder.nameTextView.getText().toString());
}
});
//holder.setupListeners();
}
public List<PokemonViewModel> getPokemonViewModelList() {
return pokemonViewModelList;
}
// Return the size of your dataset (invoked by the layout manager)
@Override
public int getItemCount() {
return pokemonViewModelList.size();
}
public static class PokemonViewHolder extends RecyclerView.ViewHolder {
private TextView nameTextView;
private TextView idTextView;
private ImageView iconImageView;
private ImageButton pokemonButton;
private View v;
//private PokemonActionInterface pokemonActionInterface;
public PokemonViewHolder(View v) {
super(v);
this.v = v;
nameTextView = v.findViewById(R.id.namePokemon_textview);
idTextView = v.findViewById(R.id.idPokemon_textview);
iconImageView = v.findViewById(R.id.icon_imageview);
pokemonButton = v.findViewById(R.id.pokemon_button);
//setupListeners();
}}}
抱歉,如果我不清楚,我是初学者...
你有主意吗?
我用boolean(viewchoice)尝试了一些东西,但这是行不通的...
谢谢