gpt4 book ai didi

android - 手机旋转时,RecyclerView 不会从 2 列变为 3 列

转载 作者:行者123 更新时间:2023-11-29 16:57:52 25 4
gpt4 key购买 nike

当我将 View 旋转为横向时,RecyclerView 不会从两列切换到三列。我试过这段代码:

mGLManager.setSpanCount(3);

mGLManager = (GridLayoutManager)mRecyclerView.getLayoutManager();
mGLManager.setSpanCount(2);
mRecyclerView.setLayoutManager(mGLManager);

mRecyclerView.setLayoutManager( new GridLayoutManager(MainActivity.this, 3) );

我也试过这些行让它刷新:

mRecyclerView.refreshDrawableState();

mAdapter.notifyDataSetChanged();

mRecyclerView.invalidate();

mRecyclerView.requestLayout();

mRecyclerView.swapAdapter(mRecyclerView.getAdapter(), true);
mRecyclerView.scrollBy(0,0);

这是我当前的代码。

public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);

// set the number of columns based on the orientation
if( newConfig.orientation == Configuration.ORIENTATION_PORTRAIT ) {
mRecyclerView.setLayoutManager( new GridLayoutManager(MainActivity.this, 2));
mRecyclerView.invalidate();
Toast.makeText(this, "portrait", Toast.LENGTH_SHORT).show();
} else if( newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE ) {
mRecyclerView.setLayoutManager( new GridLayoutManager(MainActivity.this, 3));
mRecyclerView.invalidate();
Toast.makeText(this, "landscape", Toast.LENGTH_SHORT).show();
}
}

toast 表明代码在我翻转方向时运行。感谢您的帮助!

最佳答案

在 Activity 的 onCreateView 方法中添加它,每次方向改变时都会调用它

if(getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT){
mRecycler.setLayoutManager(new GridLayoutManager(MainActivity.this, 2));
}
else{
mRecycler.setLayoutManager(new GridLayoutManager(MainActivity.this, 3));
}

关于android - 手机旋转时,RecyclerView 不会从 2 列变为 3 列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44187734/

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