在这种特定情况下,我们可以使用 android 数据绑定替换 findViewById() 吗?

Can we replace findViewById() using android databinding in this particular scenario?

在 Google 的 android 数据绑定之后,我一直在删除我的代码中的 findViewById()

我有一个回收站视图,其中包含针对特定项目视图中每个按钮的点击侦听器。在这种特殊情况下,我试图找到一种方法来消除 findViewById() 方法。

来自 MainFragment.java 文件的代码片段:

// 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();
                        }
                    }
            ));

我想从上面的片段中更改的行:

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

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

是的,你可以替换它,你可能应该这样做。

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

?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>

在您的视图持有者中,声明一个 属性 以持有对您的项目布局可绑定的引用:

lateinit var binding: YoutItemLayoutBindable

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

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(...) 中,将项目设置为绑定的变量:

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

就是这样!您的代码可能与此示例不同(也许您甚至没有使用视图模型),但我希望您能理解。