Android 如何将视图模型变量与自定义类类型绑定
什么有效? XML: 我的活动:Android 如何将视图模型变量与自定义类类型绑定,android,android-databinding,android-mvvm,Android,Android Databinding,Android Mvvm,什么有效? XML: 我的活动: @BindingAdapter({"items"}) public static void myMethod(View view, String[] feeds) { // Do somthing } 我想改变什么? 我想将字符串[]更改为列表,但未找到myMethod 我的提要类: public class Feed { private String name; public Feed(String
@BindingAdapter({"items"})
public static void myMethod(View view, String[] feeds) {
// Do somthing
}
我想改变什么?
我想将字符串[]更改为列表
,但未找到myMethod
我的提要类:
public class Feed {
private String name;
public Feed(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
现在,这个类只包含一个字符串成员,但当然他将包含更多的字符串成员。在viewmodel中进行如下更改
private MutableLiveData<List<Feed>> feedListLivedata = new MutableLiveData<>();;
public MyViewModel() {
//create a list of feed here
setFeeds(feeds);
}
public void setFeeds( List<Feed> feeds) {
feedListLivedata .postValue(feeds);
}
如果您希望将列表绑定到RecyclerView 要遵循的步骤 1-使用
app:items
标记在xml文件中创建一个RecyclerView,以设置要绑定的项目
<androidx.recyclerview.widget.RecyclerView
android:layout_width="match_parent"
android:id="@+id/recycler_view"
android:layout_height="match_parent"
android:scrollbars="vertical"
app:items="@{viewmodel.items}"
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager" />
使用字符串列表而不是字符串数组。谢谢您的回答,但是-您已经阅读了我的问题吗?字符串[]是有效的。列表不起作用@你能发布你的Feed类吗?@Rajnishsuryavanshi-当然。编辑。现在看。从哪里调用setFeeds方法。?好极了!救了我一天!顺便说一句它在我的片段中起作用。当我试图在另一个活动中执行相同操作时,在调用(!)postValue之前到达BindableAdapter方法。但在这种情况下,该值将为null。我的意思是,在片段类中,一切正常。但在活动课上不是。也许我们可以私下谈谈,让这个帖子保持清晰?当然。这是我出去玩的邮箱idrajnishsuryavanshi223@gmail.com.
private MutableLiveData<List<Feed>> feedListLivedata = new MutableLiveData<>();;
public MyViewModel() {
//create a list of feed here
setFeeds(feeds);
}
public void setFeeds( List<Feed> feeds) {
feedListLivedata .postValue(feeds);
}
app:items="@{viewModel.feedListLivedata }"
<androidx.recyclerview.widget.RecyclerView
android:layout_width="match_parent"
android:id="@+id/recycler_view"
android:layout_height="match_parent"
android:scrollbars="vertical"
app:items="@{viewmodel.items}"
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager" />
<?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">
<data>
<import type="android.widget.CompoundButton" />
<variable
name="task"
type="com...data.model.Task" />
<variable
name="viewmodel"
type="com...TaskListViewModel" />
</data>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:orientation="horizontal"
android:onClick="@{() -> viewmodel.openTask(task.id)}">
<CheckBox
android:id="@+id/complete"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:onClick="@{(view) -> viewmodel.completeTask(task, ((CompoundButton)view).isChecked())}"
android:checked="@{task.completed}" />
<TextView
android:id="@+id/title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginLeft="@dimen/activity_horizontal_margin"
android:layout_marginStart="@dimen/activity_horizontal_margin"
android:textAppearance="@style/TextAppearance.AppCompat.Title"
android:text="@{task.titleForList}"
app:completedTask="@{task.completed}" />
</LinearLayout>
</layout>
class TasksAdapter(private val viewModel: TaskListViewModel) :
ListAdapter<Task, CustomViewHolder>(TaskDiffCallback()) {
override fun onBindViewHolder(holder: CustomViewHolder, position: Int) {
val item = getItem(position)
holder.bind(viewModel, item)
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CustomViewHolder {
return CustomViewHolder.from(parent)
}
class CustomViewHolder private constructor(val binding: TaskItemBinding) :
RecyclerView.ViewHolder(binding.root) {
fun bind(viewModel: TaskListViewModel, item: Task) {
binding.viewmodel = viewModel
binding.task = item
binding.executePendingBindings()
}
companion object {
fun from(parent: ViewGroup): CustomViewHolder {
val layoutInflater = LayoutInflater.from(parent.context)
val binding = TaskItemBinding.inflate(layoutInflater, parent, false)
return CustomViewHolder(binding)
}
}
}
}
/**
* Callback for calculating the diff between two non-null items in a list.
*
* Used by ListAdapter to calculate the minimum number of changes between and old list and a new
* list that's been passed to `submitList`.
*/
class TaskDiffCallback : DiffUtil.ItemCallback<Task>() {
override fun areItemsTheSame(oldItem: Task, newItem: Task): Boolean {
return oldItem.id == newItem.id
}
override fun areContentsTheSame(oldItem: Task, newItem: Task): Boolean {
return oldItem == newItem
}
}