2D List with RecyclerView in HorizontalScrollView

2019-06-23 04:07发布

I am trying to build a view which will allow the user to scroll an Excel-like structure both horizontally and vertically. My initial idea was to put a RecyclerView (with LinearManager) into a HorizontalScrollView. But it does not 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>

Right now it only allows the Recycler to scroll, the HorizontalScrollView seems to act like a normal FrameLayout (as the views inside the Recycler are clipping to the edge).

I think it may be relevant that the views I put into the Recycler have fixed size.

Any tips on how to get this concept to work?

1条回答
狗以群分
2楼-- · 2019-06-23 04:32

[SOLVED]

All the trick is to manually set RecyclerView width, because it refuses to accept WRAP_CONTENT and always is maximally as wide as screen width. Trick is following:

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

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

and it WORKS! Happy coding...

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

查看更多
登录 后发表回答