آموزش LayoutInflater در برنامه نویسی اندروید

سلام دوستان امیدوارم سلامت باشید در این سری از آموزش برنامه نویسی اندروید به آموزش LayoutInflater در برنامه نویسی اندروید می پردازیم از LayoutInflater به عنوان inflate کردن لایه های دیگر در یک view استفاده می شود در ادامه LayoutInflater را با یک مثال برای شما شرح خواهیم داد با ما همراه باشید.
به طور مثال فکر کنید ما می خواهیم یک لایه dynamic ایجاد کنیم که در view های دیگر به نمایش در بیادش به طور مثال یک دایره داریم که بخواهیم هربار در view ما نمایش داده شود به این منظور باید از LayoutInflater  استفاده کنیم اگر از LayoutInflater  استفاده نکنیم و بخواهیم آن را FindView کنیم با خطای Null Object reference
ابتدا یک لایه به نام layout_item.xml ایجاد کنید و در آن کدهای زیر را قرار دهید.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="fill_parent"
              android:layout_height="fill_parent"
              android:id="@+id/layout_item_id">
    <TextView android:layout_width="fill_parent"
              android:layout_height="wrap_content"
              android:text="Programchi.ir - Hello, this is the inflated text :D"
              android:layout_gravity="center"
              android:gravity="center_horizontal"
              android:id="@+id/text_item_id"/>
</LinearLayout>

این لایه ای است که آن را inflate می کنیم در بالا یک linear گرفتیم و در داخل آن یک TextView قرار دادیم.
فکر کنید ما یک layout اصلی داریم به نام activity_main.xml و در آن کدهای زیر  قرار دارد.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:id="@+id/main_layout_id">
</LinearLayout>

در بالا لایه اصلی ما یک آیدی داره در ادامه از آن استفاده می کنیم.
ابتدا باید LinearLayout را find کنیم همانند زیر

LinearLayout mainLayout = (LinearLayout)findViewById(R.id.main_layout_id);

سپس همانند زیر از LayoutInflater  استفاده می کنیم.

View view = getLayoutInflater().inflate(R.layout.layout_item, mainLayout,false);

سپس از addView استفاده می کنیم.

mainLayout.addView(view);

کل نمونه کد برای استفاده

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
public class MyActivity extends Activity
{
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        LinearLayout mainLayout = (LinearLayout)findViewById(R.id.main_layout_id);
        View view = getLayoutInflater().inflate(R.layout.layout_item, mainLayout,false);
         mainLayout.addView(view);
    }
}

یگ نمونه کد دیگر برای آشنایی بیشتر

LayoutInflater layoutInflater = LayoutInflater.from(context);
View view = inflater.inflate(R.layout.your_layout, null);

نمونه مثال کد layoutInfalter با گرفتن LAYOUT_INFLATER_SERVICE

LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.list_item, parent, false);

 
 
موفق و موید باشید.

مطالعه بیشتر