Guest User

Untitled

a guest
Apr 21st, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.84 KB | None | 0 0
  1. public class MainActivity extends AppCompatActivity {
  2.  
  3. ArrayList<Mascotas> mascotasList;
  4. private RecyclerView mascotaRecycler;
  5.  
  6. @Override
  7. protected void onCreate(Bundle savedInstanceState) {
  8. super.onCreate(savedInstanceState);
  9. setContentView(R.layout.activity_main);
  10.  
  11. Toolbar actionBar = findViewById(R.id.miactionbar);
  12. setSupportActionBar(actionBar);
  13.  
  14. favoritas = findViewById(R.id.favoritasCinco);
  15. favoritas.setOnClickListener(new View.OnClickListener() {
  16. @Override
  17. public void onClick(View v) {
  18.  
  19.  
  20.  
  21. // Snackbar.make(v,"Vas alas cinco favoritas", Snackbar.LENGTH_SHORT).show();
  22. Intent i = new Intent(MainActivity.this, MascotasFavoritas.class);
  23.  
  24. startActivity(i);
  25. }
  26. });
  27.  
  28. mascotaRecycler = findViewById(R.id.recyclerMascotas);
  29.  
  30. LinearLayoutManager llmanager = new LinearLayoutManager(this);
  31. llmanager.setOrientation(LinearLayoutManager.VERTICAL);
  32. mascotaRecycler.setLayoutManager(llmanager);
  33.  
  34. inicializarLista();
  35. inicializarAdaptador();
  36.  
  37. }
  38.  
  39.  
  40.  
  41. public void inicializarLista(){
  42.  
  43. mascotasList = new ArrayList<Mascotas>();
  44.  
  45. mascotasList.add(new Mascotas(R.drawable.loro,"Coti el loro"));
  46. mascotasList.add(new Mascotas(R.drawable.hamster,"El Ghamster"));
  47. mascotasList.add(new Mascotas(R.drawable.tortuga,"Turtle Ninja"));
  48. mascotasList.add(new Mascotas(R.drawable.perro,"Mochi"));
  49. mascotasList.add(new Mascotas(R.drawable.conejo,"Bonny"));
  50.  
  51.  
  52. }
  53.  
  54. public MascotasAdapter adapter;
  55. public void inicializarAdaptador(){
  56. adapter = new MascotasAdapter(mascotasList);
  57. mascotaRecycler.setAdapter(adapter);
  58. }
  59.  
  60. }
  61.  
  62. public class MascotasAdapter extends RecyclerView.Adapter<MascotasAdapter.MascotasViewHolder>{
  63.  
  64. ArrayList<Mascotas> mascotas;
  65. Activity activity;
  66. TextView contador;
  67.  
  68. public MascotasAdapter(ArrayList<Mascotas> mascotas){
  69. this.mascotas = mascotas;
  70. }
  71.  
  72. public MascotasAdapter(ArrayList<Mascotas> mascotasArrayList, Activity activity, TextView contador){
  73. this.mascotas = mascotasArrayList;
  74. this.activity = activity;
  75. this.contador = contador;
  76. }
  77.  
  78.  
  79. @Override
  80. public MascotasViewHolder onCreateViewHolder( ViewGroup parent, int viewType) {
  81. //Inflo el layout que usaré en el recylcerView
  82. View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.card_mascotas, parent, false);
  83. return new MascotasViewHolder(v);
  84. }
  85.  
  86. @Override
  87. public void onBindViewHolder(final MascotasViewHolder mascotasHolder, int position) {
  88.  
  89. final Mascotas mascotasList = mascotas.get(position);
  90.  
  91. mascotasHolder.imFoto.setImageResource(mascotasList.getIvFoto());
  92. mascotasHolder.textNombre.setText(mascotasList.getNombre());
  93.  
  94. mascotasHolder.ibDarLike.setOnClickListener(new View.OnClickListener() {
  95. @Override
  96. public void onClick(View v) {
  97. Snackbar.make(v,"Diste like a "+ mascotasList.getNombre(),Snackbar.LENGTH_SHORT).show();
  98. int plusLike = Integer.parseInt(mascotasHolder.textCantidadLikes.getText().toString());
  99. mascotasHolder.textCantidadLikes.setText(String.valueOf(plusLike + 1));
  100.  
  101. }
  102. });
  103.  
  104.  
  105. }
  106.  
  107. @Override
  108. public int getItemCount() {
  109. return mascotas.size();
  110. }
  111.  
  112. public static class MascotasViewHolder extends RecyclerView.ViewHolder {
  113.  
  114. private ImageView imFoto;
  115. private TextView textNombre;
  116. private ImageButton ibDarLike;
  117. private TextView textCantidadLikes;
  118. //Context context;
  119.  
  120. public MascotasViewHolder(View itemView) {
  121.  
  122. super(itemView);
  123. //context = itemView.getContext();
  124. imFoto = itemView.findViewById(R.id.ivFoto);
  125. textNombre = itemView.findViewById(R.id.tvNombre);
  126.  
  127. textCantidadLikes = itemView.findViewById(R.id.tvCantidad);
  128. ibDarLike = itemView.findViewById(R.id.ibPuntuar);
  129.  
  130.  
  131. }
  132.  
  133.  
  134.  
  135. }
  136.  
  137.  
  138.  
  139. }
  140.  
  141. public class MascotasFavoritas extends AppCompatActivity {
  142.  
  143. ArrayList<Mascotas> favoritasList;
  144. private RecyclerView favoritasRecycler;
  145.  
  146. @Override
  147. protected void onCreate(Bundle savedInstanceState) {
  148. super.onCreate(savedInstanceState);
  149. setContentView(R.layout.activity_mascotas_favoritas);
  150.  
  151. Toolbar miActionBar = findViewById(R.id.barFavoritas);
  152. setSupportActionBar(miActionBar);
  153. getSupportActionBar().setDisplayHomeAsUpEnabled(true);
  154. getSupportActionBar().setDisplayShowHomeEnabled(true);
  155.  
  156. favoritasRecycler = findViewById(R.id.recyclerFavoritas);
  157.  
  158. LinearLayoutManager llmfav = new LinearLayoutManager(this);
  159. llmfav.setOrientation(LinearLayoutManager.VERTICAL);
  160. favoritasRecycler.setLayoutManager(llmfav);
  161.  
  162. inicializarFavoritas();
  163. inicializarAdaptador();
  164.  
  165.  
  166.  
  167. }
  168.  
  169. void inicializarFavoritas(){
  170. favoritasList = new ArrayList<Mascotas>();
  171.  
  172. favoritasList.add(new Mascotas(R.drawable.perro,"Mochi"));
  173. favoritasList.add(new Mascotas(R.drawable.conejo,"Bonny"));
  174. favoritasList.add(new Mascotas(R.drawable.loro,"Coti el loro"));
  175. favoritasList.add(new Mascotas(R.drawable.hamster,"El Ghamster"));
  176. favoritasList.add(new Mascotas(R.drawable.tortuga,"Turtle Ninja"));
  177.  
  178. }
  179.  
  180. public MascotasAdapter adapter;
  181. public void inicializarAdaptador(){
  182. adapter = new MascotasAdapter(favoritasList);
  183. favoritasRecycler.setAdapter(adapter);
  184. }
  185.  
  186. }
  187.  
  188. <?xml version="1.0" encoding="utf-8"?>
  189. <android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
  190. android:layout_width="match_parent"
  191. android:layout_height="wrap_content"
  192. xmlns:cardView="http://schemas.android.com/apk/res-auto"
  193. android:id="@+id/cvMascotas"
  194. cardView:cardCornerRadius="@dimen/esquinasCardV"
  195. android:layout_marginBottom="@dimen/margenBottom"
  196. android:layout_marginLeft="@dimen/margenBottom"
  197. android:layout_marginRight="@dimen/margenTop"
  198. android:layout_marginTop="@dimen/margenTop"
  199. cardView:cardElevation="@dimen/cardview_default_elevation"
  200. >
  201. <LinearLayout
  202. android:layout_width="match_parent"
  203. android:layout_height="wrap_content"
  204. android:id="@+id/llvertical"
  205. android:orientation="vertical">
  206.  
  207. <ImageView
  208. android:layout_width="match_parent"
  209. android:layout_height="wrap_content"
  210. android:src="@drawable/loro"
  211. android:id="@+id/ivFoto"
  212. android:background="@color/fondoImagen"/>
  213. <LinearLayout
  214. android:layout_width="match_parent"
  215. android:layout_height="wrap_content"
  216. android:id="@+id/llhorizontal"
  217. android:orientation="horizontal"
  218. >
  219.  
  220. <ImageButton
  221. android:layout_width="@dimen/anchoIcono"
  222. android:layout_height="@dimen/anchoIcono"
  223. android:id="@+id/ibPuntuar"
  224. android:src="@drawable/icons8_enamorado_80"
  225. android:background="@color/fondoImagen"
  226. android:scaleType="centerCrop"
  227. android:layout_marginLeft="@dimen/margenDeIcono"/>
  228.  
  229. <TextView
  230. android:layout_width="wrap_content"
  231. android:layout_height="match_parent"
  232. android:id="@+id/tvNombre"
  233. android:text="@string/nombreMascota"
  234. android:textSize="@dimen/tamañoNumero"
  235. android:gravity="center"
  236. android:textStyle="bold"
  237. android:layout_marginRight="@dimen/margenDelMedio"
  238. />
  239. <LinearLayout
  240. android:layout_width="match_parent"
  241. android:layout_height="wrap_content"
  242. android:orientation="horizontal"
  243. android:id="@+id/llinterior"
  244. android:gravity="right">
  245.  
  246. <TextView
  247. android:layout_width="wrap_content"
  248. android:layout_height="match_parent"
  249. android:id="@+id/tvCantidad"
  250. android:text="@string/numeroXdefecto"
  251. android:gravity="center"
  252. android:textSize="@dimen/tamañoNumero"
  253. android:textStyle="bold"
  254. android:layout_marginRight="@dimen/margenDeIcono"
  255. />
  256.  
  257. <ImageView
  258. android:layout_width="@dimen/anchoIcono"
  259. android:layout_height="@dimen/anchoIcono"
  260. android:id="@+id/ivCaritaColor"
  261. android:src="@drawable/icons8_enamorado_96"
  262. android:layout_marginRight="@dimen/margenDeIcono"
  263. />
  264.  
  265. </LinearLayout>
  266.  
  267. </LinearLayout>
  268.  
  269. </LinearLayout>
  270.  
  271. </android.support.v7.widget.CardView>
Add Comment
Please, Sign In to add comment