Mình chỉ tất cả thói thân quen là video để chia sẻ kiến thức thiết kế như tự nhiên lại mong muốn viết để trải nghiệm phải mình đang 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 sẵn Android. Hy vọng các bạn ủng hộ và trong bài viết này bạn muốn chia sẻ với các bạn về LayoutInflater.
Bạn đang xem: Tìm hiểu về layoutinflater trong android
Định nghĩa:
LayoutInflater là 1 component giúp bạn chuyển layout file(Xml) thành View(Java code) vào Android. Bạn thường sử dụng nó trong thủ tục 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 gồm 2 phương pháp để tạo ra đối tượng người dùng LayoutInflater:
1.LayoutInflater là 1 System Service của apk và cách thực hiện của nó giống hệt như các System Service không giống như khi chúng ta sử dụng WINDOW_SERVICE, ALARM_SERVICE tốt LOCATION_SERVICE.
LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);Đây là bí quyết được khuyên sử dụng nhưng nó tương đối dài cái và tôi siêu ít khi áp dụng cách này.2. Sử dụng static method của LayoutInflater:
LayoutInflater layoutInflater = LayoutInflater.from(context);Đây là bí quyết tôi hay được dùng nhất bởi nó gọn ghẽ ^_^
Phương thức Inflate
Công bài toán của LayoutInflater là gọi xml layout file và biến đổi các trực thuộc tính của chính nó thành 1 View trong Java code. Sau khoản thời gian có đối tượng LayoutInflater, ta rất có thể dùng cách thức inflate để đổi khác 1 xml layout file thành 1 View trong java. Ta tất cả 2 phương thức inflate với con số tham số không giống nhau:
1. View view = layoutInflater.inflate(int resource, ViewGroup parent)2. View view = layoutInflater.inflate(int resource, ViewGroup parent, boolean attachToRoot)Các bạn sẽ thắc mắc những tham số của inflater có chân thành và ý nghĩa gì? 2 phương thức inflate bên trên chỉ khác biệt tham số attachToRoot vậy attachToRoot là gì? Cùng khám phá thông qua một số lấy ví dụ nhé.Trước tiên họ tìm gọi 3 tham số của nó là gì sẽ nhé:Như định nghĩa thì nhiệm vụ của LayoutInflater là biến đổi xml layout tệp tin thành đối tượng người dùng View trong java code, vậy thì:
Tham số đầu tiên là: int resource, nó chính là xml layout file mà chúng ta muốn đổi khác thành View.Tham số vật dụng hai là: ViewGroup parent, nó là ViewGroup nơi mà xml layout file(tham số lắp thêm nhất) hoàn toàn có thể được nhúng vào, LayoutInflater sẽ biến đổi xml layout tệp tin thành View cùng sử dụng những thuộc tính phù hợp với ViewGroup parrent.Tham số thứ ba là: attachToRoot, khi mà attachToRoot=true thì ngay sau khoản thời gian quá trình đổi khác xml file(resource) thành View hoàn thành thì nó sẽ nhúng View đó vào ViewGroup parent (RIGHT NOW) , lúc attachToRoot = false thì nó chỉ chuyển đổi xml file(resource) thành View vào java cơ mà không thêm ngay lập tức vào ViewGroup(NOT NOW)Rồi cùng đi vào ví dụ cho dễ nắm bắt nào..Tôi có xml layout file mang tên là activity_main.xml cùng với root là LinearLayout hướng 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 tệp tin khác thương hiệu 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 chừ tôi sẽ sử dụng lần lượt những phương thức inflate cùng tôi đã chỉ ra kết quả sau khi chúng ta sử dụng nó
// TH1: chúng ta chỉ sử dụng 2 tham số mặc dù attachToRoot sẽ được đặt mang định bằng true và công dụng là item_button đang được đổi khác thành View và được địa chỉ cửa hàng 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: bọn họ chỉ sử dụng 3 tham cùng với attachToRoot = true, khi đó item_button sẽ tiến hành chuyển thành View và được showroom vào llMain tức thì khi đổi khác hoàn tất như là TH1.
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à TH2 tất cả cùng công dụng như hình 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 không được địa chỉ vào LinearLayout như hình mặt 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 sử dụng LayoutInflater vào custom adapter
Qua 3 lấy ví dụ trên dĩ nhiên bạn cũng đã hiểu thông số attachToRoot dùng để gia công gì rồi yêu cầu không? tóm lại thì attachToRoot ra quyết định View mà lại được tạo nên bởi qua trình inflate của LayoutInflater tất cả được add vào ViewGroup parent tuyệt không.Nhưng bọn họ thử so với trường hợp cần sử dụng LayoutInflater trong cách thức getView khi custom adapter coi nhé.Ở trong cách làm getView chúng ta hay áp dụng 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à update dữ liệu của view */ return view;Ở cách thức trên thì LayoutInflater đang đọc file 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 áp dụng đoạn code trên và mặt Activity bạn set adapter thì đều chuyện phần nhiều ổn..ứng dụng chúng ta chạy bình thường và listview đã hiển thị các tin nhắn.
Nhưng bạn nhận biết là listview mong muốn hiển thị những item message, vậy vì sao khi tạo nên view từ item_message.xml chúng ta không gán luôn luôn nó vào listview bằng phương pháp cho tham số attachToRoot=true sinh sống trong cách tiến hành 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à update dữ liệu của view */ return view; Nếu bạn thực hiện code này thì ứng dụng của các bạn sẽ bị dừng bất ngờ đột ngột 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 thực hiện đ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);Ở phía trên LayoutInflater đang đọc tệp tin item_button.xml thành View (Java code) và địa chỉ cửa hàng nó làm bé của ViewGroup llMain. Hiệu quả sẽ tương tự như với xml layout tệp tin 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 địa chỉ view so với các ViewGroup như LinearLayout tốt RelativeLayout thì trọn vẹn bình thường, vì những viewgroup gồm thể đựng nhiều View bé hoặc ViewGroup không giống ở vào 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ể thủ công bằng việc add trong xml hoặc vào 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ì this//Adapter
Overridepublic View getView(int position, View convertView, ViewGroup parent) //Don"t set 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 giả dụ bạn gặp mặt lỗi: addView(View, LayoutParams) is not supported in AdapterView thì hãy kiểm tra lại xem bạn có gọi địa chỉ cửa hàng view thủ công bằng tay vào AdapterView ko nhé..
Xem thêm: Nghĩa Của Từ Diplomatic Là Gì, Diplomatic Translation Into Vietnamese
Đó là toàn bộ những gì bạn thích nói với các bạn về LayoutInflater. Hẹn gặp các bạn ở các bài tiếp nhé.