📋 作者介绍:友友们好我是乱披风锤,大二学生党一枚
💖作者主页:乱披风锤的个人博客主页.乱披风锤
🔥我和友友们一样喜欢编辑,天天敲代码,沉迷学习,日渐消瘦。很荣幸能向大家分享我的所学,和大家一起进步。如果文章有错误,欢迎在评论区指正。那么开始今天的学习吧!
🎉 支持我:点赞👍+收藏⭐️+留言📝
一、开启Databinding
//在build.gradle中添加
android {
compileSdk 32
dataBinding{
enabled=true
}
二、添加依赖
implementation("com.squareup.okhttp3:okhttp:4.9.3")
implementation 'com.squareup.retrofit2:retrofit:2.9.0'
implementation 'com.squareup.retrofit2:converter-gson:2.9.0'
implementation 'com.squareup.okhttp3:logging-interceptor:4.9.3'
implementation 'com.github.bumptech.glide:glide:4.13.0'
annotationProcessor 'com.github.bumptech.glide:compiler:4.13.0'
三、打开网络权限
四、生成databinding布局
光标在布局文件的根布局–>点击Alt + Enter–>点击 “Convert to data binding layout”
<?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"
xmlns:tools="http://schemas.android.com/tools">
<data>
<!--放变量数据-->
</data>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
五、创建数据类
package com.hnucm.databinding;
import androidx.databinding.BaseObservable;
public class Person extends BaseObservable {
public int age;
public String name;
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
notifyChange();
}
}
六、在data变量中声明变量及类全名
<data>
<variable
name="itemlist"
type="com.hnucm.databinding.ChatItem.ChatlistDTO" />
</data>
七、通过@{}使用变量;(默认值无需加引号,且只在预览视图显示)
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="28dp"
android:layout_marginTop="13dp"
android:text="@{itemlist.name}"
app:layout_constraintStart_toEndOf="@+id/imageView"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="7dp"
android:layout_marginTop="20dp"
android:text="@{itemlist.content}"
app:layout_constraintStart_toStartOf="@+id/textView"
app:layout_constraintTop_toBottomOf="@+id/textView" />
八、databinding在recycleview中使用
ActivityMainBinding activityMainBinding;
public List<ChatItem.ChatlistDTO> chatlist = new ArrayList<>();
MyAdapter myAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
activityMainBinding = DataBindingUtil.setContentView(this,R.layout.activity_main);
Api api = RetrofitUtils.getRetrofit("http://121.4.44.56/").create(Api.class);
Call<ChatItem> chatItemCall = api.getChatList();
chatItemCall.enqueue(new Callback<ChatItem>() {
@Override
public void onResponse(Call<ChatItem> call, Response<ChatItem> response) {
ChatItem chatItem = response.body();
chatlist.addAll(chatItem.chatlist);
myAdapter.notifyDataSetChanged();
}
@Override
public void onFailure(Call<ChatItem> call, Throwable t) {
}
});
myAdapter = new MyAdapter();
activityMainBinding.recyclerView.setAdapter(myAdapter);
activityMainBinding.recyclerView.setLayoutManager(new LinearLayoutManager(this));
}
public class MyAdapter extends RecyclerView.Adapter<MyViewHodler>{
@NonNull
@Override
public MyViewHodler onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
ChatItemBinding chatItemBinding = DataBindingUtil.
inflate(LayoutInflater.from(MainActivity.this),R.layout.chat_item,parent,false);
MyViewHodler myViewHodler = new MyViewHodler(chatItemBinding.getRoot());
myViewHodler.chatItemBinding = chatItemBinding;
return myViewHodler;
}
@Override
public void onBindViewHolder(@NonNull MyViewHodler holder, int position) {
holder.chatItemBinding.setItemlist(chatlist.get(position));
}
@Override
public int getItemCount() {
return chatlist.size();
}
}
public class MyViewHodler extends RecyclerView.ViewHolder{
public ChatItemBinding chatItemBinding;
public MyViewHodler(@NonNull View itemView) {
super(itemView);
}
}
}
文章来源:https://www.toymoban.com/news/detail-424075.html
总结
好了,那么今天的学习就到这里了。友友们觉得不错的可以给个关注,点赞或者收藏哦!感谢各位友友们的支持。以下的代码希望各位大佬们自行检验哦,毕竟亲手操作让记忆更加深刻。文章来源地址https://www.toymoban.com/news/detail-424075.html
到了这里,关于Android Studio第12课关于databinding的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!