feat:android support listItem's actions

This commit is contained in:
pengfei.zhou 2021-04-25 15:47:59 +08:00 committed by osborn
parent e18b7e781e
commit 658f0ed3b3
2 changed files with 43 additions and 1 deletions

View File

@ -15,11 +15,18 @@
*/
package pub.doric.shader.list;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.view.View;
import android.widget.FrameLayout;
import com.github.pengfeizhou.jscore.JSArray;
import com.github.pengfeizhou.jscore.JSObject;
import com.github.pengfeizhou.jscore.JSValue;
import java.util.ArrayList;
import java.util.List;
import pub.doric.DoricContext;
import pub.doric.extension.bridge.DoricPlugin;
import pub.doric.shader.StackNode;
@ -32,6 +39,7 @@ import pub.doric.shader.StackNode;
@DoricPlugin(name = "ListItem")
public class ListItemNode extends StackNode {
public String identifier = "";
private JSArray actions = null;
public ListItemNode(DoricContext doricContext) {
super(doricContext);
@ -40,7 +48,11 @@ public class ListItemNode extends StackNode {
@Override
protected void blend(FrameLayout view, String name, JSValue prop) {
if ("identifier".equals(name)) {
if ("actions".equals(name)) {
if (prop.isArray()) {
this.actions = prop.asArray();
}
} else if ("identifier".equals(name)) {
this.identifier = prop.asString().value();
} else {
super.blend(view, name, prop);
@ -52,5 +64,33 @@ public class ListItemNode extends StackNode {
super.blend(jsObject);
getNodeView().getLayoutParams().width = getLayoutParams().width;
getNodeView().getLayoutParams().height = getLayoutParams().height;
if (this.actions != null && this.actions.size() > 0) {
getView().setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
if (actions == null || actions.size() == 0) {
return false;
}
String[] items = new String[actions.size()];
final String[] callbacks = new String[actions.size()];
for (int i = 0; i < actions.size(); i++) {
JSObject action = actions.get(i).asObject();
String title = action.getProperty("title").asString().value();
String callback = action.getProperty("callback").asString().value();
items[i] = title;
callbacks[i] = callback;
}
new AlertDialog.Builder(getContext())
.setItems(items, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
callJSResponse(callbacks[which]);
dialog.dismiss();
}
}).show();
return true;
}
});
}
}
}

View File

@ -84,12 +84,14 @@ class ListVM extends ViewModel<ListModel, ListVH> {
actions: [
{
title: "First",
backgroundColor: Color.RED,
callback: () => {
modal(context).alert("First action")
}
},
{
title: "Second",
backgroundColor: Color.BLUE,
callback: () => {
modal(context).alert("Second action")
}