gpt4 book ai didi

java - 为什么 RecyclerView.OnScrollListener() 只影响 recyclerview 的最后一项

转载 作者:行者123 更新时间:2023-12-02 03:09:43 25 4
gpt4 key购买 nike

我想在我的应用程序中拥有此功能

enter image description here

意味着当RecyclerView滚动时,显示时间的TextView应该出现并在一小段时间后消失,我的努力是这样的,目标TextView具有Visiblity.GONE并且适配器看起来像

public class ChatMessageAdapter extends RecyclerView.Adapter<ViewHolder> {

private List<ChatMessage> mMessageList;
private Context mContext;
private boolean isMe = false;

private ThemMessageHolder messageHolder;

public ChatMessageAdapter(Context mContext, List<ChatMessage> mMessageList){
this.mContext = mContext;
this.mMessageList = mMessageList;
}

@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View rootView = LayoutInflater.from(mContext).inflate( R.layout.chat_them_container, parent, false );
return new ThemMessageHolder(rootView);
}

@Override
public void onBindViewHolder(ViewHolder holder, int position) {
messageHolder = (ThemMessageHolder) holder;
messageHolder.mMessageTextView.setText(mMessageList.get(position).getMessage());
messageHolder.mSentAtTextView.setText(mMessageList.get(position).getSentAt());
}

@Override
public int getItemCount() {
return mMessageList.size();
}

public void showAndHideDate(){
messageHolder.mSentAtTextView.setVisibility(View.VISIBLE);
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
messageHolder.mSentAtTextView.setVisibility(View.GONE);
}
},3500);

}

}

在 MainActivity 中我只是

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

mMessageList = new CopyOnWriteArrayList<>();

mChatMessagesRecyclerView = (RecyclerView) findViewById(R.id.chat_msg_rv);
mChatMessagesRecyclerView.setLayoutManager(new LinearLayoutManager(MainActivity.this));
mChatMessageAdapter = new ChatMessageAdapter(MainActivity.this, mMessageList);
mChatMessagesRecyclerView.setAdapter(mChatMessageAdapter);

mChatMessagesRecyclerView.setOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
super.onScrollStateChanged(recyclerView, newState);
mChatMessageAdapter.showAndHideDate(); // Here I'm calling the method defined in Adapter class
}

@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
}
});
}

通过上面的代码,我仅在 RecyclerView 的最后一项上获得了所需的效果,如图所示

enter image description here

如何对 RecyclerView 的所有项目应用所需的效果?

最佳答案

问题是您在适配器中仅使用了一个 ThemMessageHolder 变量。这意味着,每次调用 onBindViewHolder() 都会覆盖该变量。然后,messageHolder将指向最后创建的持有者,并且showAndHideDate()将仅与它一起使用。

解决方案可以找到所有可见的持有者(伪代码):

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

if (initChatBarUI()){
mSendImageButton.setOnClickListener(MainActivity.this);
mEmoImageButton.setOnClickListener(MainActivity.this);
}

mMessageList = new CopyOnWriteArrayList<>();

final LinearLayoutManager layoutManager = new LinearLayoutManager(MainActivity.this);
mChatMessagesRecyclerView = (RecyclerView) findViewById(R.id.chat_msg_rv);
mChatMessagesRecyclerView.setLayoutManager(layoutManager);
mChatMessageAdapter = new ChatMessageAdapter(MainActivity.this, mMessageList);
mChatMessagesRecyclerView.setAdapter(mChatMessageAdapter);

mChatMessagesRecyclerView.setOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
super.onScrollStateChanged(recyclerView, newState);
final int firstVisibleItemPosition = layoutManager.findFirstVisibleItemPosition();
final int lastVisibleItemPosition = layoutManager.findLastVisibleItemPosition();
mChatMessageAdapter.showAndHideDate(mChatMessagesRecyclerView, firstVisibleItemPosition, lastVisibleItemPosition); // Here I'm calling the method defined in Adapter class
}

@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
}
});
}

和 showAndHideDate() 可能看起来像

public void showAndHideDate(RecyclerView mRecyclerView, int firstVisibleItemPosition, int lastVisibleItemPosition){

for (int i = firstVisibleItemPosition; i <= lastVisibleItemPosition; ++i) {
final ThemMessageHolder holder = (ThemMessageHolder) mRecyclerView.findViewHolderForAdapterPosition(i);
holder.mSentAtTextView.setVisibility(View.VISIBLE);
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
holder.mSentAtTextView.setVisibility(View.GONE);
}
},3500);
}

}

关于java - 为什么 RecyclerView.OnScrollListener() 只影响 recyclerview 的最后一项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41259178/

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