简体   繁体   English

如何更改 RecyclerView 隐藏项的可见性?

[英]How to change visibility of hidden items of RecyclerView?

I'm developing chat app.我正在开发聊天应用程序。 It has a feature that is reply action (like Telegram/WhatsApp).它具有回复操作(如 Telegram/WhatsApp)的功能。 When user click replied message then a RecyclerView should scroll to declared position.当用户点击回复消息时,RecyclerView 应该滚动到声明的 position。 But if I set position of hidded item of recyclerview, app throw a NullPoinerException.但是,如果我设置了 recyclerview 的隐藏项的 position,应用程序会抛出 NullPoinerException。 It is working for only visible items.它仅适用于可见项目。

        holder.replyContainer.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                mRecyclerView.smoothScrollToPosition(m.content.getReply().getPosition());
                    View view = mRecyclerView.
                            findViewHolderForAdapterPosition(m.content.getReply().getPosition())
                            .itemView.findViewById(R.id.selected);
                    view.setVisibility(View.VISIBLE);
                    view.startAnimation(AnimationUtils.loadAnimation(mActivity, R.anim.fade_out));
            }
        });

I have fixed it, the invisible items of methods are must be call after scrolling finished.我已经修复它,方法的不可见项必须在滚动完成后调用。 So, I added listener to RecyclerView:所以,我在 RecyclerView 中添加了监听器:

RecyclerView.OnScrollListener listener = new RecyclerView.OnScrollListener() {
  @Override
  public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newState) {
                                   super.onScrollStateChanged(recyclerView, newState);
      if (newState == RecyclerView.SCROLL_STATE_IDLE) {
          RecyclerView.ViewHolder holder = (RecyclerView.ViewHolder)
                mRecyclerView.findViewHolderForAdapterPosition(replyPosition);
                      if(holder != null) {
                           View view = holder.itemView.findViewById(R.id.selected);
                           view.setVisibility(View.VISIBLE);
                           view.startAnimation(AnimationUtils.loadAnimation(mActivity,
                                                   R.anim.fade_out));

                                       }

                                   }
                               }

  @Override
  public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
        super.onScrolled(recyclerView, dx, dy);
  }
                           };
  mRecyclerView.addOnScrollListener(listener);
  mRecyclerView.smoothScrollToPosition(replyPosition);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Android-如何更改RecyclerView的小部件的可见性并将其显示在recyclerView的所有项目中? - Android - How to change the visibility of a widget of and show it in all items in recyclerView? 更改RecyclerView所有项目的View可见性 - Change the View visibility of all items of RecyclerView 在Android中长按一个项目时,如何更改所有RecyclerView项目的可见性? - How to change the visibility of all RecyclerView items when a single item is long clicked in Android? 将EditText可见性更改为隐藏 - Change EditText Visibility To Hidden 如何一次更改JFrame中所有项目的可见性? - How to change visibility of ALL items in a JFrame at once? RecyclerView项目不会一次全部更改可见性 - RecyclerView items not changing visibility all at once 如何更改RecyclerView中所有项目的布局? - How to change layout for all items in RecyclerView? android:-如何更改recyclerview列表项的语言? - android:- How to change the language of list items of recyclerview? 如何从WebInternface更改导航抽屉菜单项的可见性 - How to change navigation drawer menu items visibility from WebInternface 从RecyclerView中的某些行更改ImageView可见性 - Change ImageView visibility from certain rows in RecyclerView
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM