Last active
October 15, 2016 22:21
-
-
Save alvareztech/85ecf2a28424f6376c583466c5446ff7 to your computer and use it in GitHub Desktop.
Ejemplo RecyclerView de Personas
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
xmlns:tools="http://schemas.android.com/tools" | |
android:id="@+id/activity_main" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
android:paddingBottom="@dimen/activity_vertical_margin" | |
android:paddingLeft="@dimen/activity_horizontal_margin" | |
android:paddingRight="@dimen/activity_horizontal_margin" | |
android:paddingTop="@dimen/activity_vertical_margin" | |
tools:context="tech.alvarez.listapersonas.activities.MainActivity"> | |
<android.support.v7.widget.RecyclerView | |
android:id="@+id/personasRecyclerView" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" /> | |
</RelativeLayout> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
apply plugin: 'com.android.application' | |
android { | |
compileSdkVersion 24 | |
buildToolsVersion "24.0.2" | |
defaultConfig { | |
applicationId "tech.alvarez.listapersonas" | |
minSdkVersion 15 | |
targetSdkVersion 24 | |
versionCode 1 | |
versionName "1.0" | |
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" | |
} | |
buildTypes { | |
release { | |
minifyEnabled false | |
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' | |
} | |
} | |
} | |
dependencies { | |
compile fileTree(dir: 'libs', include: ['*.jar']) | |
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', { | |
exclude group: 'com.android.support', module: 'support-annotations' | |
}) | |
compile 'com.android.support:appcompat-v7:24.2.1' | |
testCompile 'junit:junit:4.12' | |
compile 'com.android.support:recyclerview-v7:24.2.1' | |
compile 'com.android.support:cardview-v7:24.2.1' | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android" | |
xmlns:card_view="http://schemas.android.com/apk/res-auto" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:layout_margin="8dp" | |
card_view:cardCornerRadius="4dp"> | |
<LinearLayout | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:orientation="vertical" | |
android:padding="8dp"> | |
<TextView | |
android:id="@+id/nombresTextView" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:text="Maria Perez" | |
android:textSize="20sp" /> | |
<TextView | |
android:id="@+id/ciTextView" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:text="6120073" | |
android:textSize="16sp" /> | |
<TextView | |
android:id="@+id/sexoTextView" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:text="Femenino" | |
android:textColor="@color/colorAccent" | |
android:textSize="12sp" /> | |
</LinearLayout> | |
</android.support.v7.widget.CardView> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package tech.alvarez.listapersonas.activities; | |
import android.os.Bundle; | |
import android.support.v7.app.AppCompatActivity; | |
import android.support.v7.widget.LinearLayoutManager; | |
import android.support.v7.widget.RecyclerView; | |
import android.widget.Toast; | |
import tech.alvarez.listapersonas.R; | |
import tech.alvarez.listapersonas.adapters.OnItemClickListener; | |
import tech.alvarez.listapersonas.adapters.PersonasAdapter; | |
import tech.alvarez.listapersonas.models.Persona; | |
public class MainActivity extends AppCompatActivity implements OnItemClickListener{ | |
private RecyclerView personasRecyclerView; | |
private PersonasAdapter personasAdapter; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
personasRecyclerView = (RecyclerView) findViewById(R.id.personasRecyclerView); | |
personasRecyclerView.setHasFixedSize(true); | |
personasRecyclerView.setLayoutManager(new LinearLayoutManager(this)); | |
personasAdapter = new PersonasAdapter(this); | |
personasRecyclerView.setAdapter(personasAdapter); | |
// adicionar datos | |
personasAdapter.add(new Persona("Maria", "Perez", 1762354, "F")); | |
personasAdapter.add(new Persona("Juan", "Rivera", 542345, "M")); | |
personasAdapter.add(new Persona("Ricardo", "Gutierrez", 1937491, "M")); | |
} | |
@Override | |
public void onItemClick(Persona p) { | |
Toast.makeText(this, p.getNombres() + " " + p.getCi(), Toast.LENGTH_LONG).show(); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package tech.alvarez.listapersonas.adapters; | |
import tech.alvarez.listapersonas.models.Persona; | |
public interface OnItemClickListener { | |
void onItemClick(Persona p); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package tech.alvarez.listapersonas.models; | |
public class Persona { | |
private String nombres; | |
private String apellidos; | |
private int ci; | |
private String sexo; | |
public Persona(String nombres, String apellidos, int ci, String sexo) { | |
this.nombres = nombres; | |
this.apellidos = apellidos; | |
this.ci = ci; | |
this.sexo = sexo; | |
} | |
public String getNombres() { | |
return nombres; | |
} | |
public void setNombres(String nombres) { | |
this.nombres = nombres; | |
} | |
public String getApellidos() { | |
return apellidos; | |
} | |
public void setApellidos(String apellidos) { | |
this.apellidos = apellidos; | |
} | |
public int getCi() { | |
return ci; | |
} | |
public void setCi(int ci) { | |
this.ci = ci; | |
} | |
public String getSexo() { | |
return sexo; | |
} | |
public void setSexo(String sexo) { | |
this.sexo = sexo; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package tech.alvarez.listapersonas.adapters; | |
import android.support.v7.widget.RecyclerView; | |
import android.view.LayoutInflater; | |
import android.view.View; | |
import android.view.ViewGroup; | |
import android.widget.TextView; | |
import java.util.ArrayList; | |
import tech.alvarez.listapersonas.R; | |
import tech.alvarez.listapersonas.models.Persona; | |
public class PersonasAdapter extends RecyclerView.Adapter<PersonasAdapter.ViewHolder> { | |
private ArrayList<Persona> dataset; | |
private OnItemClickListener onItemClickListener; | |
public PersonasAdapter(OnItemClickListener onItemClickListener) { | |
this.dataset = new ArrayList<Persona>(); | |
this.onItemClickListener = onItemClickListener; | |
} | |
@Override | |
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { | |
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_persona, parent, false); | |
return new ViewHolder(view); | |
} | |
@Override | |
public void onBindViewHolder(ViewHolder holder, int position) { | |
Persona p = dataset.get(position); | |
holder.nombresTextView.setText(p.getNombres() + " " + p.getApellidos()); | |
holder.ciTextView.setText(p.getCi() + ""); | |
if (p.getSexo().equals("M")) { | |
holder.sexoTextView.setText("Masculino"); | |
} else { | |
holder.sexoTextView.setText("Femenino"); | |
} | |
holder.setOnItemClickListener(p, onItemClickListener); | |
} | |
@Override | |
public int getItemCount() { | |
return dataset.size(); | |
} | |
public static class ViewHolder extends RecyclerView.ViewHolder { | |
TextView nombresTextView; | |
TextView ciTextView; | |
TextView sexoTextView; | |
public ViewHolder(View itemView) { | |
super(itemView); | |
nombresTextView = (TextView) itemView.findViewById(R.id.nombresTextView); | |
ciTextView = (TextView) itemView.findViewById(R.id.ciTextView); | |
sexoTextView = (TextView) itemView.findViewById(R.id.sexoTextView); | |
} | |
public void setOnItemClickListener(final Persona p, final OnItemClickListener onItemClickListener) { | |
itemView.setOnClickListener(new View.OnClickListener() { | |
@Override | |
public void onClick(View v) { | |
onItemClickListener.onItemClick(p); | |
} | |
}); | |
} | |
} | |
public void add(Persona p) { | |
dataset.add(p); | |
notifyDataSetChanged(); | |
} | |
public void clear() { | |
dataset.clear(); | |
notifyDataSetChanged(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment