android - Recycler View with GridLayoutManager horizontal Scrolling doesnt work -
i have activity, includes fragment. fragment includes recycler view. want display data of game in grid. need scroll down (every round of game 1 row) horizontal, because need 5 - 10 columns. when id use paramter android:scrollbars="vertical|horizontal" in recycler view scrolls down , makes column small. happens when set horizontal.
code: 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 items in 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; /** * 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 layout 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, uses 2 dimensional array fill 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 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 = 0; < 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 new view view v = layoutinflater.from(parent.getcontext()) .inflate(r.layout.singlegamedataitem, parent, false); // set view's size, margins, paddings , layout parameters viewholder vh = new viewholder(v); return vh; } // replace contents of view (invoked layout manager) @override public void onbindviewholder(viewholder holder, int position) { // - element dataset @ position // - replace contents of view element holder.textview.settext(list.get(position).getinhalt()); } @override public int getitemcount() { return list.size(); } }
what doing wrong? :/
you used wrong constructor new gridlayoutmanager(getactivity(), gamedata[0].length)
. javadoc says "creates vertical gridlayoutmanager".
try new gridlayoutmanager(getactivity(), 1, gridlayoutmanager.horizontal, false)
instead.
here 1
counts number of rows.
Comments
Post a Comment