使用GridLayoutManager和RecyclerView更改列数

fap*_*pps 55 android android-orientation android-gridlayout android-recyclerview

在我的片段中,我按以下方式设置GridLayout: mRecycler.setLayoutManager(new GridLayoutManager(rootView.getContext(), 2));

所以,我只是想改变24,当用户旋转手机/平板电脑.我已经读过了onConfigurationChanged,我试图让它适用于我的情况,但它没有以正确的方式进行.当我旋转手机时,应用程序崩溃了......

你能告诉我如何解决这个问题吗?

以下是我找到解决方案的方法,该方法无法正常工作:

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

        int orientation = newConfig.orientation;

        if (orientation == Configuration.ORIENTATION_PORTRAIT) {
            mRecycler.setLayoutManager(new GridLayoutManager(mContext, 2));
        } else if (orientation == Configuration.ORIENTATION_LANDSCAPE) {
            mRecycler.setLayoutManager(new GridLayoutManager(mContext, 4));
        }
    }
Run Code Online (Sandbox Code Playgroud)

提前致谢!

TWi*_*Rob 77

如果您有多个条件或在多个地方使用该值,这可能会非常快.我建议创建以下结构:

res
  - values
    - dimens.xml
  - values-land
    - dimens.xml
Run Code Online (Sandbox Code Playgroud)

res/values/dimens.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <integer name="gallery_columns">2</integer>
</resources>
Run Code Online (Sandbox Code Playgroud)

res/values-land/dimens.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <integer name="gallery_columns">4</integer>
</resources>
Run Code Online (Sandbox Code Playgroud)

然后代码变成(并永远保持)像这样:

final int columns = getResources().getInteger(R.integer.gallery_columns);
mRecycler.setLayoutManager(new GridLayoutManager(mContext, columns));
Run Code Online (Sandbox Code Playgroud)

您可以轻松地看到添加确定列数的新方法是多么容易,例如使用-w500dp/ -w600dp/ -w700dp资源文件夹而不是-land.

将这些文件夹分组到单独的资源文件夹中也很容易,以防您不想混淆其他(更相关)的资源:

android {
    sourceSets.main.res.srcDir 'src/main/res-overrides' // add alongside src/main/res
}
Run Code Online (Sandbox Code Playgroud)


Sob*_*per 68

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

if(getActivity().getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT){
     mRecycler.setLayoutManager(new GridLayoutManager(mContext, 2));
}
else{
     mRecycler.setLayoutManager(new GridLayoutManager(mContext, 4));
}
Run Code Online (Sandbox Code Playgroud)

  • @ciccioska我认为[资源文件夹](http://developer.android.com/guide/practices/screens_support.html)比在`getConfiguration()`或`onConfigurationChanged`上做条件更好.另请参阅我对这个问题的回答. (4认同)
  • 这是完成此任务的正确行为吗?谢谢 (2认同)

kat*_*hri 7

回收视图支持AutofitRecycleView.

您需要添加android:numColumns="auto_fit"xml文件.

您可以参考此AutofitRecycleViewLink


Row*_*ezi 7

一种更健壮的方法来确定否.列将根据屏幕宽度和运行时计算它.我通常使用以下功能.

public static int calculateNoOfColumns(Context context) {
    DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics();
    float dpWidth = displayMetrics.widthPixels / displayMetrics.density;
    int scalingFactor = 200; // You can vary the value held by the scalingFactor
    // variable. The smaller it is the more no. of columns you can display, and the
    // larger the value the less no. of columns will be calculated. It is the scaling
    // factor to tweak to your needs.
    int columnCount = (int) (dpWidth / scalingFactor);
    return (columnCount>=2?columnCount:2); // if column no. is less than 2, we still display 2 columns
}
Run Code Online (Sandbox Code Playgroud)

这是一种更加动态的方法来准确计算出.列.对于不同屏幕尺寸的用户而言,这将更具适应性,而不会仅限于两个可能的值.

注意:您可以改变scalingFactor变量所持有的值.它越小越不.您可以显示的列数,值越大,否则越少.列将被计算.它是根据您的需求调整的缩放因子.


Sha*_*hmi 5

除了答案。也可以仅使用 XML 属性来完成。下面是代码。

<androidx.recyclerview.widget.RecyclerView
        android:id="@+id/pax_seat_map_rv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:spanCount="3"
        android:orientation="vertical"
        app:layoutManager="androidx.recyclerview.widget.GridLayoutManager" />
Run Code Online (Sandbox Code Playgroud)