gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 09:59:48 25 4
gpt4 key购买 nike

我正在开发聊天应用程序。它具有回复操作功能(如 Telegram/WhatsApp)。当用户点击回复消息时,RecyclerView 应该滚动到声明的位置。但是,如果我设置 recyclerview 的隐藏项的位置,应用程序会抛出 NullPoinerException。它仅适用于可见项目。

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

最佳答案

我已经修复了,不可见的方法项必须在滚动完成后调用。所以,我向 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);

关于java - 如何更改 RecyclerView 隐藏项的可见性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58592012/

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