【发布时间】:2017-05-25 18:45:06
【问题描述】:
当我将视图旋转到横向时,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();
}
}
祝酒词表明当我翻转方向时代码运行。感谢您的帮助!
【问题讨论】:
标签: android android-recyclerview gridlayoutmanager