GridLayoutManager - 列宽包装自己最大的子节点

MHo*_*gge 12 android gridlayoutmanager android-recyclerview android-wrap-content

我有一个RecyclerView内部HorizontalScrollView,我希望它使用一个GridLayoutManager.这没关系,但有一件事还是困扰我,每列的宽度是相同的(根据我想的最大单元格宽度?).是否可以包装列的宽度以匹配此特定列的最大单元格?

它应该看起来像这样:

在此输入图像描述

橙色部分是细胞视图所占据的部分.


编辑

我们让我澄清我的期望.一个例子比单词更好,在这里你可以看到一个带有GridLayoutManager的RecyclerView的截图.每个项目都是一个简单的TextView,随机包含10到40个字符之间的文本.如前所述,RecyclerView位于Horizo​​ntalScrollView内.我们可以看到每个列都具有相同的宽度,尽管此列中的任何项都不能满足整个宽度.我想要的是删除那些无用的空白空间和具有不同大小的列,每列与其自己的最大子项的宽度匹配.

在此输入图像描述

如果你想测试这个行为,你可以克隆我在Github上传的这个repo:https://github.com/ShargotthDev/TestGrid

如上所述,这是我的XML布局(非常基本):

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <HorizontalScrollView
        android:id="@+id/gameplay_hotizontalScroll_ScrollView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="70dp">

        <android.support.v7.widget.RecyclerView
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:id="@+id/recycler_view" />

    </HorizontalScrollView>

</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)

编辑2

我应该提到一些单元格的跨度大小可能超过1,而LayoutManager应该是垂直的,这样那些单元格可以在水平而不是垂直方向上占据更多位置(不知道我是否让自己理解).

谢谢你的时间 !

Sur*_*mar 8

您不需要将RecyclerView放在Horizo​​ntalScrollView中.请参阅下面的代码.

public class MainActivity extends AppCompatActivity {

    String[] list = new String[]{"Some text goes here", "Some small", "text", "goes here", "Some", "very large text", "goes here",
            "Some text goes here", "Some small", "text", "goes here", "Some", "very large text", "goes here"};
    RecyclerView grid;
    GridAdapter adapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        grid = (RecyclerView)findViewById(R.id.grid);
        grid.setLayoutManager(new GridLayoutManager(this, 2, LinearLayoutManager.HORIZONTAL, false));
        grid.setHasFixedSize(true);
        adapter = new GridAdapter(list);
        grid.setAdapter(adapter);
    }
}
Run Code Online (Sandbox Code Playgroud)

适配器类

public class GridAdapter extends RecyclerView.Adapter<GridAdapter.ViewHolder>{
    String[] mList;
    public GridAdapter(String[] list) {
        mList = list;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.row, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        holder.bind(mList[position]);
    }

    @Override
    public int getItemCount() {
        return mList.length;
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        TextView textView;
        public ViewHolder(View itemView) {
            super(itemView);
            textView = (TextView)itemView.findViewById(R.id.text);
        }

        public void bind(String s) {
            textView.setText(s);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

row.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:padding="10dp">
    <TextView android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

activity_main.xml中

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="cab.suresh.gridlayoutexample.MainActivity">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/grid"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)

编辑 将RecyclerView放在NestedScrollView中,如下所示

<android.support.v4.widget.NestedScrollView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:scrollbars="none">
    <android.support.v7.widget.RecyclerView
        android:id="@+id/grid"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>
</android.support.v4.widget.NestedScrollView>
Run Code Online (Sandbox Code Playgroud)

并设置你的spanCount数量

spanCount = 8;

grid.setLayoutManager(new GridLayoutManager(this, spanCount, LinearLayoutManager.HORIZONTAL, false));
Run Code Online (Sandbox Code Playgroud)