As the question says, I have my RecyclerView
filled with 50 elements approximately which by default are shown but what I'm looking for is to be able to give a specific number of elements that I want to see, I know that the getItemCount()
controls said number of items what is not is how to make it dynamic with a simple EditText
My adapter is this
public class Adaptador extends RecyclerView.Adapter<Adaptador.CartaViewHolder> {
private Context contexto;
private List<Contenido> listacartas;
public Adaptador(Context contexto, List<Contenido> listacartas) {
this.contexto = contexto;
this.listacartas = listacartas;
}
@Override
public CartaViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
LayoutInflater inflado = LayoutInflater.from(contexto);
View vistosa = inflado.inflate(R.layout.cartas, null);
return new CartaViewHolder(vistosa);
}
@Override
public void onBindViewHolder(CartaViewHolder holder, int position) {
Contenido datos = listacartas.get(position);
holder.nombre.setText(datos.getNombre());
holder.imagen.setImageDrawable(contexto.getResources().getDrawable(datos.getImagen()));
}
@Override
public int getItemCount() {
return listacartas.size();
}
class CartaViewHolder extends RecyclerView.ViewHolder {
ImageView imagen;
TextView nombre;
public CartaViewHolder(View itemView) {
super(itemView);
imagen = itemView.findViewById(R.id.imageView);
nombre = itemView.findViewById(R.id.textView);
}
}
My mainActivity where I have the edittext
to give the exact number of items to recycle
public class MainActivity extends AppCompatActivity {
Button click;
BlankFragment blankFragment;
EditText numero;
RecyclerView reciclador;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
click = findViewById(R.id.fragmento);
numero = findViewById(R.id.numero);
blankFragment = new BlankFragment();
findViewById(R.id.fragmento).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
getSupportFragmentManager().beginTransaction().add(R.id.contenedor, blankFragment).addToBackStack(null).commit();
click.setVisibility(View.INVISIBLE);
numero.setVisibility(View.INVISIBLE);
}
});
}
@Override
public void onBackPressed() {
super.onBackPressed();
click.setVisibility(View.VISIBLE);
numero.setVisibility(View.VISIBLE);
}
And the fragment where I fill the list that is shown in the recicleView
which I need to retrace the number of items
that is displayed
public class BlankFragment extends Fragment {
RecyclerView reciclador;
List<Contenido> listota;
LinearLayoutManager layoutManager
= new LinearLayoutManager(getContext(), LinearLayoutManager.HORIZONTAL, false);
public BlankFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_blank, container, false);
listota = new ArrayList<>();
reciclador = view.findViewById(R.id.Lista);
reciclador.setLayoutManager(layoutManager);
reciclador.setHasFixedSize(true);
reciclador.setLayoutManager(new LinearLayoutManager(getContext()));
listota.add(new Contenido(R.drawable.alacran, "Alacran"));
listota.add(new Contenido(R.drawable.apache, "Apache"));
listota.add(new Contenido(R.drawable.arana, "Araña"));
listota.add(new Contenido(R.drawable.arbol, "Arbol"));
listota.add(new Contenido(R.drawable.arpa, "Arpa"));
listota.add(new Contenido(R.drawable.bandera, "Bandera"));
listota.add(new Contenido(R.drawable.barril, "Barril"));
listota.add(new Contenido(R.drawable.borracho, "Borracho"));
listota.add(new Contenido(R.drawable.bota, "Bota"));
listota.add(new Contenido(R.drawable.botella, "Botella"));
listota.add(new Contenido(R.drawable.calavera, "Calavera"));
listota.add(new Contenido(R.drawable.camaron, "Camaron"));
listota.add(new Contenido(R.drawable.campana, "Campana"));
listota.add(new Contenido(R.drawable.cantaro, "Catrin"));
listota.add(new Contenido(R.drawable.cazo, "Cazo"));
listota.add(new Contenido(R.drawable.chalupa, "Chalupa"));
listota.add(new Contenido(R.drawable.corazon, "Corazon"));
listota.add(new Contenido(R.drawable.corona, "Corona"));
listota.add(new Contenido(R.drawable.cotorro, "Cotorro"));
listota.add(new Contenido(R.drawable.dama, "Dama"));
listota.add(new Contenido(R.drawable.diablito, "Diablito"));
listota.add(new Contenido(R.drawable.escalera, "Escalera"));
listota.add(new Contenido(R.drawable.estrella, "Estrella"));
listota.add(new Contenido(R.drawable.gallo, "Gallo"));
listota.add(new Contenido(R.drawable.garza, "Garza"));
listota.add(new Contenido(R.drawable.gorrito, "Gorrito"));
listota.add(new Contenido(R.drawable.jaras, "Jarras"));
listota.add(new Contenido(R.drawable.luna, "Luna"));
listota.add(new Contenido(R.drawable.maceta, "Maceta"));
listota.add(new Contenido(R.drawable.mano, "Mano"));
listota.add(new Contenido(R.drawable.melon, "Melon"));
listota.add(new Contenido(R.drawable.muerte, "Muerte"));
listota.add(new Contenido(R.drawable.mundo, "Mundo"));
listota.add(new Contenido(R.drawable.musico, "Musico"));
listota.add(new Contenido(R.drawable.negro, "Negro"));
listota.add(new Contenido(R.drawable.nopal, "Nopal"));
listota.add(new Contenido(R.drawable.pajaro, "Pajaro"));
listota.add(new Contenido(R.drawable.palma, "Palma"));
listota.add(new Contenido(R.drawable.paragauas, "Paraguas"));
listota.add(new Contenido(R.drawable.pera, "Pera"));
listota.add(new Contenido(R.drawable.pescado, "Pescado"));
listota.add(new Contenido(R.drawable.pino, "Pino"));
listota.add(new Contenido(R.drawable.rana, "Rana"));
listota.add(new Contenido(R.drawable.rosa, "Rosa"));
listota.add(new Contenido(R.drawable.sandolon, "Sandolon"));
listota.add(new Contenido(R.drawable.sandria, "Sandia"));
listota.add(new Contenido(R.drawable.sirena, "Sirena"));
listota.add(new Contenido(R.drawable.sol, "Sol"));
listota.add(new Contenido(R.drawable.soldado, "Soldado"));
listota.add(new Contenido(R.drawable.tambor, "Tambor"));
listota.add(new Contenido(R.drawable.valiente, "Valiente"));
listota.add(new Contenido(R.drawable.venado, "Venado"));
listota.add(new Contenido(R.drawable.violoncelo, "Violonchelo"));
Adaptador adapter = new Adaptador(getContext(), listota);
reciclador.setAdapter(adapter);
return view;
}