[英]How to change the column span in grid layout on orientation change in Android Studio?
我有一个 2 列网格布局,但是当方向更改为横向时,我想将其更改为 3 列。 我该怎么做?
这是我的 OnCreate 方法,我在其中设置了 RecyclerView 和 Grid。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
birdList = new ArrayList<>();
getJSONfromFile();
// Recycler View
mainRecyclerView = findViewById(R.id.main_recycler_view);
mainRecyclerView.setLayoutManager(new GridLayoutManager(this, 2));
// Set Adapter
BirdAdapter adapter = new BirdAdapter(this, birdList);
mainRecyclerView.setAdapter(adapter);
}
尝试在您的 onCreateView 方法中处理此问题,因为每次发生方向更改时都会调用它:
if(getActivity().getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT){
mainRecyclerView.setLayoutManager(new GridLayoutManager(this, 2));
}
else{
mainRecyclerView.setLayoutManager(new GridLayoutManager(this, 3));
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.