我正在尝试构建一个视图,允许用户水平和垂直滚动类似 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 内的视图被裁剪到边缘)。
我认为我放入回收器中的视图具有固定大小可能是相关的。
关于如何让这个概念发挥作用有什么建议吗?
[SOLVED]
所有的技巧都是手动设置 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 https://dl.dropboxusercontent.com/u/79978438/RecyclerView_ScrollView.zip
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)