Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class MainActivity extends AppCompatActivity {
- ArrayList<Mascotas> mascotasList;
- private RecyclerView mascotaRecycler;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- Toolbar actionBar = findViewById(R.id.miactionbar);
- setSupportActionBar(actionBar);
- favoritas = findViewById(R.id.favoritasCinco);
- favoritas.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- // Snackbar.make(v,"Vas alas cinco favoritas", Snackbar.LENGTH_SHORT).show();
- Intent i = new Intent(MainActivity.this, MascotasFavoritas.class);
- startActivity(i);
- }
- });
- mascotaRecycler = findViewById(R.id.recyclerMascotas);
- LinearLayoutManager llmanager = new LinearLayoutManager(this);
- llmanager.setOrientation(LinearLayoutManager.VERTICAL);
- mascotaRecycler.setLayoutManager(llmanager);
- inicializarLista();
- inicializarAdaptador();
- }
- public void inicializarLista(){
- mascotasList = new ArrayList<Mascotas>();
- mascotasList.add(new Mascotas(R.drawable.loro,"Coti el loro"));
- mascotasList.add(new Mascotas(R.drawable.hamster,"El Ghamster"));
- mascotasList.add(new Mascotas(R.drawable.tortuga,"Turtle Ninja"));
- mascotasList.add(new Mascotas(R.drawable.perro,"Mochi"));
- mascotasList.add(new Mascotas(R.drawable.conejo,"Bonny"));
- }
- public MascotasAdapter adapter;
- public void inicializarAdaptador(){
- adapter = new MascotasAdapter(mascotasList);
- mascotaRecycler.setAdapter(adapter);
- }
- }
- public class MascotasAdapter extends RecyclerView.Adapter<MascotasAdapter.MascotasViewHolder>{
- ArrayList<Mascotas> mascotas;
- Activity activity;
- TextView contador;
- public MascotasAdapter(ArrayList<Mascotas> mascotas){
- this.mascotas = mascotas;
- }
- public MascotasAdapter(ArrayList<Mascotas> mascotasArrayList, Activity activity, TextView contador){
- this.mascotas = mascotasArrayList;
- this.activity = activity;
- this.contador = contador;
- }
- @Override
- public MascotasViewHolder onCreateViewHolder( ViewGroup parent, int viewType) {
- //Inflo el layout que usaré en el recylcerView
- View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.card_mascotas, parent, false);
- return new MascotasViewHolder(v);
- }
- @Override
- public void onBindViewHolder(final MascotasViewHolder mascotasHolder, int position) {
- final Mascotas mascotasList = mascotas.get(position);
- mascotasHolder.imFoto.setImageResource(mascotasList.getIvFoto());
- mascotasHolder.textNombre.setText(mascotasList.getNombre());
- mascotasHolder.ibDarLike.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- Snackbar.make(v,"Diste like a "+ mascotasList.getNombre(),Snackbar.LENGTH_SHORT).show();
- int plusLike = Integer.parseInt(mascotasHolder.textCantidadLikes.getText().toString());
- mascotasHolder.textCantidadLikes.setText(String.valueOf(plusLike + 1));
- }
- });
- }
- @Override
- public int getItemCount() {
- return mascotas.size();
- }
- public static class MascotasViewHolder extends RecyclerView.ViewHolder {
- private ImageView imFoto;
- private TextView textNombre;
- private ImageButton ibDarLike;
- private TextView textCantidadLikes;
- //Context context;
- public MascotasViewHolder(View itemView) {
- super(itemView);
- //context = itemView.getContext();
- imFoto = itemView.findViewById(R.id.ivFoto);
- textNombre = itemView.findViewById(R.id.tvNombre);
- textCantidadLikes = itemView.findViewById(R.id.tvCantidad);
- ibDarLike = itemView.findViewById(R.id.ibPuntuar);
- }
- }
- }
- public class MascotasFavoritas extends AppCompatActivity {
- ArrayList<Mascotas> favoritasList;
- private RecyclerView favoritasRecycler;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_mascotas_favoritas);
- Toolbar miActionBar = findViewById(R.id.barFavoritas);
- setSupportActionBar(miActionBar);
- getSupportActionBar().setDisplayHomeAsUpEnabled(true);
- getSupportActionBar().setDisplayShowHomeEnabled(true);
- favoritasRecycler = findViewById(R.id.recyclerFavoritas);
- LinearLayoutManager llmfav = new LinearLayoutManager(this);
- llmfav.setOrientation(LinearLayoutManager.VERTICAL);
- favoritasRecycler.setLayoutManager(llmfav);
- inicializarFavoritas();
- inicializarAdaptador();
- }
- void inicializarFavoritas(){
- favoritasList = new ArrayList<Mascotas>();
- favoritasList.add(new Mascotas(R.drawable.perro,"Mochi"));
- favoritasList.add(new Mascotas(R.drawable.conejo,"Bonny"));
- favoritasList.add(new Mascotas(R.drawable.loro,"Coti el loro"));
- favoritasList.add(new Mascotas(R.drawable.hamster,"El Ghamster"));
- favoritasList.add(new Mascotas(R.drawable.tortuga,"Turtle Ninja"));
- }
- public MascotasAdapter adapter;
- public void inicializarAdaptador(){
- adapter = new MascotasAdapter(favoritasList);
- favoritasRecycler.setAdapter(adapter);
- }
- }
- <?xml version="1.0" encoding="utf-8"?>
- <android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- xmlns:cardView="http://schemas.android.com/apk/res-auto"
- android:id="@+id/cvMascotas"
- cardView:cardCornerRadius="@dimen/esquinasCardV"
- android:layout_marginBottom="@dimen/margenBottom"
- android:layout_marginLeft="@dimen/margenBottom"
- android:layout_marginRight="@dimen/margenTop"
- android:layout_marginTop="@dimen/margenTop"
- cardView:cardElevation="@dimen/cardview_default_elevation"
- >
- <LinearLayout
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:id="@+id/llvertical"
- android:orientation="vertical">
- <ImageView
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:src="@drawable/loro"
- android:id="@+id/ivFoto"
- android:background="@color/fondoImagen"/>
- <LinearLayout
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:id="@+id/llhorizontal"
- android:orientation="horizontal"
- >
- <ImageButton
- android:layout_width="@dimen/anchoIcono"
- android:layout_height="@dimen/anchoIcono"
- android:id="@+id/ibPuntuar"
- android:src="@drawable/icons8_enamorado_80"
- android:background="@color/fondoImagen"
- android:scaleType="centerCrop"
- android:layout_marginLeft="@dimen/margenDeIcono"/>
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="match_parent"
- android:id="@+id/tvNombre"
- android:text="@string/nombreMascota"
- android:textSize="@dimen/tamañoNumero"
- android:gravity="center"
- android:textStyle="bold"
- android:layout_marginRight="@dimen/margenDelMedio"
- />
- <LinearLayout
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:orientation="horizontal"
- android:id="@+id/llinterior"
- android:gravity="right">
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="match_parent"
- android:id="@+id/tvCantidad"
- android:text="@string/numeroXdefecto"
- android:gravity="center"
- android:textSize="@dimen/tamañoNumero"
- android:textStyle="bold"
- android:layout_marginRight="@dimen/margenDeIcono"
- />
- <ImageView
- android:layout_width="@dimen/anchoIcono"
- android:layout_height="@dimen/anchoIcono"
- android:id="@+id/ivCaritaColor"
- android:src="@drawable/icons8_enamorado_96"
- android:layout_marginRight="@dimen/margenDeIcono"
- />
- </LinearLayout>
- </LinearLayout>
- </LinearLayout>
- </android.support.v7.widget.CardView>
Add Comment
Please, Sign In to add comment