android:fix list item set action cannot respond to click

This commit is contained in:
pengfei.zhou 2021-06-08 16:52:39 +08:00 committed by osborn
parent 4da1f3be9d
commit 23a39f20fc
4 changed files with 78 additions and 39 deletions

View File

@ -15,6 +15,8 @@
*/ */
package pub.doric.shader.list; package pub.doric.shader.list;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.text.TextUtils; import android.text.TextUtils;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
@ -150,6 +152,39 @@ class ListAdapter extends RecyclerView.Adapter<ListAdapter.DoricViewHolder> {
} }
} }
public void onItemLongClick(int position, View childView) {
JSValue jsValue = getItemModel(position);
if (jsValue != null && jsValue.isObject()) {
JSObject jsObject = jsValue.asObject();
String id = jsObject.getProperty("id").asString().value();
final ViewNode itemNode = this.listNode.getSubNodeById(id);
JSObject props = jsObject.getProperty("props").asObject();
JSValue prop = props.getProperty("actions");
if (itemNode != null && prop.isArray()) {
JSArray actions = prop.asArray();
if (actions != null && actions.size() > 0) {
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(childView.getContext())
.setItems(items, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
itemNode.callJSResponse(callbacks[which]);
dialog.dismiss();
}
}).show();
}
}
}
}
static class DoricViewHolder extends RecyclerView.ViewHolder { static class DoricViewHolder extends RecyclerView.ViewHolder {
ListItemNode listItemNode; ListItemNode listItemNode;

View File

@ -26,9 +26,7 @@ import com.github.pengfeizhou.jscore.JSValue;
import pub.doric.DoricContext; import pub.doric.DoricContext;
import pub.doric.extension.bridge.DoricPlugin; import pub.doric.extension.bridge.DoricPlugin;
import pub.doric.shader.GroupNode;
import pub.doric.shader.StackNode; import pub.doric.shader.StackNode;
import pub.doric.shader.ViewNode;
/** /**
* @Description: com.github.penfeizhou.doric.widget * @Description: com.github.penfeizhou.doric.widget
@ -48,9 +46,6 @@ public class ListItemNode extends StackNode {
@Override @Override
protected void blend(FrameLayout view, String name, JSValue prop) { protected void blend(FrameLayout view, String name, JSValue prop) {
if ("actions".equals(name)) { if ("actions".equals(name)) {
if (prop.isArray()) {
this.actions = prop.asArray();
}
} else if ("identifier".equals(name)) { } else if ("identifier".equals(name)) {
this.identifier = prop.asString().value(); this.identifier = prop.asString().value();
} else { } else {
@ -90,25 +85,6 @@ public class ListItemNode extends StackNode {
return true; return true;
} }
}); });
recursiveHandleLongClick(this);
}
}
private void recursiveHandleLongClick(GroupNode groupNode) {
for (int i = 0; i != groupNode.getChildNodes().size(); i++) {
ViewNode node = (ViewNode) groupNode.getChildNodes().get(i);
node.getView().setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
getView().performLongClick();
return false;
}
});
if (node instanceof GroupNode) {
recursiveHandleLongClick((GroupNode) node);
}
} }
} }
} }

View File

@ -17,6 +17,8 @@ package pub.doric.shader.list;
import android.text.TextUtils; import android.text.TextUtils;
import android.util.SparseArray; import android.util.SparseArray;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View; import android.view.View;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
@ -87,7 +89,7 @@ public class ListNode extends SuperNode<RecyclerView> implements IDoricScrollabl
@Override @Override
protected RecyclerView build() { protected RecyclerView build() {
RecyclerView recyclerView = new RecyclerView(getContext()); final RecyclerView recyclerView = new RecyclerView(getContext());
recyclerView.setLayoutManager(new LinearLayoutManager(getContext()) { recyclerView.setLayoutManager(new LinearLayoutManager(getContext()) {
@Override @Override
public boolean canScrollVertically() { public boolean canScrollVertically() {
@ -136,6 +138,29 @@ public class ListNode extends SuperNode<RecyclerView> implements IDoricScrollabl
} }
} }
}); });
final GestureDetector gestureDetector = new GestureDetector(
getContext(),
new GestureDetector.SimpleOnGestureListener() {
@Override
public void onLongPress(MotionEvent e) {
super.onLongPress(e);
View childView = recyclerView.findChildViewUnder(e.getX(), e.getY());
if (childView != null) {
int position = recyclerView.getChildLayoutPosition(childView);
listAdapter.onItemLongClick(position, childView);
}
}
});
recyclerView.addOnItemTouchListener(new RecyclerView.SimpleOnItemTouchListener() {
@Override
public boolean onInterceptTouchEvent(RecyclerView rv, MotionEvent e) {
if (gestureDetector.onTouchEvent(e)) {
return true;
}
return false;
}
});
return recyclerView; return recyclerView;
} }

View File

@ -1,4 +1,4 @@
import { Group, Panel, List, text, gravity, Color, LayoutSpec, list, listItem, log, vlayout, Gravity, hlayout, Text, refreshable, Refreshable, ListItem, layoutConfig, ViewHolder, ViewModel, VMPanel, loge, modal } from "doric"; import { Group, Panel, List, text, gravity, Color, LayoutSpec, list, listItem, log, vlayout, Gravity, hlayout, Text, refreshable, Refreshable, ListItem, layoutConfig, ViewHolder, ViewModel, VMPanel, loge, modal, stack } from "doric";
interface ItemModel { interface ItemModel {
text: string text: string
@ -67,34 +67,37 @@ class ListVM extends ViewModel<ListModel, ListVH> {
vh.list.apply({ vh.list.apply({
renderItem: (index) => { renderItem: (index) => {
const data = state.data[index] const data = state.data[index]
return listItem(text({ return listItem(stack([
text({
text: data.text, text: data.text,
textSize: 20, textSize: 20,
layoutConfig: { layoutConfig: {
widthSpec: LayoutSpec.MOST, widthSpec: LayoutSpec.FIT,
heightSpec: LayoutSpec.JUST, heightSpec: LayoutSpec.JUST,
}, },
height: 50, height: 50,
onClick: () => { modal(context).alert(data.text) } onClick: () => { modal(context).alert(data.text) }
}), { })
]), {
layoutConfig: { layoutConfig: {
widthSpec: LayoutSpec.MOST, widthSpec: LayoutSpec.MOST,
heightSpec: LayoutSpec.FIT, heightSpec: LayoutSpec.FIT,
} },
//onClick: () => { modal(context).alert("Item Clicked " + index) }
}).apply({ }).apply({
actions: [ actions: [
{ {
title: "First", title: "First",
backgroundColor: Color.RED, backgroundColor: Color.RED,
callback: () => { callback: () => {
modal(context).alert("First action") modal(context).alert("First action " + index)
} }
}, },
{ {
title: "Second", title: "Second",
backgroundColor: Color.BLUE, backgroundColor: Color.BLUE,
callback: () => { callback: () => {
modal(context).alert("Second action") modal(context).alert("Second action " + index)
} }
} }
] ]