【发布时间】:2023-04-07 23:32:01
【问题描述】:
这是我第一次尝试与 RecyclerView 进行数据绑定,但不是我第一次使用 RecyclerView 本身。
由于某种原因,没有调用任何适配器方法 - 甚至没有调用 getItemCount()。 这可能是我的 RecyclerView 的一个愚蠢问题,与数据绑定完全无关,但我看不出有什么问题。
View rootview = inflater.inflate(R.layout.fragment_profile_first, container, false);
// Initialize recycler view
RecyclerView badgesRV = (RecyclerView) rootview.findViewById(R.id.badgesRV);
LinearLayoutManager llm = new LinearLayoutManager(getActivity());
llm.setOrientation(LinearLayoutManager.HORIZONTAL);
badgesRV.setLayoutManager(llm);
BadgeAdapter badgeAdapter = new BadgeAdapter(profileObject.badgesEntity.badges);
badgesRV.setAdapter(badgeAdapter);
适配器:
public class BadgeAdapter extends RecyclerView.Adapter<BadgeAdapter.BadgeBindingHolder>{
private static final int MAX_BADGES_TO_DISPLAY = 5;
private BadgeObject[] badges;
public BadgeAdapter(BadgeObject[] badges){
this.badges = badges;
}
@Override
public BadgeBindingHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext())
.inflate(R.layout.profile_badge_row, parent, false);
BadgeBindingHolder holder = new BadgeBindingHolder(v);
return holder;
}
@Override
public void onBindViewHolder(BadgeBindingHolder holder, int position) {
final BadgeObject badgeObject = badges[position];
holder.getBinding().setVariable(BR.badge, badgeObject);
holder.getBinding().executePendingBindings();
}
@Override
public int getItemCount() {
Log.d(TAG, "item count = " + Math.min(MAX_BADGES_TO_DISPLAY, badges.length));
return Math.min(MAX_BADGES_TO_DISPLAY, badges.length);
}
public class BadgeBindingHolder extends RecyclerView.ViewHolder{
private ViewDataBinding binding;
public BadgeBindingHolder(View rowView) {
super(rowView);
binding = DataBindingUtil.bind(rowView);
}
public ViewDataBinding getBinding() {
return binding;
}
}
}
profile_badge_row.xml
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<data>
<variable name="badge" type="parseJsonEntities.requestObjects.BadgeObject"/>
</data>
<LinearLayout
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@{badge.badgeImage}"/>
</LinearLayout>
我查过了,那里肯定有数据。 我错过了什么?
======================
更新:
据我所知,RecyclerView 在数据绑定布局中根本不起作用。 我只用我的 RV 创建了一个单独的布局,它运行良好。 一旦我将它包含在我的主要布局中,它就停止工作了。
不确定这是错误还是功能。
所以,我想如果我把它做成一个自定义视图,也许它会起作用,而且确实如此。 我的问题是我不知道如何将值传递到我的自定义视图中。
我看了here,但不明白他到底是什么意思。 这是我在自定义视图中的代码。
LayoutInflater inflater = (LayoutInflater)
context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.profile_badges_layout, this, true);
ProfileBadgesLayoutBinding binding = ProfileBadgesLayoutBinding.inflate(inflater);
RecyclerView badgesRV = (RecyclerView) view.findViewById(R.id.badgesRV);
LinearLayoutManager llm = new LinearLayoutManager(context);
llm.setOrientation(LinearLayoutManager.HORIZONTAL);
badgesRV.setLayoutManager(llm);
BadgeAdapter badgeAdapter = new BadgeAdapter(null);
badgesRV.setAdapter(badgeAdapter);
这会给出一条消息,即找不到 ProfileBadgesLayoutBinding。
【问题讨论】:
-
你的片段 xml 在哪里??
-
RecyclerView与DataBinding完美配合,因为许多其他人都在使用它而没有问题。您可以在RecyclerView所在的位置发布您的布局吗?其他滚动视图可能有一些奇怪的行为。 -
另外,
badge.badgeImage是什么?一个Resource id,一个Drawable..?
标签: android android-custom-view android-recyclerview android-databinding