[安卓开发]kotlin编写 RecyclerView 结合 DataBinding 进行 双向绑定
kotlin编写的RecyclerView,使用DataBinding进行双向绑定实现了 BaseObservable 的数据类,其实也没啥必要,就一个String具体效果就是修改一侧RecyclerView数据,另一侧也会同步变化Unresolved reference: BR->这个错误可以见我的上篇博客直接上代码数据类class UseData() : BaseObservable()
·
kotlin编写的RecyclerView,使用DataBinding进行双向绑定
实现了 BaseObservable 的数据类,其实也没啥必要,就一个String
具体效果就是修改一侧RecyclerView数据,另一侧也会同步变化
Unresolved reference: BR ->这个错误可以见我的上篇博客
直接上代码
数据类
class UseData() : BaseObservable() {
var useData: String = ""
@Bindable get
set(value: String) {
if (useData != value) {
field = value
notifyChange()
}
}
}
adapter
class RecyclerViewLearnAdapter(private val dataTemp : Array<UseData>) :
RecyclerView.Adapter<RecyclerViewLearnAdapter.BindingHolder>() {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): BindingHolder {
val binding = DataBindingUtil.inflate<ConfigRecyclerViewLearnListItemBinding>(
LayoutInflater.from(parent.context),
viewType,
parent,
false
)
return BindingHolder(binding)
}
override fun onBindViewHolder(holder: BindingHolder, position: Int) {
holder.bindData(dataTemp[position])
}
override fun getItemCount() = dataTemp.size
override fun getItemViewType(position: Int): Int {
return R.layout.config_recycler_view_learn_list_item
}
class BindingHolder(private var binding : ConfigRecyclerViewLearnListItemBinding) : RecyclerView.ViewHolder(binding.root){
fun bindData(useData: UseData){
binding.setVariable(BR.mUseData,useData)
binding.executePendingBindings()
}
}
}
item布局
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<data>
<variable
name="mUseData"
type="com.example.myapplication.data.UseData" />
</data>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<EditText
android:id="@+id/list_item_textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/ic_launcher_background"
android:padding="10dp"
android:text="@={mUseData.useData}"
android:textSize="16sp" />
</LinearLayout>
</layout>
activity布局
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
tools:context=".RecyclerViewLearn">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recycle_view_learn_recycleview1"
android:layout_width="100dp"
android:layout_height="729dp"
app:layout_constraintEnd_toStartOf="@+id/textView2"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recycle_view_learn_recycleview2"
android:layout_width="100dp"
android:layout_height="729dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/textView2"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/recyclerview_textview_text"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
activity代码(java)
public class RecyclerViewLearn extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.config_recycler_view_learn_activity);
RecyclerView leftRecyclerView=findViewById(R.id.recycle_view_learn_recycleview1);
RecyclerView rightRecyclerView=findViewById(R.id.recycle_view_learn_recycleview2);
UseData[] data=new UseData[7];
data[0]=new UseData( );
data[1]=new UseData( );
data[2]=new UseData( );
data[3]=new UseData( );
data[4]=new UseData( );
data[5]=new UseData( );
data[6]=new UseData( );
data[0].setUseData("12345678");
data[1].setUseData("azazazaz");
data[2].setUseData("!!!!!!!!");
data[3].setUseData("????????");
data[4].setUseData("########");
data[5].setUseData("********");
data[6].setUseData("????????");
LinearLayoutManager leftLayoutManager = new LinearLayoutManager(this);
leftRecyclerView.setLayoutManager(leftLayoutManager);
RecyclerViewLearnAdapter leftAdapter=new RecyclerViewLearnAdapter(data);
leftRecyclerView.setAdapter(leftAdapter);
LinearLayoutManager rightLayoutManager = new LinearLayoutManager(this);
rightRecyclerView.setLayoutManager(rightLayoutManager);
RecyclerViewLearnAdapter rightAdapter=new RecyclerViewLearnAdapter(data);
rightRecyclerView.setAdapter(rightAdapter);
}
}
更多推荐
已为社区贡献2条内容
所有评论(0)