gpt4 book ai didi

java - Android Realm RecyclerView 动画不工作?

转载 作者:行者123 更新时间:2023-11-29 02:41:03 24 4
gpt4 key购买 nike

我有一个 RecyclerView,它由 RealmRecyclerViewAdapter 填充,但不知何故,当数据更改时没有播放动画。适配器类使用多个 ViewHolders 来实现不同的布局,但这应该不会影响动画吧?

public class DiaryPageEntryAdapter extends RealmRecyclerViewAdapter<Entry, RecyclerView.ViewHolder> {

static class MealEntryViewHolder extends RecyclerView.ViewHolder {
@BindView(R.id.item_diary_entry_drink_title) TextView tvMealEntryTitle;
@BindView(R.id.item_diary_entry_meal_time) TextView tvMealEntryTime;
BindView(R.id.item_diary_entry_meal_bullet_list) RecyclerView rvBulletList;

MealEntryBulletAdapter bulletAdapter;

public MealEntryViewHolder(View itemView) {
super(itemView);
ButterKnife.bind(this, itemView);
this.bulletAdapter = new MealEntryBulletAdapter();
rvBulletList.setLayoutManager(new LinearLayoutManager(itemView.getContext()));
rvBulletList.setAdapter(this.bulletAdapter);
}

void bindData(MealEntry mealEntry) {
tvMealEntryTitle.setText(mealEntry.getTitle());
tvMealEntryTime.setText(DateTimeUtils.timeValueToText(itemView.getContext(), mealEntry.getTime()));
this.bulletAdapter.updateData(mealEntry.getConsumedMeals() ,mealEntry.getConsumedDrinks());
}
}

// Other ViewHolders

public DiaryPageEntryAdapter(@Nullable OrderedRealmCollection<Entry> data, boolean autoUpdate) {
super(data, autoUpdate, true);
}

@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
switch (viewType) {
case MEAL_ENTRY:
// Inflate meal entry layout and then create a new meal view holder with it
View rowMeal = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_diary_entry_meal, parent, false);
return new MealEntryViewHolder(rowMeal);
// Other case options.
}

@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
Entry entry = getItem(position);

if(Entry.isMealEntry(entry)) {
MealEntryViewHolder mealHolder = (MealEntryViewHolder) holder;
mealHolder.bindData(Entry.getMealEntryFromEntry(entry));
}
// Other if branches.
}

@Override
public int getItemViewType(int position) {
Entry entry = getItem(position);

if(Entry.isMealEntry(entry)) return MEAL_ENTRY;
// Other if branches.
}

设置 RecyclerView 和适配器的代码如下所示:

        RecyclerView recyclerView = (RecyclerView) view.findViewById(R.id.frag_diary_page_rv_entries);
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));

entryAdapter = new DiaryPageEntryAdapter(null, true);
recyclerView.setAdapter(entryAdapter);

将数据绑定(bind)到适配器的代码如下:

RealmResults<Entry> sortedEntries = diaryEntry.getEntries()
.where()
.findAllSortedAsync("time");

entryAdapter.updateData(sortedEntries);

自动更新工作正常,但不知何故,数据更改时没有动画。一个新条目只是出现但没有动画。此外,我希望在第一次填充 RecyclerView 时播放一个简短的动画 - 类似于 [ http://anthony-skr.com/article/recyclerview-items-animation-with-rebound-effect][1] 的入口动画。 .

注意:在我的应用程序的 build.gradle 文件中

dependencies {
// Other dependencies
compile 'io.realm:android-adapters:2.1.0'
}

在项目的build.gradle文件中:

dependencies {
classpath 'com.android.tools.build:gradle:2.3.2'
classpath "io.realm:realm-gradle-plugin:3.1.4"
}

最佳答案

从您的代码示例中,不容易看出您从哪里调用 entryAdapter.updateDate(),但是在每次更新时连续调用此方法将禁用所有动画,因为它只是调用 notifyDataSetChanged()

一个更标准的模式应该是这样的:

RealmResults<Entry> sortedEntries = diaryEntry.getEntries()
.where()
.findAllSortedAsync("time");
entryAdapter = new DiaryPageEntryAdapter(sortedEntries, true);
recyclerView.setAdapter(entryAdapter);

如果您执行上述操作,动画应该工作。

关于java - Android Realm RecyclerView 动画不工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44105724/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com