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;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.text.TextUtils;
import android.view.View;
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 {
ListItemNode listItemNode;

View File

@@ -26,9 +26,7 @@ import com.github.pengfeizhou.jscore.JSValue;
import pub.doric.DoricContext;
import pub.doric.extension.bridge.DoricPlugin;
import pub.doric.shader.GroupNode;
import pub.doric.shader.StackNode;
import pub.doric.shader.ViewNode;
/**
* @Description: com.github.penfeizhou.doric.widget
@@ -48,9 +46,6 @@ public class ListItemNode extends StackNode {
@Override
protected void blend(FrameLayout view, String name, JSValue prop) {
if ("actions".equals(name)) {
if (prop.isArray()) {
this.actions = prop.asArray();
}
} else if ("identifier".equals(name)) {
this.identifier = prop.asString().value();
} else {
@@ -90,25 +85,6 @@ public class ListItemNode extends StackNode {
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.util.SparseArray;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import androidx.annotation.NonNull;
@@ -87,7 +89,7 @@ public class ListNode extends SuperNode<RecyclerView> implements IDoricScrollabl
@Override
protected RecyclerView build() {
RecyclerView recyclerView = new RecyclerView(getContext());
final RecyclerView recyclerView = new RecyclerView(getContext());
recyclerView.setLayoutManager(new LinearLayoutManager(getContext()) {
@Override
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;
}