1

我正在尝试干掉我的代码,并且我有几个活动使用相同的代码块,我想将它们移入父活动中的方法中。问题是这段代码使用生成的 ViewBindings 是唯一的类,我无法弄清楚父类是什么,以便将其用作方法参数。

例如,这段代码在两个不同的活动中,唯一的区别是一个活动绑定 = Activity1Binding,另一个是 Activity2Binding。他们共享一些具有相同 ID 的视图。

binding.noteTitleTV.setOnClickListener { changeTitle() }
binding.deleteModalLayout.setOnClickListener { binding.deleteModalLayout.visibility = View.GONE }
binding.cancelDeleteButton.setOnClickListener { binding.deleteModalLayout.visibility = View.GONE }
binding.confirmDeleteButton.setOnClickListener { onDeleteNoteClicked() }

如果可能的话,我想在父活动中实现类似的东西以防止重复代码:

fun setUp(binding: [BINDING PARENT CLASS]) {
  binding.noteTitleTV.setOnClickListener { changeTitle() }
  // etc
}
4

3 回答 3

1

生成的类扩展了 Object 类 (java.lang.Object)

于 2020-08-26T17:03:50.017 回答
0

ViewBinding 可以通过创建bind(view),所以你可以像这样创建一个基类:

abstract class BaseActivity : AppCompatActivity() {

    private lateinit var binding: Activity1Binding

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        val view = createContentView()
        // create ViewBinding
        binding = Activity1Binding.bind(view)
    }

    // create view by subclass
    abstract fun createContentView(): View

    fun setTextViewTitle(text: CharSequence) {
        binding.tvTitle.text = text
    }
}

这是内容Activity1Binding#bind()

  @NonNull
  public static ActivityMainBinding bind(@NonNull View rootView) {
    // The body of this method is generated in a way you would not otherwise write.
    // This is done to optimize the compiled bytecode for size and performance.
    int id;
    missingId: {
      id = R.id.layout;
      FinanceLabelLayout layout = rootView.findViewById(id);
      if (layout == null) {
        break missingId;
      }

      return new ActivityMainBinding((ConstraintLayout) rootView, layout);
    }
    String missingId = rootView.getResources().getResourceName(id);
    throw new NullPointerException("Missing required view with ID: ".concat(missingId));
  }

但这不推荐。

这不是类型安全的。

于 2021-03-19T06:55:53.870 回答
0

绑定类继承自 ViewDataBinding,因此您可以这样做(Kotlin 代码)

fun setUp(binding: ViewDataBinding) {
   when(binding){
      is Activity1Binding -> { (binding as Activity1Binding).noteTitelTV.setOnClickListner{ changeTitle() } }  
      is Activity2Binding -> { (binding as Activity2Binding).noteTitelTV.setOnClickListner{ changeTitle() } }  
   }
   // etc
}

我不知道它会变得比这更“通用”,因为您不控制生成的类。但这至少可以让您按照您的建议将所有代码放在一个类中。我使用了类似的方法,因为我有一个所有生成的绑定类的 lateinit 实例,并且只是根据传递的变量设置哪个是活动的,并使用该实例名称,所以我不必继续键入 as。

前任:

private lateinit var itemBinding : GroceryItemBinding
private lateinit var maintItemBinding : GroceryItemMaintBinding
private lateinit var compareItemBinding : GroceryItemCompareBinding
private lateinit var priceItemBinding : GroceryItemPriceBinding
private lateinit var adItemBinding : GroceryItemAdBinding
when(viewBinding){ 
   is GroceryItemMaintBinding -> {
      maintItemBinding = viewBinding as GroceryItemMaintBinding
      maintItemBinding.groceryItem = gi
      maintItemBinding.groceryItemImage.setOnClickListener { ... }
      ......
   }
   is GroceryItemBinding -> {
      itemBinding = viewBinding as GroceryItemBinding
   }
   ......
}
于 2021-01-10T18:20:04.467 回答