我想创建一个 Excel 类型的布局,其中有无限的垂直滚动。水平列数固定,但它们应该是可滚动的。
我试过下面的代码
<HorizontalScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">
<android.support.v7.widget.RecyclerView
android:id="@+id/table_data_recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</HorizontalScrollView>
如果我使用上面的代码,那么我可以垂直滚动,但列是不可滚动的。
即使在 xml 中为 HorizontalScrollView
和 RecyclerView
提供固定高度,列也不会滚动。
终于找到答案here ,建议扩展 RecyclerView
并动态计算高度。
public class MySmartRecyclerView extends RecyclerView {
public MySmartRecyclerView(Context context) {
super(context);
}
public MySmartRecyclerView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public MySmartRecyclerView(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;
}
}
那么只有这样才能实现吗?或者有没有其他方法可以实现这一目标。 ScrollView
中的 RecyclerView 有 NestedScrollView
,但 HorizontalScrollView
没有这样的东西。
任何指针将不胜感激。 TIA。
最佳答案
还有一种替代解决方案。你可以用这个 library .
关于Android - 如何创建 Excel 工作表类型布局?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42397725/