Nó được tán thành bởi chủ nghĩa thuần túy để sử dụng phương pháp
recyclerView.findViewHolderForAdapterPosition(pos).itemView.performClick();
vì nó khẳng định trong tài liệu:
Return the ViewHolder for the item in the given position of the data set. Unlike
* {@link #findViewHolderForLayoutPosition(int)} this method takes into account any pending
* adapter changes that may not be reflected to the layout yet. On the other hand, if
* {@link Adapter#notifyDataSetChanged()} has been called but the new layout has not been
* calculated yet, this method will return <code>null</code> since the new positions of views
* are unknown until the layout is calculated.
cho chính xác lý do mà trì hoãn với một sợi đôi khi hữu ích. Bất kỳ cuộc gọi nào để thông báoDataSetChanged() đều làm cho nó trả về null. Tốt hơn để sử dụng phương pháp bộ điều hợp onBindViewHolder và kiểm tra vị trí 0. Điều chỉnh cách bạn đã thiết lập trình nghe, ví dụ usual way ở đây, được tạo trong hàm tạo ViewHolder. Sử dụng một biến init để ngăn chặn nó được gọi là một lần nữa:
@Override
public void onBindViewHolder(FooAdapter.ViewHolder viewHolder, int position) {
//code
...
//do this last especially if you are using an animator
if(position == 0 && isInit) {
listener.onItemClick(viewHolder.itemView,0);
isInit =false;
}
}
Nguồn
2017-08-17 20:46:41
Tại sao bạn đề nghị để sử dụng xử lý ở đây? – Gaket
Để trì hoãn 1 ms trên sự kiện nhấp chuột vì đôi khi nó đã đưa ra một ngoại lệ con trỏ null. – Saurabh
Cậu bé này đúng ... giải quyết ngoại lệ con trỏ null –