ویرگول
ورودثبت نام
امیر حسین باقری
امیر حسین باقریبرنامه نویس اندروید
امیر حسین باقری
امیر حسین باقری
خواندن ۳ دقیقه·۶ سال پیش

دیتا بایندینگ: خداحافظ findViewById(قسمت اول)

دیتا بایندینگ یکی از کتابخانه های شناخته شده اندروید می باشد که توسط گوگل پیاده سازی شده است. این کتابخانه بسیار کاربردی بوده و ویژگی های جذاب دارد که در این مقاله و مقاله بعدی به بررسی آنها میپردازیم.

ابتدا کتابخانه دیتا بایندیگ را به پروژه اضافه میکنیم. برای این کار کافیست کد زیر را به gradle برنامه اضافه کنیم.

android { ... dataBinding { enabled true } }


در مرحله بعد باید فایل xml لیوتی که میخواهیم نمایش دهیم را تغییر بدیم. تگ layout را به لیوت اضافه میکنیم(به عنوان تک پدر). به مثال زیر توجه کنید.

<layout xmlns:android=&quothttp://schemas.android.com/apk/res/android&quot xmlns:app=&quothttp://schemas.android.com/apk/res-auto&quot xmlns:tools=&quothttp://schemas.android.com/tools&quot> <androidx.constraintlayout.widget.ConstraintLayout android:layout_width=&quotmatch_parent&quot android:layout_height=&quotmatch_parent&quot tools:context=&quot.MainActivity&quot> <TextView android:id=&quot@+id/hello&quot android:layout_width=&quotwrap_content&quot android:layout_height=&quotwrap_content&quot android:text=&quotHello World!&quot app:layout_constraintBottom_toBottomOf=&quotparent&quot app:layout_constraintLeft_toLeftOf=&quotparent&quot app:layout_constraintRight_toRightOf=&quotparent&quot app:layout_constraintTop_toTopOf=&quotparent&quot /> </androidx.constraintlayout.widget.ConstraintLayout> </layout>

حال باید کدهایمان را تغییر داده و به جای findViewById از دیتابایندینگ استفاده کنیم. چطور این کار را انجام بدیم؟

به جای این کد

@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); TextView hello = findViewById(R.id.hello); hello.setText(&quotHello World!&quot); }

می نویسیم

@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); ActivityMainBinding binding = DataBindingUtil.setContentView(this,R.layout.activity_main); binding.hello.setText(&quotHello World!&quot); }

اگر از fragment استفاده می کنید میتوانید از روش زیر به ویو ها دسترسی پیدا کنید

به جای این کد

@Nullable @Override public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment_home, container, false); TextView hello = view.findViewById(R.id.hello); hello.setText(&quotHello World!&quot); return view.getRootView(); }

می نویسیم

@Nullable @Override public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { FragmentHomeBinding binding = DataBindingUtil.inflate( inflater, R.layout.fragment_home, container, false); binding.hello.setText(&quotHello World!&quot); return binding.getRoot(); }

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

@Nullable @Override public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { FragmentHomeBinding binding = FragmentHomeBinding.inflate(inflater, container, false); binding.hello.setText(&quotHello World!&quot); return binding.getRoot(); }

فرض کنید در یک صفحه، ۲۰ عدد ویو داشتیم، که فرض دور از واقعیت هم نیست. در این صورت در حالت قبل مجبور بودیم بیست خط findViewById بنویسیم، که با کمک کتابخانه دیتابایندینگ دیگر لازم نیست آن ها را بنویسیم. این ویژگی گفته شده، یکی از ابتدایی ترین مزیت های دیتابایندیگ می باشد.

دیتابایندینگ، مزیت های پیچیده و کاربردی تر دیگری هم دارد که به وسیله آن میتوان در xml متغییر تعریف کنیم و به صورت داینامیک ui را تغییر دهیم. از این مزیت در معماری mvvm به کررات استفاده می شود. در مقاله بعدی به بررسی این ویژگی ها میپردازیم.

لایک و سابسکرایب فراموش نشه. اگر هم سوالی داشتید تو کامنتا برسید! ممنون از همتون

databindingdata binding
۱۷
۳
امیر حسین باقری
امیر حسین باقری
برنامه نویس اندروید
بیستون
بیستون
خلاقیت قلب هر رویاست. هر ایده و هر قدم بزرگ که جهان ما را تغییر می‌دهد با دورنمایی شروع می‌شود که خالقان آن ایده دارند و ما در شرکت بیستون ابزارهایی که آن خالقان نیاز دارند را برایشان مهیا می‌سازیم تا ایده‌هایشان را به واقعیت بدل کنند.
شاید از این پست‌ها خوشتان بیاید