Skip to content

Commit

Permalink
fix(android): crash in ListView on API 21 (#13262)
Browse files Browse the repository at this point in the history
* fix(android): crash in ListView on API 21

default listDivider on API 21 is instance of NinePatchDrawable, not a GradientDrawable

fix #13261

* fix(android): hide default separator when separatorStyle is SEPARATOR_STYLE_NONE

* docs(api): fix ListView example

* docs(api): add warning about separatorHeight usage on Android API below 23

Co-authored-by: Hans Knöchel <hansemannn@users.noreply.github.com>
  • Loading branch information
drauggres and hansemannn committed Mar 21, 2022
1 parent 5068dc1 commit b30de10
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
Expand Up @@ -8,6 +8,7 @@

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.GradientDrawable;
import android.view.View;
import android.view.ViewGroup;
Expand Down Expand Up @@ -214,7 +215,9 @@ private void processProperty(String name, Object value)
? TiConvert.toTiDimension(heightString, TiDimension.TYPE_HEIGHT)
.getAsPixels((View) getNativeView().getParent()) : 0;

if (name.equals(TiC.PROPERTY_SEPARATOR_COLOR)
if (height == 0) {
this.listView.setSeparator(0, height);
} else if (name.equals(TiC.PROPERTY_SEPARATOR_COLOR)
|| properties.containsKey(TiC.PROPERTY_SEPARATOR_COLOR)) {
String colorString = properties.getString(TiC.PROPERTY_SEPARATOR_COLOR);
if (name.equals(TiC.PROPERTY_SEPARATOR_COLOR)) {
Expand All @@ -227,10 +230,12 @@ private void processProperty(String name, Object value)

} else {
final TypedArray divider = context.obtainStyledAttributes(new int[] { android.R.attr.listDivider });
final GradientDrawable defaultDrawable = (GradientDrawable) divider.getDrawable(0);
final Drawable defaultDrawable = divider.getDrawable(0);

// Set platform default separator.
defaultDrawable.setSize(0, height);
if (defaultDrawable instanceof GradientDrawable) {
((GradientDrawable) defaultDrawable).setSize(0, height);
}
this.listView.setSeparator(defaultDrawable);
divider.recycle();
}
Expand Down
4 changes: 3 additions & 1 deletion apidoc/Titanium/UI/ListView.yml
Expand Up @@ -758,6 +758,8 @@ properties:
description: |
Height of the ListView separator, in platform-specific units. If undefined, default native height will be used.
Numerical inputs are treated as pixels. For example, 3 and "3px" are equivalent.
On Android API < 23 you must specify <Titanium.UI.ListView.separatorStyle> and
<Titanium.UI.ListView.separatorColor> for this property to work.
type: [String, Number]
since: "4.1.0"
platforms: [android]
Expand Down Expand Up @@ -1367,8 +1369,8 @@ examples:
{properties: { title: 'Carrots'}},
{properties: { title: 'Potatoes'}},
];
sections.push(vegSection);
var vegSection = Ti.UI.createListSection({ headerTitle: 'Vegetables', items: vegDataSet});
sections.push(vegSection);
listView.sections = sections;
win.add(listView);
Expand Down

0 comments on commit b30de10

Please sign in to comment.