Fix - no dividers between Waypoint rows

This commit is contained in:
Alexey Kulish 2016-01-11 18:27:12 +03:00
parent e5a085575b
commit 76f7e284c8
6 changed files with 245 additions and 28 deletions

View file

@ -224,6 +224,7 @@ public class DashboardOnMap implements ObservableScrollViewCallbacks, DynamicLis
stableAdapter.getObjects().remove(item);
activeObjPos = stableAdapter.getActiveObjects().indexOf(item);
stableAdapter.getActiveObjects().remove(item);
stableAdapter.refreshData();
stableAdapter.notifyDataSetChanged();
} else {
@ -240,7 +241,7 @@ public class DashboardOnMap implements ObservableScrollViewCallbacks, DynamicLis
stableAdapter.insert(item, position);
stableAdapter.getObjects().add(position, item);
stableAdapter.getActiveObjects().add(activeObjPos, item);
stableAdapter.updateIdMap();
stableAdapter.refreshData();
onItemsSwapped(stableAdapter.getActiveObjects());
}
@ -252,7 +253,7 @@ public class DashboardOnMap implements ObservableScrollViewCallbacks, DynamicLis
public void onHidePopup() {
if (listAdapter instanceof StableArrayAdapter) {
StableArrayAdapter stableAdapter = (StableArrayAdapter) listAdapter;
stableAdapter.updateIdMap();
stableAdapter.refreshData();
onItemsSwapped(stableAdapter.getActiveObjects());
}
}

View file

@ -1,9 +1,14 @@
package net.osmand.plus.helpers;
import android.app.Activity;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.DialogInterface.OnDismissListener;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.ShapeDrawable;
import android.graphics.drawable.shapes.Shape;
import android.os.AsyncTask;
import android.support.v4.app.FragmentActivity;
import android.support.v7.app.AlertDialog;
@ -30,6 +35,7 @@ import net.osmand.plus.activities.IntermediatePointsDialog;
import net.osmand.plus.activities.MapActivity;
import net.osmand.plus.helpers.WaypointHelper.LocationPointWrapper;
import net.osmand.plus.poi.PoiUIFilter;
import net.osmand.plus.views.controls.ListDividerShape;
import net.osmand.plus.views.controls.StableArrayAdapter;
import net.osmand.util.Algorithms;
import net.osmand.util.MapUtils;
@ -161,6 +167,85 @@ public class WaypointDialogHelper {
return activePoints;
}
private List<Drawable> getCustomDividers(Context ctx, List<Object> points, boolean nightMode) {
int color;
int pointColor;
if (nightMode) {
color = ctx.getResources().getColor(R.color.dashboard_divider_dark);
pointColor = ctx.getResources().getColor(R.color.dashboard_divider_dark);
} else {
color = ctx.getResources().getColor(R.color.dashboard_divider_light);
pointColor = ctx.getResources().getColor(R.color.ctx_menu_info_divider_light);
}
Shape fullDividerShape = new ListDividerShape(color, 0);
Shape halfDividerShape = new ListDividerShape(color, AndroidUtils.dpToPx(ctx, 56f));
Shape halfPointDividerShape = new ListDividerShape(color, AndroidUtils.dpToPx(ctx, 56f),
pointColor, AndroidUtils.dpToPx(ctx, 1.5f), true);
Shape headerDividerShape = new ListDividerShape(color, AndroidUtils.dpToPx(ctx, 16f));
final ShapeDrawable fullDivider = new ShapeDrawable(fullDividerShape);
final ShapeDrawable halfDivider = new ShapeDrawable(halfDividerShape);
final ShapeDrawable halfPointDivider = new ShapeDrawable(halfPointDividerShape);
final ShapeDrawable headerDivider = new ShapeDrawable(headerDividerShape);
final Drawable startingPointDivider = app.getIconsCache().getIcon(R.drawable.bg_shadow_list_bottom);
int divHeight = AndroidUtils.dpToPx(ctx, 1f);
fullDivider.setIntrinsicHeight(divHeight);
halfDivider.setIntrinsicHeight(divHeight);
halfPointDivider.setIntrinsicHeight(divHeight);
headerDivider.setIntrinsicHeight(divHeight);
List<Drawable> res = new ArrayList<>();
for (int i = 0; i < points.size(); i++) {
Object obj = points.get(i);
Object objNext = i + 1 < points.size() ? points.get(i + 1) : null;
if (objNext == null) {
break;
}
boolean labelView = (obj instanceof Integer);
boolean bottomDividerViewNext = (objNext instanceof Boolean) && !((Boolean) objNext);
boolean locationPoint = (obj instanceof LocationPointWrapper);
boolean locationPointNext = (objNext instanceof LocationPointWrapper);
Drawable d = null;
if (locationPointNext) {
if (locationPoint) {
LocationPointWrapper w = (LocationPointWrapper) obj;
if (w.type == WaypointHelper.TARGETS && ((TargetPoint) w.point).start) {
d = startingPointDivider; // starting point divider
} else {
if (w.type == WaypointHelper.TARGETS) {
d = halfPointDivider;
} else {
d = halfDivider;
}
}
} else {
LocationPointWrapper w = (LocationPointWrapper) objNext;
if (w.type == WaypointHelper.TARGETS) {
if (!((TargetPoint) w.point).start) {
d = fullDivider;
}
} else {
d = fullDivider;
}
}
} else if (objNext instanceof RadiusItem && labelView) {
d = headerDivider;
} else if (locationPoint && !bottomDividerViewNext) {
d = fullDivider;
}
res.add(d);
}
return res;
}
public StableArrayAdapter getWaypointsDrawerAdapter(
final boolean edit, final List<LocationPointWrapper> deletedPoints,
final MapActivity ctx, final int[] running, final boolean flat, final boolean nightMode) {
@ -176,6 +261,11 @@ public class WaypointDialogHelper {
return new StableArrayAdapter(ctx,
R.layout.waypoint_reached, R.id.title, points, activePoints) {
@Override
public void buildDividers() {
dividers = getCustomDividers(ctx, getObjects(), nightMode);
}
@Override
public boolean isEnabled(int position) {
Object obj = getItem(position);

View file

@ -287,6 +287,7 @@ public class MapContextMenuFragment extends Fragment implements DownloadEvents {
break;
case MotionEvent.ACTION_MOVE:
if (moving) {
hasMoved = true;
float y = event.getY();
float newY = getViewY() + (y - dy);
@ -305,11 +306,13 @@ public class MapContextMenuFragment extends Fragment implements DownloadEvents {
velocityY = Math.abs(velocity.getYVelocity());
if (velocityY > maxVelocityY)
maxVelocityY = velocityY;
}
break;
case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_CANCEL:
if (moving) {
moving = false;
int currentY = getViewY();
@ -320,7 +323,7 @@ public class MapContextMenuFragment extends Fragment implements DownloadEvents {
boolean skipHalfScreenState = Math.abs(currentY - dyMain) > skipHalfScreenStateLimit;
changeMenuState(currentY, skipHalfScreenState, slidingUp, slidingDown);
}
break;
}

View file

@ -27,6 +27,7 @@ import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.support.annotation.NonNull;
import android.support.v4.view.ViewCompat;
import android.util.AttributeSet;
@ -245,6 +246,51 @@ public class DynamicListView extends ObservableListView {
if (mHoverCell != null) {
mHoverCell.draw(canvas);
}
// Draw dividers
StableArrayAdapter stableAdapter = getStableAdapter();
if (stableAdapter != null && stableAdapter.hasDividers()) {
List<Drawable> dividers = stableAdapter.getDividers();
final int count = getChildCount();
final int first = getFirstVisiblePosition();
final int headerCount = getHeaderViewsCount();
final int itemCount = getCount();
final int footerLimit = (itemCount - getFooterViewsCount());
final Rect bounds = new Rect();
bounds.left = getPaddingLeft();
bounds.right = getRight() - getLeft() - getPaddingRight();
final int listBottom = getBottom() - getTop() + getScrollY();
for (int i = 0; i < count; i++) {
final int itemIndex = (first + i);
final boolean isHeader = (itemIndex < headerCount);
final boolean isFooter = (itemIndex >= footerLimit);
if (!isHeader && !isFooter && itemIndex < dividers.size()) {
Drawable divider = dividers.get(itemIndex - headerCount);
if (divider != null) {
final View child = getChildAt(i);
int bottom = child.getBottom();
final boolean isLastItem = (i == (count - 1));
if (bottom < listBottom && !isLastItem) {
final int nextIndex = (itemIndex + 1);
if (nextIndex >= headerCount && nextIndex < footerLimit) {
bounds.top = bottom;
bounds.bottom = bottom + divider.getIntrinsicHeight();
drawDivider(canvas, divider, bounds);
}
}
}
}
}
}
}
void drawDivider(Canvas canvas, Drawable divider, Rect bounds) {
divider.setBounds(bounds);
divider.draw(canvas);
}
@Override

View file

@ -0,0 +1,59 @@
package net.osmand.plus.views.controls;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.RectF;
import android.graphics.drawable.shapes.Shape;
public class ListDividerShape extends Shape {
private RectF mRect = new RectF();
private int lineColor;
private int paddingLeft;
private int pointColor;
private float pointRadius;
private boolean drawPoint;
public ListDividerShape(int lineColor, int paddingLeft) {
this.lineColor = lineColor;
this.paddingLeft = paddingLeft;
this.pointColor = 0;
this.pointRadius = 0f;
this.drawPoint = false;
}
public ListDividerShape(int lineColor, int paddingLeft, int pointColor, float pointRadius, boolean drawPoint) {
this.lineColor = lineColor;
this.paddingLeft = paddingLeft;
this.pointColor = pointColor;
this.pointRadius = pointRadius;
this.drawPoint = drawPoint;
}
@Override
public void draw(Canvas canvas, Paint paint) {
paint.setColor(lineColor);
mRect.left = paddingLeft;
canvas.drawRect(mRect, paint);
if (paddingLeft > 0 && drawPoint) {
paint.setColor(pointColor);
canvas.drawCircle(paddingLeft / 2f, (mRect.bottom - mRect.top) / 2f, pointRadius, paint);
}
}
@Override
protected void onResize(float width, float height) {
mRect.set(0, 0, width, height);
}
@Override
public ListDividerShape clone() throws CloneNotSupportedException {
final ListDividerShape shape = (ListDividerShape) super.clone();
shape.mRect = new RectF(mRect);
shape.lineColor = lineColor;
shape.paddingLeft = paddingLeft;
shape.pointColor = pointColor;
shape.pointRadius = pointRadius;
shape.drawPoint = drawPoint;
return shape;
}
}

View file

@ -1,6 +1,7 @@
package net.osmand.plus.views.controls;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.support.annotation.NonNull;
import android.widget.ArrayAdapter;
@ -17,6 +18,7 @@ public class StableArrayAdapter extends ArrayAdapter<Object> {
List<Object> objects;
List<Object> activeObjects;
HashMap<Object, Integer> mIdMap = new HashMap<>();
protected List<Drawable> dividers;
public StableArrayAdapter(Context context, int textViewResourceId, int titleId,
List<Object> objects, List<Object> activeObjects) {
@ -32,13 +34,21 @@ public class StableArrayAdapter extends ArrayAdapter<Object> {
return activeObjects;
}
public List<Drawable> getDividers() {
return dividers;
}
public boolean hasDividers() {
return dividers != null && dividers.size() > 0;
}
public void updateObjects(List<Object> objects, List<Object> activeObjects) {
this.objects = objects;
this.activeObjects = activeObjects;
updateIdMap();
refreshData();
}
public void updateIdMap() {
private void updateIdMap() {
HashMap<Object, Integer> idMap = new HashMap<>();
for (int i = 0; i < objects.size(); ++i) {
idMap.put(objects.get(i), i);
@ -46,6 +56,14 @@ public class StableArrayAdapter extends ArrayAdapter<Object> {
mIdMap = idMap;
}
public void refreshData() {
updateIdMap();
buildDividers();
}
public void buildDividers() {
}
@Override
public Object getItem(int position) {
return objects.get(position);