jlh*_*ora 15 android android-databinding
我正在尝试将Android的数据绑定功能与自定义适配器和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?
}
}
Run Code Online (Sandbox Code Playgroud)
所以我的问题是:
View我应该返回的元素?或者换句话说,如何将对象绑定到膨胀/转换视图?convertView使用数据绑定时如何重用?以下是本指南中ListViews的唯一参考:
如果您在ListView或RecyclerView适配器中使用数据绑定项,您可能更喜欢使用:
Run Code Online (Sandbox Code Playgroud)ListItemBinding binding = ListItemBinding.inflate(layoutInflater, viewGroup, false); //or ListItemBinding binding = DataBindingUtil.inflate(layoutInflater, R.layout.list_item, viewGroup, false);
ser*_*rgi 22
您应该执行以下操作以平滑滚动..
@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;
}
Run Code Online (Sandbox Code Playgroud)
cse*_*nga 10
根据这个,你应该返回binding.getRoot().
View getRoot ()
Run Code Online (Sandbox Code Playgroud)
返回与Binding关联的布局文件中最外面的View.如果此绑定用于合并布局文件,则将返回合并标记中的第一个根.
这里是 kotlin 变体:
val binding = convertView?.tag as? CheckBinding ?: CheckBinding.inflate(layoutInflater, parent, false)
binding.check = this.getItem(position)
binding.root.tag = binding
return binding.root
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
8334 次 |
| 最近记录: |