我正在RecyclerView
结合LayoutManager
. LinearLayoutManager
我的目标是让LayoutManager
设备在纵向时水平绘制我的项目,而在设备处于横向时垂直绘制它们。为此,我在我的活动中使用以下代码onCreate
:
RecyclerView recyclerView = (RecyclerView) findViewById(R.id.nextColors);
recyclerView.setAdapter(myAdapter = new MyAdapter(getBaseContext(), myData, myLayout);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
linearLayoutManager.setOrientation(LinearLayout.HORIZONTAL);
} else {
linearLayoutManager.setOrientation(LinearLayout.VERTICAL);
}
recyclerView.setLayoutManager(linearLayoutManager);
recyclerView.setItemAnimator(new DefaultItemAnimator());
如果我在设备处于横向或纵向模式时启动活动,这很好用。但是,当我在此活动中更改方向时,项目始终水平或垂直绘制,具体取决于活动首次启动时设备的方向。这对我来说没有意义,因为在设备旋转后重新创建了活动并且LayoutManager
应该采用正确的方向。
有谁知道如何setOrientation
使用设备方向更改?