简体   繁体   English

具有可滚动水平行的ListView

[英]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. 因此,我正在使用HorizontalScrollViewLinearLayout来构造每一行。 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.javajava.lang.NullPointerException: Attempt to read from null array出现以下错误java.lang.NullPointerException: Attempt to read from null array

I'm curious to know: 我很好奇:

  1. Why the layout of text views don't change with calling layout method? 为什么文本视图的布局不会随调用layout方法而改变?
  2. Why does the app crashes if I call 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. 您不应在视图上调用layoutmeasure方向。 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. 删除所有对layoutmeasure直接调用,然后让Android布局系统处理。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM