Hi Guys, Maybe you all are expert in terms of using RecyclerView in android. This blog is simple example for using filter option with RecyclerView adapter. As for now you will instantiate RecyclerView and set the adapter to RecyclerView as following way. RecyclerView list = (RecyclerView) findViewById(R.id.list); list.setLayoutManager(new LinearLayoutManager(this)); list.setHasFixedSize(true); ArrayList<Number> numbers = new ArrayList<>(); String ONEs[] = {"ZERO", "ONE", "TWO", "THREE", "FOUR", "FIVE", "SIX", "SEVEN", "EIGHT", "NINE", "TEN"}; String TENs[] = {"ZERO", "TEN", "TWENTY", "THIRTY", "FOURTY", "FIFTY", "SIXTY", "SEVENTY", "EIGHTY", "NINETY", "HUNDRED"}; String HUNDREDS[] = {"ZERO", "HUNDRED", "TWO HUND
Good day!!!
As a Android developer we all have experienced
LayoutInflater#inflate
at some point. But we maynot know clearly what this function will do. Come on,
stay attention, Let's explore this in this blog with simple examples!!!
Let's assume you have a layout named fragment_first.xml as following
<?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".FirstFragment"> <TextView android:id="@+id/textView" style="@style/TextAppearance.MaterialComponents.Headline4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@color/colorAccent" android:gravity="center" android:text="@string/app_name" android:textColor="@color/colorPrimary" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> </androidx.constraintlayout.widget.ConstraintLayout>
And you have test_layout.xml as following
<?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent"> <Button android:id="@+id/buttonTest" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Click Me!" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" /> </androidx.constraintlayout.widget.ConstraintLayout>
As you know you can include the test_layout.xml programatically by following
way
val testLayout = LayoutInflater.from(requireContext()).inflate(R.layout.test_layout, view as ConstraintLayout, true)
If you run the app, you will get following output.
As per
LayoutInflater#inflate
recieves following parameters.
But the second and third parameters are bit confusing, we are not clear what should we pass, in each condition what will happen. Let's explore this.
They are saying second parameter is optional, if third parameter is true, the first parameter will be child of second parameter. That's all, the second parameter is depend on third parameter, but its not necessory at all, but you can pass either true/false in third parameter at the same time you can pass rootview/null in second parameter, nothing will happen, app will not get crashed, no exception will be thrown.
- resource
- - int:ID for an XML layout resource to load (e.g., R.layout.main_page)
- root
- - ViewGroup: Optional view to be the parent of the generated hierarchy (if attachToRoot is true), or else simply an object that provides a set of LayoutParams values for root of the returned hierarchy (if attachToRoot is false.) This value may be null
- attachToRoot
- - boolean: Whether the inflated hierarchy should be attached to the root parameter? If false, root is only used to create the correct subclass of LayoutParams for the root view in the XML.
- View
- - The root View of the inflated hierarchy. If root was supplied and attachToRoot is true, this is root; otherwise it is the root of the inflated XML file.
But the second and third parameters are bit confusing, we are not clear what should we pass, in each condition what will happen. Let's explore this.
They are saying second parameter is optional, if third parameter is true, the first parameter will be child of second parameter. That's all, the second parameter is depend on third parameter, but its not necessory at all, but you can pass either true/false in third parameter at the same time you can pass rootview/null in second parameter, nothing will happen, app will not get crashed, no exception will be thrown.
But if you know this clearly, you can code easily on your requirement. If you
want the first paramenter as child of your second parameter, note that the
first parameter will be added based on your second parameter's
LayoutParams
For example, in the above layouts, both are constraint layout, and both has
width and height as match_parent.
Let's set some background color for test_layout, ex:
Ofcourse the TextView is not visible in output, but note that, its accessible, see the toast which appeared tap on TextView.
Let's set some background color for test_layout, ex:
android:background="@android:color/holo_orange_light"
Run the app, you will get following output.
Ofcourse the TextView is not visible in output, but note that, its accessible, see the toast which appeared tap on TextView.
Another example, let's change fragment_first.xml to LinearLayout and set
orientation as vertical.
val testLayout = LayoutInflater.from(requireContext()).inflate(R.layout.test_layout, view as LinearLayout, true)Run the app, you will get following output.
I hope, now you understand, why the second and third parameters required and
what they are doing. If you have any doubts, please share as comment.
Also note that LayoutInflater#inflate returns a View, this is nothing but, if you pass second one as a layout and third parameter as true then its the current root view.
If you pass second one as null or third one as false, it is the root view of currently inflated layout, you can use it in RecyclerView, PopupWindow or some other component which expcets rootview as parameter.
Also note that LayoutInflater#inflate returns a View, this is nothing but, if you pass second one as a layout and third parameter as true then its the current root view.
If you pass second one as null or third one as false, it is the root view of currently inflated layout, you can use it in RecyclerView, PopupWindow or some other component which expcets rootview as parameter.
Thank You, Share your thought as comments.
superb explanation, useful post.
ReplyDeleteeskort eryaman,
ReplyDeleteEryaman eskort bayan,
Eryaman eskort,
Eskort Bayan,
CYOG