赞
踩
android {
...
buildFeatures {
viewBinding true
}
...
}
android {
...
viewBinding.enabled = true
...
}
public final class ActivityViewBindingTestBinding implements ViewBinding { @NonNull private final ConstraintLayout rootView; private ActivityViewBindingTestBinding(@NonNull ConstraintLayout rootView) { this.rootView = rootView; } @Override @NonNull public ConstraintLayout getRoot() { return rootView; } @NonNull public static ActivityViewBindingTestBinding inflate(@NonNull LayoutInflater inflater) { return inflate(inflater, null, false); } @NonNull public static ActivityViewBindingTestBinding inflate(@NonNull LayoutInflater inflater, @Nullable ViewGroup parent, boolean attachToParent) { View root = inflater.inflate(R.layout.activity_view_binding_test, parent, false); if (attachToParent) { parent.addView(root); } return bind(root); } @NonNull public static ActivityViewBindingTestBinding bind(@NonNull View rootView) { if (rootView == null) { throw new NullPointerException("rootView"); } return new ActivityViewBindingTestBinding((ConstraintLayout) rootView); } }
class ViewBindingTestActivity : AppCompatActivity() {
private val vb: ActivityViewBindingTestBinding by lazy {
ActivityViewBindingTestBinding.inflate(
layoutInflater
)
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(vb.root)
}
}
class ViewBindingTestFragment : Fragment() { //注意 _binding 和 binding,因为需要在 onDestroyView 进行释放,以免内存泄漏,所以设计这样 private var _binding: FragmentViewBindingTestBinding? = null private val binding get() = _binding!! override fun onCreateView( inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle? ): View? { // Inflate the layout for this fragment _binding = FragmentViewBindingTestBinding.inflate(layoutInflater, container, false) binding.tv.text = "测试一下ViewBinding" return binding.root } override fun onDestroyView() { super.onDestroyView() _binding = null } }
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".viewbinding.ViewBindingTestActivity"> <fragment android:id="@+id/fragmentTest" android:name="com.example.cmlayoutlearn.viewbinding.ViewBindingTestFragment" android:layout_width="match_parent" android:layout_height="wrap_content" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> <include android:id="@+id/includeItem" layout="@layout/include_item_layout" /> <androidx.recyclerview.widget.RecyclerView android:id="@+id/rcv" android:layout_width="match_parent" android:layout_height="wrap_content" tools:itemCount="3" tools:listitem="@layout/item_vb_test_view" /> </LinearLayout>
class ViewBindingTestActivity : AppCompatActivity() { private val vb: ActivityViewBindingTestBinding by lazy { ActivityViewBindingTestBinding.inflate( layoutInflater ) } override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(vb.root) vb.includeItem.tvContent.text = "内容设置" val arrayList = ArrayList<String>() for (i in 0..30) { arrayList.add("i = $i") } vb.rcv.layoutManager = LinearLayoutManager(this) vb.rcv.adapter = VbRcvAdapter(arrayList) } }
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".viewbinding.ViewBindingTestActivity"> <fragment android:id="@+id/fragmentTest" android:name="com.example.cmlayoutlearn.viewbinding.ViewBindingTestFragment" android:layout_width="match_parent" android:layout_height="wrap_content" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> <include layout="@layout/include_item_layout" /> <androidx.recyclerview.widget.RecyclerView android:id="@+id/rcv" android:layout_width="match_parent" android:layout_height="wrap_content" tools:itemCount="3" tools:listitem="@layout/item_vb_test_view" /> </LinearLayout>
class ViewBindingTestActivity : AppCompatActivity() { private val vb: ActivityViewBindingTestBinding by lazy { ActivityViewBindingTestBinding.inflate( layoutInflater ) } private val vbInclude: IncludeItemLayoutBinding by lazy { IncludeItemLayoutBinding.bind(vb.root) } override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(vb.root) // vb.includeItem.tvContent.text = "内容设置" vbInclude.tvContent.text = "内容设置" val arrayList = ArrayList<String>() for (i in 0..30) { arrayList.add("i = $i") } vb.rcv.layoutManager = LinearLayoutManager(this) vb.rcv.adapter = VbRcvAdapter(arrayList) } }
class VbRcvAdapter(private val dataList: ArrayList<String>) : RecyclerView.Adapter<VbTestViewHolder>() { override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): VbTestViewHolder { val vb = ItemVbTestViewBinding.inflate(LayoutInflater.from(parent.context), parent, false) return VbTestViewHolder(vb) } override fun onBindViewHolder(holder: VbTestViewHolder, position: Int) { holder.bindDataToView(dataList[position]) } override fun getItemCount(): Int = dataList.size } class VbTestViewHolder(val vb: ItemVbTestViewBinding) : RecyclerView.ViewHolder(vb.root) { fun bindDataToView(str: String) { vb.tv.text = str //... } }
class VbCustomViewTest @JvmOverloads constructor( context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0 ) : FrameLayout(context, attrs, defStyleAttr) { private val vb: ItemRcvBinding by lazy { ItemRcvBinding.inflate( //必须用三个参数的方法(最外层使用merge,则为两个) LayoutInflater.from(context), this, true //必须为true,如果最外层使用merge,则此参数不再使用(编译器会提醒) ) } init { renderView(context) } private fun renderView(context: Context) { vb.tvTitle.text = "1111111" } fun setTitle(string: String){ vb.tvTitle.text = string }
public class VbJavaTestActivity extends AppCompatActivity {
private ActivityVbJavaTestBinding vb;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
vb = ActivityVbJavaTestBinding.inflate(getLayoutInflater());
setContentView(vb.getRoot());
vb.tv.setText("测试一下");
}
}
public class VbJavaTestFragment extends Fragment { private FragmentVbJavaTestBinding vb; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // Inflate the layout for this fragment vb = FragmentVbJavaTestBinding.inflate(inflater, container, false); return vb.getRoot(); } @Override public void onDestroyView() { super.onDestroyView(); vb = null; } }
后续补充…
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。