Android数据绑定到listview,比如WPF?
我正在尝试进入android中的数据绑定。 因为我对WPF中的数据绑定有更多的经验,所以我非常困惑 您尝试过的内容: 在WPF中,很容易将对象列表绑定到带有自定义项的列表视图。以下是一个来自私人项目的示例: 就这样。不需要胶水代码或适配器 我的问题:Android数据绑定到listview,比如WPF?,android,listview,data-binding,Android,Listview,Data Binding,我正在尝试进入android中的数据绑定。 因为我对WPF中的数据绑定有更多的经验,所以我非常困惑 您尝试过的内容: 在WPF中,很容易将对象列表绑定到带有自定义项的列表视图。以下是一个来自私人项目的示例: 就这样。不需要胶水代码或适配器 我的问题: 是否可以将项目绑定到列表视图,而不使用列表中的任何代码 背景 我可以在listview的布局中定义“列表视图项”吗 因为我很困惑,关于适配器/膨胀的东西等等。 有一个很好的关于绑定到列表的教程,但是我仍然需要为绑定编写代码 安卓系统的情况不
- 是否可以将项目绑定到列表视图,而不使用列表中的任何代码 背景
- 我可以在listview的布局中定义“列表视图项”吗
安卓系统的情况不同 是否可以在后台不使用任何代码的情况下将项目绑定到列表视图? 必须在列表视图的适配器类中使用数据绑定代码 我可以在listview的布局中定义“列表视图项”吗? 不,你不能!列表视图项必须有自己的布局 由于您对适配器感到困惑,以下几点可以让您更清楚: 将适配器视为管理数据模型并使其适应列表视图的各个条目的管理器。适配器将填充每行的布局,并将数据分配给行中的单个视图 没有数据绑定,适配器类可以包含大量代码,具体取决于行UI的复杂程度。因此,使用数据绑定将有助于通过几行绑定代码从适配器类中删除所有不必要的代码 您发布的链接已经足够好了,可以开始了,但我建议使用回收器视图。以下是您可以查看的其他链接
Android数据绑定需要一些代码来完成 前面的步骤1可以被所有项目重用,下面的步骤2和3都是布局资源文件,就像WPF所做的那样 第一步。为所有AbsListView定义BindingAdapter类,其他项目AbsListView可以重用该类
public class AbsListViewBindingAdapter {
@BindingAdapter(value = {"android:items", "android:itemTemplate", "android:dropDownItemTemplate"}, requireAll = false)
public static <T> void setListAdapter(AbsListView view, List<T> items, @LayoutRes int itemTemplateLayout, @LayoutRes int dropDownItemTemplateLayout) {
final ListAdapter oldAdapter = view.getAdapter();
if (oldAdapter instanceof ObservableListAdapter) {
((ObservableListAdapter<T>) oldAdapter).setParams(items, itemTemplateLayout, dropDownItemTemplateLayout);
} else {
view.setAdapter(new ObservableListAdapter<>(view.getContext(), items, itemTemplateLayout, dropDownItemTemplateLayout));
}
}
@BindingAdapter(value = {"android:items", "android:itemTemplate", "android:dropDownItemTemplate"}, requireAll = false)
public static <T> void setListAdapter(AbsListView view, T[] items, @LayoutRes int itemTemplateLayout, @LayoutRes int dropDownItemTemplateLayout) {
setListAdapter(view, items != null ? Arrays.asList(items) : null, itemTemplateLayout, dropDownItemTemplateLayout);
}
@BindingAdapter(value = {"android:items", "android:itemTemplate", "android:dropDownItemTemplate"}, requireAll = false)
public static <T> void setListAdapter(AbsListView view, int[] items, @LayoutRes int itemTemplateLayout, @LayoutRes int dropDownItemTemplateLayout) {
setListAdapter(view, items != null ? IntStream.of(items).boxed().collect(Collectors.toList()) : null, itemTemplateLayout, dropDownItemTemplateLayout);
}
static class ObservableListAdapter<T> extends BaseAdapter {
private List<T> mList;
private int mDropDownResourceId = 0;
private int mResourceId = 0;
private final LayoutInflater mLayoutInflater;
final ObservableList.OnListChangedCallback mListChangedCallback = new ObservableList.OnListChangedCallback() {
@Override
public void onChanged(ObservableList observableList) {
notifyDataSetChanged();
}
@Override
public void onItemRangeChanged(ObservableList observableList, int i, int i1) {
notifyDataSetChanged();
}
@Override
public void onItemRangeInserted(ObservableList observableList, int i, int i1) {
notifyDataSetChanged();
}
@Override
public void onItemRangeMoved(ObservableList observableList, int i, int i1, int i2) {
notifyDataSetChanged();
}
@Override
public void onItemRangeRemoved(ObservableList observableList, int i, int i1) {
notifyDataSetChanged();
}
};
public ObservableListAdapter(Context context, List<T> list, @LayoutRes int itemTemplate, @LayoutRes int dropDownItemTemplate) {
mLayoutInflater = LayoutInflater.from(context);
setParams(list, itemTemplate, dropDownItemTemplate);
}
public void setParams(List<T> list, @LayoutRes int itemTemplate, @LayoutRes int dropDownItemTemplate) {
boolean requireNotifyChange = mResourceId != itemTemplate || mDropDownResourceId != dropDownItemTemplate || !Objects.equals(list, mList);
mResourceId = itemTemplate;
mDropDownResourceId = dropDownItemTemplate;
if (!Objects.equals(list, mList)) {
if (mList instanceof ObservableList) {
((ObservableList) mList).removeOnListChangedCallback(mListChangedCallback);
}
mList = list;
if (mList instanceof ObservableList) {
((ObservableList) mList).addOnListChangedCallback(mListChangedCallback);
}
}
if (requireNotifyChange) {
notifyDataSetChanged();
}
}
@Override
public int getCount() {
return (mList != null) ? mList.size() : 0;
}
@Override
public T getItem(int position) {
return mList != null ? mList.get(position) : null;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
return getViewForResource(mResourceId, position, convertView, parent);
}
@Override
public View getDropDownView(int position, View convertView, ViewGroup parent) {
return getViewForResource(mDropDownResourceId, position, convertView, parent);
}
public View getViewForResource(int resourceId, int position, View convertView, ViewGroup parent) {
final ViewDataBinding binding = (convertView != null)
? DataBindingUtil.getBinding(convertView)
: DataBindingUtil.inflate(mLayoutInflater, resourceId, parent, false);
binding.setVariable(BR.viewModel, getItem(position));
return binding.getRoot();
}
}
}
公共类AblistViewBindingAdapter{
@BindingAdapter(值={“android:items”、“android:itemTemplate”、“android:dropDownItemTemplate”},requireAll=false)
公共静态void setListAdapter(AbsListView视图,列表项,@LayoutRes int itemTemplateLayout,@LayoutRes int dropDownItemTemplateLayout){
最终列表adapter oldapter=view.getAdapter();
if(ObservableListAdapter的旧适配器实例){
((ObservableListAdapter)oldAdapter).setParams(items,itemTemplateLayout,dropDownItemTemplateLayout);
}否则{
setAdapter(新的ObservableListAdapter(view.getContext(),items,itemTemplateLayout,dropDownItemTemplateLayout));
}
}
@BindingAdapter(值={“android:items”、“android:itemTemplate”、“android:dropDownItemTemplate”},requireAll=false)
公共静态void setListAdapter(AbsListView视图,T[]项,@LayoutRes int itemTemplateLayout,@LayoutRes int dropDownItemTemplateLayout){
setListAdapter(视图,项!=null?数组。asList(项):null,itemTemplateLayout,dropDownItemTemplateLayout);
}
@BindingAdapter(值={“android:items”、“android:itemTemplate”、“android:dropDownItemTemplate”},requireAll=false)
公共静态void setListAdapter(AbsListView视图,int[]项,@LayoutRes int itemTemplateLayout,@LayoutRes int dropDownItemTemplateLayout){
setListAdapter(视图,items!=null?IntStream.of(items).boxed().collect(Collectors.toList()):null,itemTemplateLayout,dropDownItemTemplateLayout);
}
静态类ObservableListAdapter扩展BaseAdapter{
私人名单;
private int mDropDownResourceId=0;
私有int mResourceId=0;
私人最终布局平面图mLayoutInflater;
最终ObservableList.OnListChangedCallback mListChangedCallback=新ObservableList.OnListChangedCallback(){
@凌驾
更改后的公共无效(可观察列表可观察列表){
notifyDataSetChanged();
}
@凌驾
已更改的公共无效列表(可观察列表、可观察列表、int i、int i1){
notifyDataSetChanged();
}
@凌驾
插入的公共无效列表(可观察列表、可观察列表、int i、int i1){
notifyDataSetChanged();
}
@凌驾
已移动的公共无效列表(可观察列表、可观察列表、int i、int i1、int i2){
notifyDataSetChanged();
}
@凌驾
已删除的公共无效(可观察列表可观察列表,int i,int i1){
notifyDataSetChanged();
}
};
公共ObservableListAdapter(上下文上下文、列表列表、@LayoutRes int-itemTemplate、@LayoutRes int-dropDownItemTemplate){
mLayoutInflater=LayoutInflater.from(上下文);
setParams(列表、itemTemplate、dropDownItemTemplate);
}
public void setParams(列表列表、@LayoutRes int itemTemplate、@LayoutRes int dropdownitemplate){
布尔值requireNotifyChange=mResourceId!=itemTemplate | | | mDropDownResourceId!=dropDownItemTemplate | | |!Objects.equals(list,mList);
mResourceId=项目模板;
mDropDownResourceId=dropDownItemTemplate;
如果(!Objects.equals(list,mList)){
if(可观察列表的mList instanceof ObservableList){
((ObservableList)mList).removeOnListChangedCallback(mListChangedCallback);
}
mList=列表;
if(可观察列表的mList instanceof ObservableList){
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">
<data>
<variable
name="viewModel"
type="java.io.File" />
</data>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="0px"
android:layout_height="wrap_content"
android:layout_weight="0.2"
android:text='@{viewModel.directory ? "Folder" : "File"}'
android:textSize="20sp" />
<TextView
android:layout_width="0px"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@{viewModel.name}"
android:textSize="20sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text='@{viewModel.length()+ ""}'
android:textSize="20sp" />
</LinearLayout>
</layout>
<?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>
<import type="android.os.Environment" />
<variable
name="viewModel"
type="com.mycompany.databindingtest.MainActivity.ViewModel" />
</data>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:animateLayoutChanges="true"
android:dropDownItemTemplate="@{@layout/file_list_item_template}"
android:itemTemplate="@{@layout/file_list_item_template}"
android:items="@{Environment.externalStorageDirectory.listFiles()}" />
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>