代码之家  ›  专栏  ›  技术社区  ›  Abhimanyu

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

  •  0
  • Abhimanyu  · 技术社区  · 6 年前

    在谷歌安卓数据绑定之后,我一直在删除 findViewById() 在我的代码中。

    我有一个回收器视图,在一个特定的项目视图中,每个按钮都有一个click listener。我正在设法消除 方法。

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

    这可能吗?如果是,有人能帮我做同样的事情吗?
    或者我应该离开 方法本身?

    0 回复  |  直到 6 年前
        1
  •  2
  •   Julio E. Rodríguez Cabañas    5 年前

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

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