Recycler View nos permite mostrar una lista de elementos, pero para convertir nuestra lista en una lista con viñetas, tenemos que hacer un trabajo adicional. Puede realizar esta tarea siguiendo estos sencillos pasos que se detallan a continuación:
- Agregue la biblioteca de soporte en el archivo build.gradle para Recycler View en la sección de dependencias.
<
dependency
>
implementation 'androidx.recyclerview:recyclerview:1.1.0'
</
dependency
>
- Cree un archivo bullet.xml en la carpeta dibujable .
<?
xml
version
=
"1.0"
encoding
=
"utf-8"
?>
<
shape
android:shape
=
"oval"
>
<
padding
android:left
=
"8dp"
android:right
=
"8dp"
/>
<
size
android:width
=
"6dp"
android:height
=
"6dp"
/>
<
solid
android:color
=
"#219806"
/>
</
shape
>
- En activity_main.xml , agregue el siguiente código.
<?
xml
version
=
"1.0"
encoding
=
"utf-8"
?>
<
androidx.constraintlayout.widget.ConstraintLayout
android:layout_width
=
"match_parent"
android:layout_height
=
"match_parent"
tools:context
=
".MainActivity"
>
<
androidx.recyclerview.widget.RecyclerView
android:layout_width
=
"match_parent"
android:layout_height
=
"match_parent"
android:id
=
"@+id/recycler_view"
/>
</
androidx.constraintlayout.widget.ConstraintLayout
>
- Cree un nuevo archivo custom_layout.xml con el siguiente código.
<?
xml
version
=
"1.0"
encoding
=
"utf-8"
?>
<
LinearLayout
android:layout_width
=
"match_parent"
android:id
=
"@+id/show_name_layout"
android:layout_height
=
"wrap_content"
android:orientation
=
"horizontal"
android:layout_margin
=
"5dp"
>
<
TextView
android:drawableLeft
=
"@drawable/bullet"
android:layout_width
=
"wrap_content"
android:layout_height
=
"wrap_content"
/>
<
TextView
android:layout_marginStart
=
"2dp"
android:layout_width
=
"match_parent"
android:layout_height
=
"wrap_content"
android:id
=
"@+id/textView"
android:textSize
=
"20sp"
/>
</
LinearLayout
>
- Cree una clase MyAdapter.java y agregue el siguiente código.
public
class
MyAdapter
extends
RecyclerView.Adapter<MyAdapter.MyViewHolder> {
String text[];
Activity activity;
public
MyAdapter(Activity activity,
String text[])
{
this
.activity = activity;
this
.text = text;
}
// This method is used to attach
// custom layout to the recycler view
@NonNull
@Override
public
MyViewHolder onCreateViewHolder(
@NonNull
ViewGroup parent,
int
viewType)
{
View view
= activity.getLayoutInflater()
.inflate(
R.layout.custom_layout,
parent,
false
);
return
new
MyViewHolder(view);
}
// This method is used to set the action
// to the widgets of our custom layout.
@Override
public
void
onBindViewHolder(
@NonNull
MyViewHolder holder,
int
position)
{
holder.textView.setText(text[position]);
}
@Override
public
int
getItemCount()
{
return
text.length;
}
class
MyViewHolder
extends
RecyclerView.ViewHolder {
TextView textView;
public
MyViewHolder(
@NonNull
View itemView)
{
super
(itemView);
textView = itemView.findViewById(R.id.textView);
}
}
}
- Finalmente, en MainActivity.java agregue el siguiente código.
public
class
MainActivity
extends
AppCompatActivity {
RecyclerView recyclerView;
String text[]
= {
"Linear Search"
,
"Binary Search"
,
"Selection Sort"
,
"Bubble Sort"
,
"Insertion Sort"
,
"Recursive Insertion Sort"
,
"Merge Sort"
,
"Iterative Merge Sort"
,
"Quick Sort"
,
"Heap Sort"
,
"Counting Sort"
,
"Klee’s Algorithm"
,
"Karatsuba algorithm"
,
"Dijkastra’s Shortest Path Algorithm"
,
"Dial’s Algorithm"
,
"Kruskal’s Minimum Spanning Tree"
,
"Prim’s Minimum Spanning Tree"
,
"Naive Pattern Searching"
,
"KMP Algorithm"
,
"Rabin-Karp Algorithm"
};
@Override
protected
void
onCreate(Bundle savedInstanceState)
{
super
.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recyclerView = findViewById(R.id.recycler_view);
MyAdapter adapter =
new
MyAdapter(
this
, text);
recyclerView.setLayoutManager(
new
LinearLayoutManager(
this
));
recyclerView.setAdapter(adapter);
}
}
Producción:
Publicación traducida automáticamente
Artículo escrito por madhavmaheshwarimm20 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA