使用自定义适配器进行Android数据绑定

6l7fqoea  于 2023-06-28  发布在  Android
关注(0)|答案(7)|浏览(140)

我尝试使用Android's Data Binding features与自定义适配器和ListView。重写自定义适配器的getView方法时遇到问题:

public class ChecksAdapter extends ArrayAdapter<Check> {

    public ChecksAdapter(Context context, ObservableList<Check> checks) {
        super(context, R.layout.check, checks);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        CheckBinding binding = DataBindingUtil.inflate(
                LayoutInflater.from(getContext()),
                R.layout.check, parent, false);
        binding.setCheck(this.getItem(position));

        // Return what?
    }
    
}

所以我的问题是

  • 在哪里可以得到应该返回的View元素?或者换句话说,如何将对象绑定到膨胀/转换的视图?
  • 使用数据绑定时如何重用convertView
  • 这是实现这一点的正确方法吗?该指南对ListViews不是很清楚

以下是指南中ListViews的唯一参考:
如果您在ListView或RecyclerView适配器中使用数据绑定项,您可能更喜欢用途:

ListItemBinding binding = ListItemBinding.inflate(layoutInflater, viewGroup, false);
   //or
   ListItemBinding binding = DataBindingUtil.inflate(layoutInflater, R.layout.list_item, viewGroup, false);
093gszye

093gszye1#

你应该做以下平滑滚动虽然..

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    CheckBinding binding;
    if(convertView == null) {
        binding = DataBindingUtil.inflate(
                LayoutInflater.from(getContext()),
                R.layout.check, parent, false);
        convertView = binding.getRoot();
    }
    else {
        binding = (CheckBinding) convertView.getTag();
    }

    binding.setCheck(this.getItem(position));
    convertView.setTag(binding);
    return convertView;
}
ryoqjall

ryoqjall2#

根据this,您应该返回binding.getRoot()

View getRoot ()

返回与绑定关联的布局文件中最外面的视图。如果此绑定用于合并布局文件,则将返回合并标记中的第一个根。

pengsaosao

pengsaosao3#

这里是Kotlin的变体:

val binding = convertView?.tag as? CheckBinding ?: CheckBinding.inflate(layoutInflater, parent, false)
    binding.check = this.getItem(position)
    binding.root.tag = binding

    return binding.root
1zmg4dgp

1zmg4dgp4#

推荐方式

使用生成的Binding类代替DataBindingUtil类。参见文档。
如果在FragmentListViewRecyclerView适配器中使用数据绑定项,则可能更喜欢使用绑定类的inflate()方法

使用

binding = CheckBinding.inflate(this, parent, false);

代替

binding = DataBindingUtil.inflate(
                LayoutInflater.from(getContext()),
                R.layout.check, parent, false);

其他代码将与@sergi相同

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    CheckBinding binding;
    if(convertView == null) {
        binding = CheckBinding.inflate(this, parent, false);
        convertView = binding.getRoot();
    }
    else {
        binding = (CheckBinding) convertView.getTag();
    }

    binding.setCheck(this.getItem(position));
    convertView.setTag(binding);
    return convertView;
}
qacovj5a

qacovj5a5#

ItemTickFilterBinding mFilterBinding;

    if (convertView==null) {
        mFilterBinding= DataBindingUtil.inflate(
                LayoutInflater.from(mContext),
                R.layout.item_tick_filter, parent, false);
        convertView.setTag(mFilterBinding);
    }
    else {
        mFilterBinding=(ItemTickFilterBinding)convertView.getTag();
    }
guz6ccqo

guz6ccqo6#

DataBindingUtil将ViewDataBinding对象保存到view.tag中,因此您可以稍后通过DataBindingUtil.getBinding(convertView)检索它

@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();
}
41zrol4v

41zrol4v7#

对于GridView适配器或自定义适配器,以下代码对我有效:

override fun getView(position: Int, convertView: View?, container: ViewGroup?): View {
    val binding = convertView?.tag as? MenuItemBinding ?:
    MenuItemBinding.inflate(LayoutInflater.from(ctx), container, false)

    val menuItem = menuList[position]
    binding.txtVw.text = menuItem.title
    binding.imgVwIcon.setImageResource(menuItem.iconId)
    return binding.root
}

MenuItemBinding基于布局文件的名称,此处布局文件的名称为**'menu_item.xml'**。

相关问题