Crear listas y tarjetas en Android
El widget RecyclerView es una versión más flexible y avanzada de ListView. Este widget es un contenedor para mostrar grandes conjuntos de datos que se pueden desplazar de manera muy eficiente al mantener una cantidad limitada de vistas. Usa el widget RecyclerView cuando tengas conjuntos de datos cuyos elementos cambien en tiempo de ejecución sobre la base de la acción del usuario o los eventos de la red.
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
public class CustomAdapter extends RecyclerView.Adapter<CustomAdapter.ViewHolder> { | |
private String[] data; | |
public static class ViewHolder extends RecyclerView.ViewHolder { | |
// each data item is just a string in this case | |
public TextView title; | |
public ViewHolder(View view) { | |
super(view); | |
title = view.findViewById(R.id.title); | |
} | |
} | |
public CustomAdapter(String[] data) { | |
data = data; | |
} | |
@Override | |
public CustomAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, | |
int viewType) { | |
View view = LayoutInflater.from(parent.getContext()) | |
.inflate(R.layout.item_custom, parent, false); | |
return new ViewHolder(view); | |
} | |
@Override | |
public void onBindViewHolder(ViewHolder holder, int position) { | |
holder.title.setText(data[position]); | |
} | |
@Override | |
public int getItemCount() { | |
return data.length; | |
} | |
} | |
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
dependencies { | |
... | |
compile 'com.android.support:cardview-v7:21.0.+' | |
compile 'com.android.support:recyclerview-v7:21.0.+' | |
... | |
} |
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"?> | |
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
xmlns:tools="http://schemas.android.com/tools" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:orientation="vertical" > | |
<TextView | |
android:id="@+id/title" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" /> | |
</LinearLayout> |
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"?> | |
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
xmlns:tools="http://schemas.android.com/tools" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:orientation="vertical"> | |
<android.support.v7.widget.RecyclerView | |
android:id="@+id/recyclerView" | |
android:scrollbars="vertical" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent"/> | |
</LinearLayout> |
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
public class MainActivity extends Activity { | |
private RecyclerView recyclerView; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.main_activity); | |
String[] titles = { | |
"Paulo", | |
"Priscilla", | |
"Gricelda", | |
}; | |
recyclerView = (RecyclerView) findViewById(R.id.my_recycler_view); | |
recyclerView.setHasFixedSize(true); | |
recyclerView.setLayoutManager(new LinearLayoutManager(this)); | |
mRecyclerView.setAdapter(new CustomAdapter(titles)); | |
} | |
} |