当前位置:   article > 正文

ViewBinding的使用(一)_viewbinding版本

viewbinding版本

~个人记录与学习使用~

一:基本配置和注意事项

  • AndroidStudio至少要升级到3.6
  • 项目配置的Android gradle plugin version 必须大于等于3.6.0
    也就是在project下的build.gradle文件中,classpath “com.android.tools.build:gradle:3.6.0” 的版本最低要为3.6.0
  • 在对应模块的build.gradle 下加入如下配置
android {
	...
	buildFeatures {
    	viewBinding true
	}
	...
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 有的低版本配置方式不同(和Android gradle plugin version 有关)
android {
	...
	viewBinding.enabled = true
	...
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • viewbinding会在 {modelName}/build/generated/data_binding_base_class_source_out/debug/out/{packageName}/databinding 下生成一个一个的binding文件,名称**{bindingFileName}.java**,名称和创建的layout的资源名有关。
    11111
  • 生成的文件代码例子如下:
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);
  }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38

二:简单使用

kotlin版本

  • Activity中使用:
class ViewBindingTestActivity : AppCompatActivity() {
    private val vb: ActivityViewBindingTestBinding by lazy {
        ActivityViewBindingTestBinding.inflate(
            layoutInflater
        )
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(vb.root)
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • Framgent中使用
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
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • Include使用(一)、给include布局设置id
<?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>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
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)
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • Include使用(二)、给include布局内设置 /merge/> 标签,然后bind父布局,可以不设置id了
<?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>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
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)
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • RecyclyView的ViewHolder使用
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
        //...
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 自定义View
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
    }

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24

Java版本

  • Activity中使用:
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("测试一下");
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • Framgent中使用
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;
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

后续补充…

声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop】
推荐阅读
相关标签
  

闽ICP备14008679号