android - 在这个特定场景中,我们可以使用 android 数据绑定(bind)来替换 findViewById() 吗?

标签 android android-databinding findviewbyid

遵循 Google 的 Android 数据绑定(bind),我已删除代码中的 findViewById()

我有一个回收器 View ,其中包含特定项目 View 中每个按钮的单击监听器。我正在尝试找到一种方法来消除这种特殊情况下的 findViewById() 方法。

MainFragment.java 文件中的代码 fragment :

// Add touch listener for the recycler view
    mainFragmentBinding.mainFragmentFoodItemsRecyclerView.addOnItemTouchListener(
            new RecyclerTouchListener(
                    getContext(),
                    mainFragmentBinding.mainFragmentFoodItemsRecyclerView,
                    new ClickListener() {

                        @Override
                        public void onClick(View view, final int position) {


                            Button addButton = view.findViewById(R.id.food_add_button);

                            addButton.setOnClickListener(addButtonView -> {

                                // Notify user
                                Toast.makeText(getContext(), "Clicked on Add button",
                                        Toast.LENGTH_SHORT).show();
                            });

                            // Values are passing to activity & to fragment as well
                            Toast.makeText(
                                    getContext(),
                                    "Single Click on position : " + position,
                                    Toast.LENGTH_SHORT
                            ).show();
                        }

                        @Override
                        public void onLongClick(View view, int position) {

                            Toast.makeText(
                                    getContext(),
                                    "Long press on position : " + position,
                                    Toast.LENGTH_LONG
                            ).show();
                        }
                    }
            ));

我想从上面的代码 fragment 中更改的行:

Button addButton = view.findViewById(R.id.food_add_button);

这可能吗?如果是,有人可以帮我吗?
或者我应该保留 findViewById() 方法吗?

最佳答案

是的,您可以替换它,而且您可能应该这样做。

为此,首先准备数据绑定(bind)的项目布局并在其中添加点击回调:

?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <data>
        <variable
            name="item"
            type="com.example.your.item.Type"/>
        <variable
            name="parentViewModel"
            type="com.example.your.viewmodel.Type"/>
    </data>

    <YourLayout
        ...
        android:onClick="@{() -> parentViewModel.onItemClicked(item)}"
        ...
        YourLayout />

</layout>

在 View 持有者中,声明一个属性来保存对可绑定(bind)项目布局的引用:

lateinit var binding: YoutItemLayoutBindable

在适配器的 onCreateViewHolder(...) 内,确保使用数据绑定(bind)来扩充项目布局:

val inflater = LayoutInflater.from(parent.context)
val binding = DataBindingUtil.inflate<YoutItemLayoutBindable>(inflater, R.layout.your_item_layout, parent, false)
val viewHolder = ViewHolder(binding.root)

// Pass your activity/fragment as the lifeCycleOwner when creating the adapter
binding.lifecycleOwner = lifeCycleOwner

// Pass also your viewmodel as a parameter when creating the adapter
binding.parentViewModel = parentViewModel

viewHolder.binding = binding

return viewHolder

然后,在 onBindViewHolder(...) 内,将该项目设置为绑定(bind)的变量:

val item = dataSet[position]
viewHolder.binding.item = item
viewHolder.binding.executePendingBindings()

就是这样!您的代码可能与此示例不同(也许您甚至没有使用 View 模型),但我希望您能明白这个想法。

关于android - 在这个特定场景中,我们可以使用 android 数据绑定(bind)来替换 findViewById() 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55571335/

相关文章:

Android 网格形成

java - Android 数据绑定(bind)中何时计算表达式?

android - findViewById错误-没有足够的信息来推断类型变量T。我复制了java代码并在线转换了它

java - Android 上 EditText 的一次性 OnFocusChangeListener

android - 带有复选框的android ListView 中未触发复选框单击事件

android - 将一个类绑定(bind)到两个布局?

android - 将 LiveData 与数据绑定(bind)一起使用

Android - findViewById 返回 null

android - onCreate中调用findViewById时出现NullPointerException

android - "Please select Android SDK"- 如何导入项目文件?