Inflate là gì
Mình chỉ tất cả thói quen thuộc là video clip để chia sẻ kiến thức thiết kế như bỗng dưng lại hy vọng viết nhằm trải nghiệm yêu cầu mình đã thử viết 1 loạt bài để chia sẻ hiểu biết của chính mình về lập trình Android. Hy vọng chúng ta ủng hộ với trong bài viết này mình thích chia sẻ với các bạn về LayoutInflater.
Bạn đang xem: Inflate là gì
Định nghĩa:
LayoutInflater là một trong những component giúp bạn chuyển layout file(Xml) thành View(Java code) trong Android. Các bạn thường thực hiện nó trong cách làm onCreateView của fragment hoặc phương thức getView khi custom adapter.Cách tạo đối tượng người tiêu dùng LayoutInflater
Chúng ta tất cả 2 phương pháp để tạo ra đối tượng người tiêu dùng LayoutInflater:
1.LayoutInflater là một trong những System Service của android và cách áp dụng của nó giống như các System Service không giống như khi bạn sử dụng WINDOW_SERVICE, ALARM_SERVICE giỏi LOCATION_SERVICE.
LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);Đây là cách được khuyên dùng nhưng nó hơi dài mẫu và tôi siêu ít khi thực hiện cách này.2. áp dụng static method của LayoutInflater:
LayoutInflater layoutInflater = LayoutInflater.from(context);Đây là bí quyết tôi hay sử dụng nhất bởi vì nó gọn nhẹ ^_^
Phương thức Inflate
Công câu hỏi của LayoutInflater là phát âm xml layout file và thay đổi các trực thuộc tính của chính nó thành 1 View vào Java code. Sau thời điểm có đối tượng người dùng LayoutInflater, ta có thể dùng cách tiến hành inflate để biến đổi 1 xml layout tệp tin thành 1 View vào java. Ta gồm 2 cách thức inflate với số lượng tham số khác nhau:
1. View view = layoutInflater.inflate(int resource, ViewGroup parent)2. View view = layoutInflater.inflate(int resource, ViewGroup parent, boolean attachToRoot)Các các bạn sẽ thắc mắc các tham số của inflater có ý nghĩa gì? 2 cách thức inflate trên chỉ khác biệt tham số attachToRoot vậy attachToRoot là gì? Cùng mày mò thông sang một số ví dụ như nhé.Trước tiên họ tìm đọc 3 tham số của nó là gì sẽ nhé:Như có mang thì nhiệm vụ của LayoutInflater là chuyển đổi xml layout tệp tin thành đối tượng người sử dụng View vào java code, vậy thì:
Tham số thứ nhất là: int resource, nó đó là xml layout tệp tin mà chúng ta muốn thay đổi thành View.Tham số thiết bị hai là: ViewGroup parent, nó là ViewGroup khu vực mà xml layout file(tham số sản phẩm công nghệ nhất) hoàn toàn có thể được nhúng vào, LayoutInflater sẽ thay đổi xml layout file thành View cùng sử dụng các thuộc tính cân xứng với ViewGroup parrent.Tham số thứ cha là: attachToRoot, khi mà lại attachToRoot=true thì ngay sau khi quá trình biến đổi xml file(resource) thành View chấm dứt thì nó đã nhúng View đó vào ViewGroup parent (RIGHT NOW) , lúc attachToRoot = false thì nó chỉ biến hóa xml file(resource) thành View vào java cơ mà không thêm ngay vào ViewGroup(NOT NOW)Rồi cùng đi vào ví dụ cho dễ nắm bắt nào..Tôi tất cả xml layout file mang tên là activity_main.xml cùng với root là LinearLayout phía vertical:
LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:id="
+id/ll_main" android:layout_width="fill_parent" android:layout_height="fill_parent">LinearLayout>Và 1 xml layout file khác tên là item_button.xml như sau:
Button xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Custom Button" android:id="
+id/custom_button">Button>và bây giờ tôi sẽ thực hiện lần lượt các phương thức inflate cùng tôi sẽ chỉ ra tác dụng sau khi chúng ta sử dụng nó
// TH1: họ chỉ sử dụng 2 tham số tuy vậy attachToRoot sẽ được đặt khoác định bởi true và hiệu quả là item_button sẽ được chuyển đổi thành View cùng được add vào llMain tức thì khi biến hóa xong.
public class DemoLayoutInflater extends AppCompatActivity
Nullable Bundle savedInstanceState) super.onCreate(savedInstanceState); setContentView(R.layout.activity_demo); LinearLayout llMain = findViewById(R.id.ll_main); View view = LayoutInflater.from(this).inflate(R.layout.item_button, llMain);//TH2: họ chỉ sử dụng 3 tham cùng với attachToRoot = true, khi ấy item_button sẽ tiến hành chuyển thành View với được địa chỉ vào llMain ngay lập tức khi biến đổi hoàn tất tương đương TH1.
Xem thêm: Hdd Và Ssd Là Gì - Ổ Cứng Hdd Và Ssd Cái Nào Tốt Hơn
public class DemoLayoutInflater extends AppCompatActivity
Nullable Bundle savedInstanceState) super.onCreate(savedInstanceState); setContentView(R.layout.activity_demo); LinearLayout llMain = findViewById(R.id.ll_main); View view = LayoutInflater.from(this).inflate(R.layout.item_button, llMain, true);TH1 với TH2 bao gồm cùng hiệu quả như hình bên dưới đây:

public class DemoLayoutInflater extends AppCompatActivity
Nullable Bundle savedInstanceState) super.onCreate(savedInstanceState); setContentView(R.layout.activity_demo); LinearLayout llMain = findViewById(R.id.ll_main); View view = LayoutInflater.from(this).inflate(R.layout.item_button, llMain, false);Kết trái của TH3 là button ko được showroom vào LinearLayout như hình bên dưới.

public class DemoLayoutInflater extends AppCompatActivity
Nullable Bundle savedInstanceState) super.onCreate(savedInstanceState); setContentView(R.layout.activity_demo); LinearLayout llMain = findViewById(R.id.ll_main); View view = LayoutInflater.from(this).inflate(R.layout.item_button, llMain, false); llMain.addView(view); //Different
Lưu ý khi thực hiện LayoutInflater trong custom adapter
Qua 3 lấy ví dụ trên vững chắc bạn cũng đã hiểu tham số attachToRoot dùng để triển khai gì rồi phải không? tóm lại thì attachToRoot đưa ra quyết định View mà lại được tạo ra bởi qua trình inflate của LayoutInflater có được showroom vào ViewGroup parent tuyệt không.Nhưng chúng ta thử so với trường hợp cần sử dụng LayoutInflater trong cách thức getView khi custom adapter xem nhé.Ở trong cách tiến hành getView họ hay thực hiện như sau:
Overridepublic View getView(int position, View convertView, ViewGroup parent) View view = LayoutInflater.from(mContext).inflate(R.layout.item_message, parent, false); /* *chỗ nàyánh xạ view và cập nhật dữ liệu của view */ return view;Ở cách thức trên thì LayoutInflater đang đọc tệp tin item_message.xml và chuyển đổi nó thành 1 view và sẽ không còn attach tức thì vào ViewGroup parent(ex:ListView, GridView...)Nếu bạn sử dụng đoạn code trên và mặt Activity chúng ta set adapter thì rất nhiều chuyện phần đa ổn..ứng dụng chúng ta chạy thông thường và listview vẫn hiển thị các tin nhắn.
Nhưng bạn nhận thấy là listview mong mỏi hiển thị các item message, vậy lý do khi tạo ra view từ item_message.xml họ không gán luôn nó vào listview bằng phương pháp cho tham số attachToRoot=true sinh sống trong thủ tục getView luôn nhỉ...Thử code dưới nhé
Override public View getView(int position, View convertView, ViewGroup parent) View view = LayoutInflater.from(mContext).inflate(R.layout.item_message, parent, true); /* *chỗ nàyánh xạ view và cập nhật dữ liệu của view */ return view; Nếu bạn áp dụng code này thì áp dụng của các bạn sẽ bị dừng bất thần với nguyên nhân:
01-17 01:57:21.961 14112-14112/com.dvt.abc E/AndroidRuntime: FATAL EXCEPTION: main Process: com.dvt.abc, PID: 14112 android.view.InflateException: Binary XML file line #20: addView(View, LayoutParams) is not supported in AdapterView Caused by: java.lang.UnsupportedOperationException: addView(View, LayoutParams) is not supported in AdapterView at android.widget.AdapterView.addView(AdapterView.java:880) at android.view.LayoutInflater.inflate(LayoutInflater.java:534) at android.view.LayoutInflater.inflate(LayoutInflater.java:427) at com.dvt.abc.MessageAdapter.getView(MessageAdapter.java:38)Tại sao lại như vậy...Khi bạn sử dụng đoạn code:
public class DemoLayoutInflater extends AppCompatActivity
Nullable Bundle savedInstanceState) super.onCreate(savedInstanceState); setContentView(R.layout.activity_demo); LinearLayout llMain = findViewById(R.id.ll_main); View view = LayoutInflater.from(this).inflate(R.layout.item_button, llMain, true);Ở đây LayoutInflater đã đọc tệp tin item_button.xml thành View (Java code) và add nó làm nhỏ của ViewGroup llMain. Kết quả sẽ tương tự như với xml layout file này:
LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:id="
+id/ll_main" android:layout_width="fill_parent" android:layout_height="fill_parent"> Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Custom Button" android:id="
+id/custom_button"> Button> LinearLayout>Hành động add view đối với các ViewGroup như LinearLayout xuất xắc RelativeLayout thì hoàn toàn bình thường, vì những viewgroup gồm thể chứa đựng nhiều View con hoặc ViewGroup không giống ở trong nó. Tuy vậy một lớp bé của AdapterView như ListView, GridView thì những item bé của nó ko thể bằng tay bằng việc địa chỉ cửa hàng trong xml hoặc trong code như dưới đây:
//xml layout fileListView android:id="
+id/lv_data" android:layout_width="match_parent" android:layout_height="wrap_content" > TextView android:layout_width="match_parent" android:layout_height="wrap_content" /> ListView>//Activity
Overrideprotected void onCreate(Bundle savedInstanceState) super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ListView lvData = findViewById(R.id.lv_data); TextView tvContent = new TextView(this); lvData.addView(tvContent); //Don"t vày this//Adapter
Overridepublic View getView(int position, View convertView, ViewGroup parent) //Don"t phối attachToRoot=true View view = LayoutInflater.from(mContext).inflate(R.layout.item_message, parent, true); /* *chỗ nàyánh xạ view và cập nhật dữ liệu của view */ return view;Nên nếu như bạn gặp lỗi: addView(View, LayoutParams) is not supported in AdapterView thì nên kiểm tra lại xem các bạn có gọi địa chỉ view bằng tay thủ công vào AdapterView không nhé..
Xem thêm: Yếu Tố Nguy Hiểm Là Gì ? Các Yếu Tố Nguy Hiểm Trong Lao Động
Đó là toàn cục những gì mình thích nói với các bạn về LayoutInflater. Hẹn gặp các bạn ở những bài tiếp nhé.