使用GridLayoutManager水平滚动的Recycler视图不起作用

时间:2016-01-16 22:27:31

标签: android android-layout android-fragments

我有一个活动,其中包括一个片段。该片段包含Recycler View。 我想让它在网格中显示游戏数据。 我需要它向下滚动(游戏的每一轮都是一行)但也需要水平,因为我需要5到10列。 当id使用参数android:scrollbars =" vertical | horizo​​ntal"在回收站视图中,它只向下滚动并使其列非常小。即使我将其设置为水平也会发生这种情况。

它怎么看......

enter image description here

代码: 活动

<?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: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="com.sb.matt.doppelkopf.activities.GameActivity">

        <fragment
            android:id="@+id/fragment_game"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            class="com.sb.matt.doppelkopf.fragments.game_fragment"
            tools:layout="@layout/fragment_game">
        </fragment>
</RelativeLayout>

碎片

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.sb.matt.doppelkopf.fragments.game_fragment">


<android.support.v7.widget.RecyclerView
    android:id="@+id/recyclerView_gameData"
    android:scrollbars="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>
</RelativeLayout>

回收商视图中的项目视图

<?xml version="1.0" encoding="utf-8"?>


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="400dp"
android:layout_height="200dp">

<TextView
    android:layout_width="400dp"
    android:layout_height="200dp"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:text="Test"
    android:id="@+id/txt_inhalt"
    android:layout_centerVertical="true"
    android:layout_marginTop="5dp"
    android:layout_marginBottom="5dp"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"/>
</RelativeLayout>

片段代码

package com.sb.matt.doppelkopf.fragments;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.sb.matt.doppelkopf.R;
import com.sb.matt.doppelkopf.activities.GameActivity;
import com.sb.matt.doppelkopf.adapters.GameData_Adapter;
import com.sb.matt.doppelkopf.data.GameData;

/**
 * A simple {@link Fragment} subclass.
 */
public class game_fragment extends Fragment
{
private GameData gameData;

private RecyclerView rGridView;

GameData_Adapter adapter;


public game_fragment()
{
    // Required empty public constructor
}


@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
    // Inflate the layout for this fragment
    final View v = inflater.inflate(R.layout.fragment_game, container, false);

    gameData = ((GameActivity)getActivity()).getGameData();



    return v;
}


@Override
public void onActivityCreated(Bundle savedInstanceState)
{
    super.onActivityCreated(savedInstanceState);
    String[][] gameData = ((GameActivity) getActivity()).getGameData().getViewData();

    rGridView = (RecyclerView) getActivity().findViewById(R.id.recyclerView_gameData);
    rGridView.setLayoutManager(new GridLayoutManager(getActivity(), gameData[0].length));

    adapter = new GameData_Adapter(gameData);
    rGridView.setAdapter(adapter);
}
}

适配器代码,它使用二维数组来填充网格。

package com.sb.matt.doppelkopf.adapters;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.sb.matt.doppelkopf.R;
import com.sb.matt.doppelkopf.data.SingleGameDataItem;

import java.util.ArrayList;

/**
 * Created by matts on 16.01.2016.
 */
public class GameData_Adapter extends      RecyclerView.Adapter<GameData_Adapter.ViewHolder>
{
private String[][] gameData;
private ArrayList<SingleGameDataItem> list;

public static class ViewHolder extends RecyclerView.ViewHolder
{
    View MyView;
    TextView textView;
    public ViewHolder(View view)
    {
        super(view);
        MyView = view;
        textView = (TextView) view.findViewById(R.id.txt_inhalt);
    }
}


public GameData_Adapter (String[][] gameData)
{
    this.gameData = gameData;
    list = new ArrayList<SingleGameDataItem>();

    for(int i = 0; i < gameData.length; i++)
    {
        for(int j = 0; j < gameData[0].length; j++)
        {
            SingleGameDataItem item = new SingleGameDataItem(gameData[i][j]);
            list.add(item);
        }
    }
}


@Override
public GameData_Adapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType)
{
    // create a new view
    View v = LayoutInflater.from(parent.getContext())
            .inflate(R.layout.singlegamedataitem, parent, false);
    // set the view's size, margins, paddings and layout parameters

    ViewHolder vh = new ViewHolder(v);
    return vh;
}


// Replace the contents of a view (invoked by the layout manager)
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
    // - get element from your dataset at this position
    // - replace the contents of the view with that element
    holder.textView.setText(list.get(position).getInhalt());
}


@Override
public int getItemCount()
{
    return list.size();
}
}

我做错了什么? :/

1 个答案:

答案 0 :(得分:21)

您使用了错误的构造函数new GridLayoutManager(getActivity(), gameData[0].length)。 Javadoc说它&#34;创建垂直 GridLayoutManager&#34;。

试试new GridLayoutManager(getActivity(), 1, GridLayoutManager.HORIZONTAL, false) 代替。

此处1计为行数。

相关问题