在Horizo​​ntalScrollView中使用RecyclerView的2D列表

Kel*_*dos 5 android horizontalscrollview parallax android-recyclerview

我正在尝试构建一个视图,允许用户在水平和垂直方向上滚动类似Excel的结构.我最初的想法是将RecyclerView(带有LinearManager)放入Horizo​​ntalScrollView.但它似乎没有用.

这是我的代码:

<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>
Run Code Online (Sandbox Code Playgroud)

现在它只允许Recycler滚动,Horizo​​ntalScrollView似乎就像一个普通的FrameLayout(因为Recycler里面的视图正在剪切到边缘).

我认为我在Recycler中提出的观点具有固定的大小可能是相关的.

有关如何使这个概念起作用的任何提示?

Hox*_*Hox 1

[解决了]

所有的技巧都是手动设置 RecyclerView 宽度,因为它拒绝接受 WRAP_CONTENT 并且始终最大与屏幕宽度一样宽。技巧如下:

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;
}
}
Run Code Online (Sandbox Code Playgroud)

然后简单地:

HorizontalScrollView myScroll = ...
SmartRecyclerView recyclerView = new SmartRecyclerView(...)
...
recyclerView.computedWidth = myNeededWidth;
myScroll.addView(recyclerView);
Run Code Online (Sandbox Code Playgroud)

它有效!快乐编码...

示例工作代码:https://dl.dropboxusercontent.com/u/79978438/RecyclerView_ScrollView.zip