2D List with RecyclerView in HorizontalScrollView

冷暖自知 提交于 2019-12-10 13:59:19

问题


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:


[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



来源:https://stackoverflow.com/questions/28653173/2d-list-with-recyclerview-in-horizontalscrollview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!