我正在尝试构建一个视图,允许用户在水平和垂直方向上滚动类似Excel的结构。我最初的想法是将RecyclerView(带有LinearManager)放入HorizontalScrollView。但它似乎没有用。
这是我的代码:
<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>
现在它只允许Recycler滚动,HorizontalScrollView看起来像普通的FrameLayout(因为Recycler里面的视图正在剪切到边缘)。
我认为我在回收商中提出的观点具有固定的大小可能是相关的。
有关如何使这个概念起作用的任何提示?
答案 0 :(得分: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;
}
}
然后简单地说:
HorizontalScrollView myScroll = ...
SmartRecyclerView recyclerView = new SmartRecyclerView(...)
...
recyclerView.computedWidth = myNeededWidth;
myScroll.addView(recyclerView);
它有效! 快乐的编码...
示例工作代码:https://dl.dropboxusercontent.com/u/79978438/RecyclerView_ScrollView.zip