假设当我导航到上一页时我在recyclerview中选择了位置3并再来一次我想要选择recyclerview的第一项
int selectposition = 0;
if (selectedPosition == position ) {
chckbox.setVisibility(View.VISIBLE);
chckbox.setChecked(true);
chckbox.setBackgroundResource(R.drawable.checkbox_selector);
chckbox.forceLayout();
layout.setBackgroundResource(R.drawable.selected_address_bg);
} else {
chckbox.setVisibility(View.VISIBLE);
chckbox.setChecked(false);
layout.setBackgroundResource(R.color.white);
}
答案 0 :(得分:1)
在onBind方法中,选择第一个位置,以便每当您返回另一个活动并再次返回时,将选择第一个项目。
if(position == 0){
chckbox.setChecked(true);
chckbox.setBackgroundResource(R.drawable.checkbox_selector);
}
答案 1 :(得分:1)
2天后,我得到了最终解决方案如下:
// Make your Adapter...
mLeftAdapter = new LeftMenuAdapter(mContext, mFilterValuesArray);
// Bind it to RecyclerView
mFilterBinding.filterLeftRecyclerView.setAdapter(mLeftAdapter);
//Call this method which will perform click for 0'th position after computing (binding) all data...
postAndNotifyAdapter(new Handler(), mFilterBinding.filterLeftRecyclerView);
postAndnotifyAdapter 方法将如下:
protected void postAndNotifyAdapter(final Handler handler, final RecyclerView recyclerView) {
handler.post(new Runnable() {
@Override
public void run() {
if (!recyclerView.isComputingLayout()) {
// This will call first item by calling "performClick()" of view.
((LeftMenuAdapter.ViewHolder) recyclerView.findViewHolderForLayoutPosition(0)).mView.performClick();
} else {
postAndNotifyAdapter(handler, recyclerView, adapter);
}
}
});
}
愿这对你有所帮助。 :)
答案 2 :(得分:0)
您可以通过以下代码滚动到第一个位置: 您可以使用布局管理器替换
LinearLayoutManager layoutManager = (LinearLayoutManager) mRecyclerView
.getLayoutManager();
layoutManager.scrollToPositionWithOffset(0, 0);