17

RecyclerViewの中に があり、HorizontalScrollViewを使用したいGridLayoutManager。これは問題ありませんが、まだ気になることが 1 つあります。すべての列の幅が同じです (最大のセル幅に基づいていると思いますか?)。この特定の列の最大セルに一致するように列の幅を折り返すことはできませんか?

次のようになります。

ここに画像の説明を入力

オレンジ色の部分は、セルのビューによって取得された部分です。


編集

私は、私が期待することを明確にするように求めました。例は言葉よりも優れています。ここでは、GridLayoutManager を使用した RecyclerView のスクリーンショットを確認できます。各アイテムは、10 ~ 40 文字のテキストをランダムに含むシンプルな TextView です。前に述べたように、RecyclerView は Horizo​​ntalScrollView 内にあります。この列には幅全体を満たすアイテムがないにもかかわらず、すべての列が同じ幅であることがわかります。私が望むのは、それらの無駄な空きスペースを削除し、各列がそれ自身の最大の子の幅に一致する異なるサイズの列を持つことです。

ここに画像の説明を入力

この動作をテストしたい場合は、私が Github にアップロードしたこのリポジトリを複製できます: 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>

編集2

一部のセルのスパン サイズが 1 を超えている可能性があり、LayoutManager を垂直にする必要があることを言及しておく必要がありました。これにより、これらのセルが垂直ではなく水平に配置されるようになります (自分が理解できるようになっているかどうかはわかりません)。

御時間ありがとうございます !

4

1 に答える 1

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

アダプタ クラス

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

行.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>

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>

編集 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>

このようにspanCountの数を設定します

スパン数 = 8;

grid.setLayoutManager(new GridLayoutManager(this, spanCount, LinearLayoutManager.HORIZONTAL, false));
于 2017-01-04T10:08:32.347 に答える