当前位置: 首页 > 知识库问答 >
问题:

Android Realm RecyclerView动画不起作用?

陈瀚玥
2023-03-14

我有一个由< code > RealmRecyclerViewAdapter 填充的< code > recycle view ,但不知何故,当数据更改时,没有动画播放。adapter类为不同的布局使用多个< code > view holder ,但这不应该影响动画,对吗?

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);

将数据绑定到适配器的代码如下所述:

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

entryAdapter.updateData(sortedEntries);

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

注意:在我的应用程序版本中。格雷德尔锉刀

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

在 Project build.gradle 文件中:

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

共有1个答案

潘弘博
2023-03-14

代码示例中,不容易看出从哪里调用< code > entry adapter . update date(),但是在每次更新时连续调用此方法将禁用所有动画,因为它只调用< code > notifyDataSetChanged()。

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

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

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

 类似资料:
  • 问题内容: 我正在尝试使形状的动画随着样本大小的增加(从100增加到1000)而呈指数分布。还有一个滑块,用于配置发行版的lambda参数,以及一个用于启动动画的按钮。 我的问题是,即使布局得到渲染,当我按下开始按钮时也没有动画发生。到目前为止,这是我的尝试: 为了简化起见,我在Python笔记本中运行它: 问题答案: 您应该注意函数的名称空间。 考虑 因此,您需要处理实际对象,而不是它们的某些本

  • 问题内容: 我有一个按钮,该按钮调用animateWithDuration代码,以淡出图像,淡入文本和in的新bg颜色,然后将其重置为正常值。动画需要几秒钟才能完成,效果很好。 然而! 有一个问题: 有时在动画结束之前会再次按下此按钮。发生这种情况时,我希望当前的动画停止并重新开始。 研究解决方案不起作用 根据我的阅读,解决方案应该很简单,只需导入QuartzCore并添加: 这确实删除了动画,但

  • 该项目提供了两种动画变体。 动画选项1,触发器('imationOption1') 无投诉。 动画选项2,触发器('imationOption2') 转换在此处不起作用。 在线查看这个项目在StackBlitz.com app.component.html 应用程序组件.ts 谷歌搜索并没有导致解决方案。

  • 下面的函数只是将视图移动到一个新位置。它不显示动画:

  • 我是AutoLayout的新手。我已经知道很多关于这个autoLayout的问题和教程,但是我还没有找到我的解决方案。所以提前感谢您的任何帮助。 我的要求是什么? 我必须制作UIView,它将在按下屏幕底部带有动画的按钮后出现在屏幕上。(如键盘)。我使用autoLayout在xib文件中创建了此UIView。到目前为止,我已经这样做了。 在ViewDidLoad中: 在这个视图中,它包含(滚动视图

  • 问题内容: 我在正在翻译动画的UILabel上有一个轻击手势。动画期间,只要在标签上轻按,轻按手势就不会有响应。 这是我的代码: 手势代码: 有任何想法吗?谢谢 :) 问题答案: 使用Tapgesture后,您将无法完成自己的工作,原因有1个。手势与标签的框架相关联。开始动画时,标签的最后一帧立即更改,而您只是在观看假电影(动画)。如果您能够触摸屏幕上的(0,900),它将在发生动画时照常触发。不