0

我正在尝试确定使单个Listview包含不同行样式的最佳方法。

getViewTypeCount()我搜索了很多,发现了一些使用and的技巧getItemViewType(int position),让列表视图布局根据位置而变化,这不是我想要的。

我要做的是根据行本身的 textview 值更改列表视图布局。

4

4 回答 4

0

I don't understand why you cant achieve this with the methods listed above, if you know how many views you have, then create an .xml for each one of them and then in the getView() inflate the corresponding layout depending on the value of your textview. You then return this row and it should then work.

于 2013-04-21T12:06:40.380 回答
0

well, this is exactly what you need to do.

override the getViewTypeCout to return the amount of types you have.
override the getItemViewType and determine the type according to the position (or item to show)

after you do that, just use this in your getView like this:

public View getView(int position, View convertView, ViewGroup parent) {
    if(getItemViewType(position) == FIRST_VIEW_TYPE) {
        // inflate first view
    } else {
        // inflate seconde view
    }
}
于 2013-04-21T12:06:50.233 回答
0

I searched alot and found some tricks using getViewTypeCount() and getItemViewType(int position), to let the listview layout changes depending on position and that was not what I'm looking for.

Yes, it is.

What I'm trying to do is to change the listview layout depeding on a textview value on the row itself.

No, you are not. You are trying to "change the listview layout depeding [sic] on" some text that you plan on putting into "a textview value on the row itself". As mr_archano points out, this TextView is not magically getting a value -- it is getting a value based on something you put there by one means or another.

Hence:

  • Override getViewTypeCount() to return the number of distinct possible row types

  • Override getItemViewType() to return a value from 0 to getViewTypeCount()-1 based upon the text that you plan on pouring into the TextView

  • Ensure that your getView() or bindView() implementation is aware of the different view types and handles them accordingly

于 2013-04-21T12:07:54.873 回答
0

ListView Row 中的 TextView 的内容必须来自您定义的一些数据(我假设可能是 ArrayList 或类似的数据),所以您需要做的是检查您之前要根据该数据绘制什么样的视图绘制 ROW,这是在 getView() 方法上完成的。

当 ListView 想要呈现给定元素时调用 getView(),您认为它与位置无关,但确实如此。您在绘制之前知道 TextView 的内容,因此解析该值并在 getView 中动态使用您想要的布局类型;)。

如果仍不清楚,请阅读此内容:Android 自定义列表视图行

于 2013-04-21T12:11:15.433 回答