Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Recycler View with GridLayoutManager horizontal Scrolling doesnt work

i have an activity, which includes a fragment. This fragment includes a Recycler View. I want it to display data of a game in a grid. I need it to scroll down (every round of the game is one row) but also horizontal, because i need 5 - 10 Columns. When id use the paramter android:scrollbars="vertical|horizontal" in the recycler view it only scrolls down and makes it column very small. This happens even when i set it only to horizontal.

How it shouldnt look..

enter image description here

Code: The Activity

<?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>

The Fragment

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

The View for the Items in the Recycler View

<?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>

Fragment Code

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

Adapter Code, its uses a 2 dimensional Array to fill the Grid.

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

What i am doing wrong? :/

like image 475
Mattizin Avatar asked Jan 16 '16 22:01

Mattizin


People also ask

How do I use SnapHelper in RecyclerView?

You can now just use a SnapHelper. If you want a center-aligned snapping behavior similar to ViewPager then use PagerSnapHelper: SnapHelper snapHelper = new PagerSnapHelper(); snapHelper. attachToRecyclerView(recyclerView);

Is RecyclerView scrollable?

To be able to scroll through a vertical list of items that is longer than the screen, you need to add a vertical scrollbar. Inside RecyclerView , add an android:scrollbars attribute set to vertical .


1 Answers

You used wrong constructor new GridLayoutManager(getActivity(), gameData[0].length). Javadoc says that it "Creates a vertical GridLayoutManager".

Try new GridLayoutManager(getActivity(), 1, GridLayoutManager.HORIZONTAL, false) instead.

Here 1 counts as number of rows.

like image 174
aeracode Avatar answered Nov 07 '22 05:11

aeracode