2D List with RecyclerView in HorizontalScrollView

I am trying to create a view that will allow the user to scroll through an Excel-like structure both horizontally and vertically. My initial idea was to translate the RecyclerView (with LinearManager) into a HorizontalScrollView. But it doesn't seem to work.

Here is my code:

<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"> <android.support.v7.widget.Toolbar android:id="@+id/gameplay_Toolbar" android:layout_width="match_parent" android:layout_height="56dp" android:background="@color/accent" app:title="@string/gameplay_score_toolbar" app:titleMarginStart="48dp" app:titleTextAppearance="@style/toolbar_title" /> <HorizontalScrollView android:id="@+id/gameplay_hotizontalScroll_ScrollView" android:layout_below="@+id/gameplay_Toolbar" android:layout_width="fill_parent" android:layout_height="match_parent" android:layout_marginTop="5dp" android:layout_marginLeft="5dp" android:fillViewport="true" > <android.support.v7.widget.RecyclerView android:id="@+id/gameplay_gameContents_RecyclerView" android:layout_width="fill_parent" android:layout_height="match_parent"/> </HorizontalScrollView> </RelativeLayout> 

Currently, it allows you to scroll through the Recycler, it looks like the HorizontalScrollView acts like a normal FrameLayout (since the views inside the Recycler are cropped to the edge).

I think it may be appropriate that the views that I put in Recycler have a fixed size.

Any tips on how to make this concept work?

+5
source share
1 answer

[SOLVED]

The whole trick is to manually set the bandwidth of the RecyclerView, as it refuses to accept WRAP_CONTENT and is always as wide as the screen. Trick:

 public class SmartRecyclerView extends RecyclerView { public int computedWidth = <needs to be set from outside> public SmartRecyclerView(Context context) { super(context); } public SmartRecyclerView(Context context, AttributeSet attrs) { super(context, attrs); } public SmartRecyclerView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } @Override public boolean canScrollHorizontally(int direction) { return false; } @Override public int getMinimumWidth() { return computedWidth; } @Override protected void onMeasure(int widthSpec, int heightSpec) { super.onMeasure(widthSpec, heightSpec); setMeasuredDimension(computedWidth, getMeasuredHeight()); } @Override protected int getSuggestedMinimumWidth() { return computedWidth; } } 

and then just:

 HorizontalScrollView myScroll = ... SmartRecyclerView recyclerView = new SmartRecyclerView(...) ... recyclerView.computedWidth = myNeededWidth; myScroll.addView(recyclerView); 

And he WORKS! Happy coding ...

working code example: https://dl.dropboxusercontent.com/u/79978438/RecyclerView_ScrollView.zip

+1
source

Source: https://habr.com/ru/post/1213884/


All Articles