[英]RecyclerView and Data Binding not working
這是我第一次嘗試使用 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 在數據綁定布局中根本不起作用。 我只用我的房車創建了一個單獨的布局,它工作得很好。 一旦我將它包含在我的主要布局中,它就停止工作了。
不確定這是錯誤還是功能。
所以,我想,也許如果我把它做成一個自定義視圖,它可能會起作用,而且確實如此。 我的問題是我不知道如何將值傳遞到我的自定義視圖中。
我看了這里,但無法弄清楚他的意思。 這是我在自定義視圖中的代碼。
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 的消息。
在您的onCreateViewHolder
:
@Override
public BadgeBindingHolder onCreateViewHolder(ViewGroup parent, int viewType) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
ProfileBadgeRowBinding binding = DataBindingUtil.inflate(inflater, R.layout.profile_badge_row, parent, false);
BadgeBindingHolder holder = new BadgeBindingHolder(binding.getRoot(), binding);
return holder;
}
並在您的項目視圖中添加綁定
public class BadgeBindingHolder extends RecyclerView.ViewHolder{
private ProfileBadgeRowBinding binding;
public BadgeBindingHolder(View rowView, ProfileBadgeRowBinding binding) {
super(rowView);
this.binding = binding;
}
...
}
並在bindToViewHolder
設置變量
@Override
public void bindToViewHolder(RecyclerView.ViewHolder viewHolder) {
ProfileBadgeRowBinding binding = ((BadgeBindingHolder) viewHolder).binding;
binding.setVariable(BR.badge, badge);
binding.executePendingBindings(); // This line is important, it will force to load the variable in a custom view
}
如果要訪問視圖,請在xml
的視圖中添加 id。
<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:id="@+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
</layout>
然后您可以訪問您的bindToViewHolder
的 id
@Override
public void bindToViewHolder(RecyclerView.ViewHolder viewHolder) {
ProfileBadgeRowBinding binding = ((BadgeBindingHolder) viewHolder).binding;
binding.setVariable(BR.badge, badge);
binding.executePendingBindings();
binding.image.setImage(...);
}
更多關於自定義視圖https://developer.android.com/topic/libraries/data-binding/index.html#dynamic_variables
今天做起來要容易得多,請參考以下示例:
public class LanguageRecyclerAdapter extends RecyclerView.Adapter<LanguageRecyclerAdapter.LanguageViewHolder> {
private List<Language> languages;
public class LanguageViewHolder extends RecyclerView.ViewHolder {
final LanguageSetItemBinding item;
public void bind(final Language language) {
item.setLanguage(language.getName());
item.setFlag(language.getFlagRes());
item.getRoot().setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//do somthing if you need
}
});
item.executePendingBindings();
}
public LanguageViewHolder(LanguageSetItemBinding itemView) {
super(itemView.getRoot());
item =itemView;
}
}
public LanguageRecyclerAdapter(List<Language> languages) {
this.languages = languages;
}
public void onBindViewHolder(LanguageViewHolder holder, int position) {
Language item = languages.get(position);
holder.bind(item);
}
public LanguageViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext());
LanguageSetItemBinding item = LanguageSetItemBinding.inflate(layoutInflater, parent,false);
return new LanguageViewHolder(item);
}
@Override
public int getItemCount() {
return languages.size();
}
@Override
public int getItemViewType(int position) {
return super.getItemViewType(position);
}
}
LanguageSetItemBinding
xml 看起來像這樣,它包含在TextView
和ImageView
:
<?xml version="1.0" encoding="utf-8"?>
<layout >
<data>
<variable name="language" type="String" />
<variable name="flag" type="int" />
</data>
<android.widget.LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="166dp"
android:layout_height="166dp"
android:background="@color/colorWhite"
android:orientation="vertical"
android:layout_margin="20dp"
android:gravity="center">
<TextView
android:layout_marginTop="20dp"
android:gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:text="@{language}"
android:textColor="@color/colorBlack"
android:textAlignment="center"/>
<ImageView
android:textColor="?attr/errorColor"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:src="@{context.getDrawable(flag)}"
android:gravity="center"
android:paddingTop="3dp"/>
</android.widget.LinearLayout>
</layout>
我有一個內部帶有 recyclerview 的活動,在活動 OmCreateView 中,我將此回收器附加到我上面創建的適配器上:
final ArrayList<Language> lang = new ArrayList(languageList.values());
final LanguageRecyclerAdapter adapter = new LanguageRecyclerAdapter(lang);
mBinding.languageRecyclerView.setAdapter(adapter);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.