I am trying to port some iOS functionality to Android.
I intent to create a table where on swipe to the left shows 2 button: Edit and Delete.
I have
If you want button(s) on left side as well when swipe in the other direction, just try to add this simple lines in the existing answer:
In the drawButtons
method:
private void drawButtons(Canvas c, View itemView, List buffer, int pos, float dX) {
float right = itemView.getRight();
float left = itemView.getLeft();
float dButtonWidth = (-1) * dX / buffer.size();
for (UnderlayButton button : buffer) {
if (dX < 0) {
left = right - dButtonWidth;
button.onDraw(
c,
new RectF(
left,
itemView.getTop(),
right,
itemView.getBottom()
),
pos, dX //(to draw button on right)
);
right = left;
} else if (dX > 0) {
right = left - dButtonWidth;
button.onDraw(c,
new RectF(
right,
itemView.getTop(),
left,
itemView.getBottom()
), pos, dX //(to draw button on left)
);
}
}
}
In the onDraw
method check the value of dX
and set the text and the colour of the buttons:
public void onDraw(Canvas c, RectF rect, int pos, float dX) {
Paint p = new Paint();
// Draw background
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
if (dX > 0)
p.setColor(Color.parseColor("#23d2c5"));
else if (dX < 0)
p.setColor(Color.parseColor("#23d2c5"));
c.drawRect(rect, p);
// Draw Text
p.setColor(Color.WHITE);
p.setTextSize(36);
// p.setTextSize(LayoutHelper.getPx(MyApplication.getAppContext(), 12));
Rect r = new Rect();
float cHeight = rect.height();
float cWidth = rect.width();
p.setTextAlign(Paint.Align.LEFT);
p.getTextBounds(text, 0, text.length(), r);
float x = cWidth / 2f - r.width() / 2f - r.left;
float y = cHeight / 2f + r.height() / 2f - r.bottom;
if (dX > 0) {
p.setColor(Color.parseColor("#23d2c5"));
c.drawText("Reject", rect.left + x, rect.top + y, p);
} else if (dX < 0) {
c.drawText(text, rect.left + x, rect.top + y, p);
}
clickRegion = rect;
this.pos = pos;
}
}