Chap5 RecyclerView
Chap5 RecyclerView
Plan
01 Advanced UI: RecyclerView
XML File
<Spinner
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/marques"
android:entries="@array/jours">
Res File
<string-array name=“jours">
<item>Lundi</item>
<item>Mardi</item>
<item>Mercredi</item>
</string-array>
Fill in Java
Java File
Fill With Array Adapter
DataSource
« Table »
ArrayAdapter View
1) Fill Adapter 2) Bind Adapter
with data to the View
1) Fill adapter with data (table):
Viewname.setAdapter(adaptername);
Example
Java File: ListView
public class MainActivity extends AppCompatActivity { TextView
String[] tab= {"Lundi", "Mardi", "Mercredi"};
TextView
ListView maliste;
@Override TextView
protected void onCreate(Bundle savedInstanceState) { TextView
super.onCreate(savedInstanceState);
TextView
setContentView(R.layout.activity_main);
maliste=findViewById(R.id.listview);
ArrayAdapter a= new
ArrayAdapter(this,android.R.layout.simple_list_item_1, tab);
maliste.setAdapter(a);
ListView & RecyclerView
- a short history -
Back to basics:
ListView is it usefull?
Can we see a Why Using aRV?
complete Example? Benefits?
RecyclerView
In Android
ListView
Data Adapter Item1
source
Item2
Item3
1. Create Custom Layout for one row item
2. Extend the BaseAdapter to create a Custom Adapter Item4
3. Use Custom BaseAdapter to inflate the new layout Item5
4. Bind the Custom BaseAdapter to ListView Item6
ListView:
What’s the problem?
One Methode
=
getView
RecyclerView
RecyclerView
- Definition & advantages-
RecyclerView: Advantages
API 21
Suitable for a list of elements based on large data sets (or
data that frequently changes).
As the user
scrolls the list,
the RV
creates new
views as
necessary.
It also saves
the view which
have scrolled
off-screen, so
they can be
reused.
RecyclerView: the term Recycling
On the other hand, if the
user keeps scrolling in
the same direction, the
view holders which
have been off-screen
the longest can be re-
bound to new data.
The view holder does not
need to be created or
have its view inflated;
instead, the app just
updates the view's
contents to match the new
item it was
bound to.
RecyclerView: the term Recycling
Once an item
scrolls out of the
visible area, it
essentially gets
placed in a
queue.
Sooner or later, it
will be needed
again when a new
item
scrolls in.
Then, the values of
the text in
the item’s UI are
replaced.
RecyclerView Flow
What’s new with the RV?
Your Picture Here
5) Create an Adapter
//Attributes
Name private String Name, Phone;
//Constructor
Phone Number public Contact(String n, String p)
{
this.Name = n;
this.Phone = p;
}
//Getters
public String getName() {
return Name;
}
public String getPhone() {
return Phone;
}
}
XML Representation
3- Create a DataModel & XML representation
What’s on one Row ?
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
TextView …
Id=name >
<TextView
android:id="@+id/name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
TextView android:layout_alignParentTop="true"
android:textColor="@color/title"
Id=phone android:textSize="16dp"
android:textStyle="bold" />
my_row.xml
<TextView
android:id="@+id/phone"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/name" />
</RelativeLayout>
The Layout Manager
4- Connect the RV object to a layout manager
Serves to Measuring and positioning item views within a RecyclerView in the chosen
way
Vertical
Grid
Arrangment
Staggered
Horizontal
LinearLayoutManager
4- Connect the RV object to a layout manager
RecyclerView rv;
rv =findViewById(R.id.recycler_view);
LinearLayoutManager LM = new LinearLayoutManager(this,
LinearLayoutManager.HORIZONTAL, false);
rv.setLayoutManager(LM);
VERTICAL
LinearLayoutManager
Vertical & Horizontal
VERTICAL HORIZONTAL
Click to Click to
see the see the
video video
GridLayoutManager
4- Connect the RV object to a layout manager
GridLayoutManager (Context context, int spanCount)
//By default, each item occupies 1 span
GridLayoutManager layoutManager=
new GridLayoutManager(this,2);
recyclerView.setLayoutManager(layoutManager);
GridLayoutManager
4- Connect the RV object to a layout manager
GridLayoutManager (Context context, int spanCount)
//By default, each item occupies 1 span
GridLayoutManager layoutManager=
new GridLayoutManager(this,3);
recyclerView.setLayoutManager(layoutManager);
StaggeredGridLayoutManager
4- Connect the RV object to a layout manager
StaggeredGridLayoutManager staggeredGridLayoutManager =
new StaggeredGridLayoutManager(3, LinearLayoutManager.VERTICAL);
recyclerView.setLayoutManager(staggeredGridLayoutManager);
StaggeredGridLayoutManager
HORIZONTAL VERTICAL
me
l
p
Exa
Adapter & View Holder
5-Create an Adapter
ViewHolder
//Adapter Implementation
}
public class Adapter extends RecyclerView.Adapter<Adapter.MyViewHolder> {
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
}
}
@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
}
@Override
public int getItemCount() {
}
}
public class Adapter extends RecyclerView.Adapter<Adapter.MyViewHolder> {
public Adapter(List<Contact> x) {
this.myList = x;
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
}
public class MyViewHolder extends RecyclerView.ViewHolder {
public MyViewHolder(View view) {
}
}
@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
}
@Override
public int getItemCount() {
}
}
public class Adapter extends RecyclerView.Adapter<Adapter.MyViewHolder> {
@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
}
@Override
public int getItemCount() {
}
}
public class Adapter extends RecyclerView.Adapter<Adapter.MyViewHolder> {
}
@Override
public int getItemCount() {
}
}
public class Adapter extends RecyclerView.Adapter<Adapter.MyViewHolder> {
-A D. Williams-