[英]ListView with scrollable horizontal rows
Working on a ListView
with a custom list view adepter inherited from BaseAdapter
. 使用从
BaseAdapter
继承的自定义列表视图Adepter处理ListView
。 I'm trying to implement a table style in which each row is horizontally scrollable. 我正在尝试实现一种表格样式,其中每一行都可以水平滚动。 Therefore, I'm using
HorizontalScrollView
and LinearLayout
to structure each row. 因此,我正在使用
HorizontalScrollView
和LinearLayout
来构造每一行。 In each row, there are multiple TextView
with hardcoded texts in each. 每行中都有多个
TextView
,每行中都有硬编码的文本。 Everything is happening in the code and there's no xml and layout file. 一切都在代码中发生,并且没有xml和布局文件。 The issue that I'm having is I don't see any of
TextView
items that I add to my LinearLayout
. 我遇到的问题是我看不到添加到
LinearLayout
任何TextView
项。 Those TextView
are added as a child view to LinearLayout
and inside onLayout
method on set up their layout as: 这些
TextView
作为子视图添加到LinearLayout
并在onLayout
方法中将其设置为以下布局:
int dx = 0, viewWidth = 180;
for (int i = 0; i < m_Views.length; i++) {
TextView view = m_Views[i];
int textViewWidth = viewWidth;
if (i == 0) {
textViewWidth *= 2;
}
view.layout(dx, 0, dx + textViewWidth, contentHeight);
dx += textViewWidth;
}
m_HorizontalScrollView.layout(0, 0, contentWidth, contentHeight);
m_LinearLayout.layout(0, 0, dx, contentHeight);
That code, doesn't render TextView
elements. 该代码不会呈现
TextView
元素。 In addition, if I try to add the following line before setting the view's layout: 另外,如果我尝试在设置视图的布局之前添加以下行:
view.measure(MeasureSpec.UNSPECIFIED, MeasureSpec.UNSPECIFIED);
The app crashes with the following error in LinearLayout.java
class, java.lang.NullPointerException: Attempt to read from null array
. 该应用程序崩溃,在
LinearLayout.java
类java.lang.NullPointerException: Attempt to read from null array
出现以下错误java.lang.NullPointerException: Attempt to read from null array
。
I'm curious to know: 我很好奇:
layout
method? layout
方法而改变? measure
method? measure
方法,为什么应用程序崩溃? PS: Using RecyclerView
or any other approaches that requires in changing the adepter and list view is out of scope since that requires changing in the framework and base classes. PS:使用
RecyclerView
或更改adepter和list视图所需的任何其他方法超出了范围,因为这需要更改框架和基类。
You should not be calling layout
or measure
direction on a view. 您不应在视图上调用
layout
或measure
方向。 Rather, you should set the LayoutParams
of the view using View.setLayoutParams()
. 相反,您应该使用
View.setLayoutParams()
设置视图的LayoutParams
。 Since you are using a LinearLayout, you would use LinearLayout.LayoutParams
as your concrete implementation. 由于使用的是LinearLayout,因此将使用
LinearLayout.LayoutParams
作为您的具体实现。 Remove all direct calls to layout
and measure
and let the Android layout system handle this. 删除所有对
layout
和measure
直接调用,然后让Android布局系统处理。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.