【问题标题】:How to change the column span in grid layout on orientation change in Android Studio?如何在 Android Studio 中更改方向时更改网格布局中的列跨度?
【发布时间】:2019-12-03 13:34:57
【问题描述】:

我有一个 2 列网格布局,但我想在方向更改为横向时将其更改为 3 列。我该怎么做?

这是我设置 RecyclerView 和 Grid 的 OnCreate 方法。

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

【问题讨论】:

    标签: android android-gridlayout


    【解决方案1】:

    尝试在您的 onCreateView 方法中处理此问题,因为每次发生方向更改时都会调用它:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-08-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-10-05
      • 1970-01-01
      相关资源
      最近更新 更多