Android 如何在recyclerview中选择第一项

Android 如何在recyclerview中选择第一项,android,Android,假设我在浏览到上一页时选择了recyclerview中的位置3,然后再次访问,我希望选择recyclerview的第一项 int selectposition = 0; if (selectedPosition == position ) { chckbox.setVisibility(View.VISIBLE); chckbox.setChecked(true); chckbox.setBackgroundRe

假设我在浏览到上一页时选择了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);
        }

您可以通过以下代码滚动到第一个位置: 可以使用布局管理器替换

LinearLayoutManager layoutManager = (LinearLayoutManager) mRecyclerView
                    .getLayoutManager();
            layoutManager.scrollToPositionWithOffset(0, 0);

在onBind方法中,选择第一个位置,以便每次返回到另一个活动并再次返回时,都会选择第一个项目

if(position == 0){
chckbox.setChecked(true);
chckbox.setBackgroundResource(R.drawable.checkbox_selector);
}

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

希望这对你有帮助。:)

在“活动”上加载传递位置索引