0% found this document useful (0 votes)
23 views23 pages

Lecture 7

Uploaded by

usamashahid7070
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views23 pages

Lecture 7

Uploaded by

usamashahid7070
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 23

Recycler View

What is Recycler View


• RecyclerView is a ViewGroup added to the android studio as a
successor of the GridView and ListView. It is an improvement on both
of them and can be found in the latest v-7 support packages. It has
been created to make possible construction of any lists
with XML layouts as an item which can be customized vastly
while improving on the efficiency of ListViews and GridViews. This
improvement is achieved by recycling the views which are out of the
visibility of the user. For example, if a user scrolled down to a position
where items 4 and 5 are visible; items 1, 2, and 3 would be cleared
from the memory to reduce memory consumption.
Memory Monitor
ListView: Pros & Cons
• Pros
• Easy to implement
• OnItemClickListener
• Cons
• Bad performance in huge List of items
• Complicate way to use ViewHolder pattern (but can use it)
• Vertical list only
Array Adapter in Android with Example
Explanation:
• The Adapter acts as a bridge between the UI Component and the
Data Source. It converts data from the data sources into view items
that can be displayed into the UI Component. Data Source can be
Arrays, HashMap, Database, etc. and UI Components can be ListView,
GridView, Spinner, etc. ArrayAdapter is the most commonly used
adapter in android. When you have a list of single type items which
are stored in an array you can use ArrayAdapter. Likewise, if you have
a list of phone numbers, names, or cities. ArrayAdapter has a layout
with a single TextView. If you want to have a more complex layout
instead of ArrayAdapter use CustomArrayAdapter. The basic syntax
for ArrayAdapter is given as:
Headers of Arrayadapter
• public ArrayAdapter(Context context, int resource, int textViewResourceId, T[]
objects)
• context: It is used to pass the reference of the current class. Here ‘this’ keyword is used to
pass the current class reference. Instead of ‘this’ we could also use
the getApplicationContext() method which is used for the Activity and
the getApplication() method which is used for Fragments.
• public ArrayAdapter(this, int resource, int textViewResourceId, T[] objects)

• Resource: It is used to set the layout file(.xml files) for the list items.
• public ArrayAdapter(this, R.layout.itemListView, int textViewResourceId, T[] objects)

• objects: These are the array object which is used to set the array element into the
TextView.
• ArrayAdapter arrayAdapter = new ArrayAdapter(this, R.layout.itemListView, R.id.itemTextView,
courseList[]);
Example

• Step 1: Create a New Project


• Step 2: Working with the activity_main.xml file
• Step 3: Create a new layout file
• Working with the MainActivity.java file
Step 2: Working with the activity_main.xml file
Step 3: Create a new layout file
Working with the MainActivity.java file
Custom Array Adapter
Methods of Custom Array Adapter
• Now Let us read the methods available in the above code one by one.
getView()
This method helps us to create the each view for the row item of listview or cell of gridview.

super.getView() method can inflate the view for row item.


Using this inflated view, we can get the access to the various UI widgets present in the row item
XML file.
For example, I have inflate the textview using it’s id and also have implemented it’s click
scenario.
Similarly, you can access other UI widgets as per your necessity.

getCount()
This method will return the number of rows present in the listview.
It will give us number of cells in the case of gridview.
We can use data source like arraylist to check the total number of data items and then return this
number.
Methods of Custom Array Adapter
getItem(int position)
• This method will return the data at the specified position.
• For example, a hashmap from arraylist of hashmap at the specified position will be
returned by this method.
getItemId()
• This method will return the specific id associated with the data set.

getItemViewType(int position)
• Listview can have more than two types of row.
• For example, a header row, sub header row, normal child row, footer row etc.
• When compiler is creating the every row of listview, there should be something which tells
it type of the row.

You might also like