add listview
This commit is contained in:
@@ -22,9 +22,6 @@ import android.widget.FrameLayout;
|
||||
|
||||
import androidx.appcompat.app.AppCompatActivity;
|
||||
|
||||
import org.greenrobot.eventbus.EventBus;
|
||||
import org.greenrobot.eventbus.Subscribe;
|
||||
import org.greenrobot.eventbus.ThreadMode;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
@@ -36,6 +33,7 @@ import pub.doric.dev.event.EnterDebugEvent;
|
||||
import pub.doric.dev.event.QuitDebugEvent;
|
||||
import pub.doric.engine.ChangeEngineCallback;
|
||||
import pub.doric.utils.DoricUtils;
|
||||
import pub.doric.utils.ThreadMode;
|
||||
|
||||
|
||||
public class MainActivity extends AppCompatActivity {
|
||||
|
@@ -20,6 +20,7 @@ import android.text.TextUtils;
|
||||
import pub.doric.plugin.ShaderPlugin;
|
||||
import pub.doric.shader.HLayoutNode;
|
||||
import pub.doric.shader.ImageNode;
|
||||
import pub.doric.shader.list.ListNode;
|
||||
import pub.doric.shader.RootNode;
|
||||
import pub.doric.shader.StackNode;
|
||||
import pub.doric.shader.TextNode;
|
||||
@@ -66,6 +67,7 @@ public class DoricRegistry {
|
||||
this.registerViewNode(StackNode.class);
|
||||
this.registerViewNode(VLayoutNode.class);
|
||||
this.registerViewNode(HLayoutNode.class);
|
||||
this.registerViewNode(ListNode.class);
|
||||
initRegistry(this);
|
||||
}
|
||||
|
||||
|
@@ -45,73 +45,69 @@ public abstract class GroupNode<F extends ViewGroup> extends ViewNode<F> {
|
||||
|
||||
@Override
|
||||
protected void blend(F view, ViewGroup.LayoutParams layoutParams, String name, JSValue prop) {
|
||||
super.blend(view, layoutParams, name, prop);
|
||||
switch (name) {
|
||||
case "children":
|
||||
JSArray jsArray = prop.asArray();
|
||||
int i;
|
||||
List<ViewNode> tobeRemoved = new ArrayList<>();
|
||||
for (i = 0; i < jsArray.size(); i++) {
|
||||
JSValue jsValue = jsArray.get(i);
|
||||
if (!jsValue.isObject()) {
|
||||
continue;
|
||||
}
|
||||
JSObject childObj = jsValue.asObject();
|
||||
String type = childObj.getProperty("type").asString().value();
|
||||
String id = childObj.getProperty("id").asString().value();
|
||||
ViewNode child = mChildrenNode.get(id);
|
||||
if (child == null) {
|
||||
child = ViewNode.create(getDoricContext(), type);
|
||||
if ("children".equals(name)) {
|
||||
JSArray jsArray = prop.asArray();
|
||||
int i;
|
||||
List<ViewNode> tobeRemoved = new ArrayList<>();
|
||||
for (i = 0; i < jsArray.size(); i++) {
|
||||
JSValue jsValue = jsArray.get(i);
|
||||
if (!jsValue.isObject()) {
|
||||
continue;
|
||||
}
|
||||
JSObject childObj = jsValue.asObject();
|
||||
String type = childObj.getProperty("type").asString().value();
|
||||
String id = childObj.getProperty("id").asString().value();
|
||||
ViewNode child = mChildrenNode.get(id);
|
||||
if (child == null) {
|
||||
child = ViewNode.create(getDoricContext(), type);
|
||||
child.index = i;
|
||||
child.mParent = this;
|
||||
child.mId = id;
|
||||
mChildrenNode.put(id, child);
|
||||
} else {
|
||||
if (i != child.index) {
|
||||
mIndexInfo.remove(i);
|
||||
child.index = i;
|
||||
child.mParent = this;
|
||||
child.mId = id;
|
||||
mChildrenNode.put(id, child);
|
||||
} else {
|
||||
if (i != child.index) {
|
||||
mIndexInfo.remove(i);
|
||||
child.index = i;
|
||||
mView.removeView(child.getView());
|
||||
}
|
||||
tobeRemoved.remove(child);
|
||||
mView.removeView(child.getView());
|
||||
}
|
||||
|
||||
ViewNode node = mIndexInfo.get(i);
|
||||
|
||||
if (node != null && node != child) {
|
||||
mView.removeViewAt(i);
|
||||
mIndexInfo.remove(i);
|
||||
tobeRemoved.add(node);
|
||||
}
|
||||
|
||||
ViewGroup.LayoutParams params = child.getLayoutParams();
|
||||
if (params == null) {
|
||||
params = generateDefaultLayoutParams();
|
||||
}
|
||||
child.blend(childObj.getProperty("props").asObject(), params);
|
||||
if (mIndexInfo.get(i) == null) {
|
||||
mView.addView(child.getView(), i, child.getLayoutParams());
|
||||
mIndexInfo.put(i, child);
|
||||
}
|
||||
}
|
||||
int count = mView.getChildCount();
|
||||
while (i < count) {
|
||||
ViewNode node = mIndexInfo.get(i);
|
||||
if (node != null) {
|
||||
mChildrenNode.remove(node.getId());
|
||||
mIndexInfo.remove(i);
|
||||
tobeRemoved.remove(node);
|
||||
mView.removeView(node.getView());
|
||||
}
|
||||
i++;
|
||||
tobeRemoved.remove(child);
|
||||
}
|
||||
|
||||
for (ViewNode node : tobeRemoved) {
|
||||
ViewNode node = mIndexInfo.get(i);
|
||||
|
||||
if (node != null && node != child) {
|
||||
mView.removeViewAt(i);
|
||||
mIndexInfo.remove(i);
|
||||
tobeRemoved.add(node);
|
||||
}
|
||||
|
||||
ViewGroup.LayoutParams params = child.getLayoutParams();
|
||||
if (params == null) {
|
||||
params = generateDefaultLayoutParams();
|
||||
}
|
||||
child.blend(childObj.getProperty("props").asObject(), params);
|
||||
if (mIndexInfo.get(i) == null) {
|
||||
mView.addView(child.getView(), i, child.getLayoutParams());
|
||||
mIndexInfo.put(i, child);
|
||||
}
|
||||
}
|
||||
int count = mView.getChildCount();
|
||||
while (i < count) {
|
||||
ViewNode node = mIndexInfo.get(i);
|
||||
if (node != null) {
|
||||
mChildrenNode.remove(node.getId());
|
||||
mIndexInfo.remove(i);
|
||||
tobeRemoved.remove(node);
|
||||
mView.removeView(node.getView());
|
||||
}
|
||||
break;
|
||||
default:
|
||||
super.blend(view, layoutParams, name, prop);
|
||||
break;
|
||||
i++;
|
||||
}
|
||||
|
||||
for (ViewNode node : tobeRemoved) {
|
||||
mChildrenNode.remove(node.getId());
|
||||
}
|
||||
} else {
|
||||
super.blend(view, layoutParams, name, prop);
|
||||
}
|
||||
}
|
||||
|
||||
|
@@ -0,0 +1,77 @@
|
||||
/*
|
||||
* Copyright [2019] [Doric.Pub]
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package pub.doric.shader.list;
|
||||
|
||||
import android.util.SparseArray;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.recyclerview.widget.RecyclerView;
|
||||
|
||||
import com.github.pengfeizhou.jscore.JSObject;
|
||||
|
||||
/**
|
||||
* @Description: com.github.penfeizhou.doric.widget
|
||||
* @Author: pengfei.zhou
|
||||
* @CreateDate: 2019-11-12
|
||||
*/
|
||||
public class ListAdapter extends RecyclerView.Adapter<ListAdapter.DoricViewHolder> {
|
||||
|
||||
private final ListNode listNode;
|
||||
String renderItemFuncId;
|
||||
String renderBunchedItemsFuncId;
|
||||
int itemCount = 0;
|
||||
int batchCount = 15;
|
||||
private SparseArray<JSObject> itemObjects = new SparseArray<>();
|
||||
|
||||
public ListAdapter(ListNode listNode) {
|
||||
this.listNode = listNode;
|
||||
}
|
||||
|
||||
@NonNull
|
||||
@Override
|
||||
public DoricViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onBindViewHolder(@NonNull DoricViewHolder holder, int position) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getItemCount() {
|
||||
return itemCount;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getItemViewType(int position) {
|
||||
return super.getItemViewType(position);
|
||||
}
|
||||
|
||||
|
||||
private void jsCallRenderItem() {
|
||||
listNode.callJSResponse(renderItemFuncId);
|
||||
}
|
||||
|
||||
|
||||
public static class DoricViewHolder extends RecyclerView.ViewHolder {
|
||||
public DoricViewHolder(@NonNull View itemView) {
|
||||
super(itemView);
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,68 @@
|
||||
/*
|
||||
* Copyright [2019] [Doric.Pub]
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package pub.doric.shader.list;
|
||||
|
||||
import android.view.ViewGroup;
|
||||
|
||||
import androidx.recyclerview.widget.RecyclerView;
|
||||
|
||||
import com.github.pengfeizhou.jscore.JSObject;
|
||||
import com.github.pengfeizhou.jscore.JSValue;
|
||||
|
||||
import pub.doric.DoricContext;
|
||||
import pub.doric.extension.bridge.DoricPlugin;
|
||||
import pub.doric.shader.ViewNode;
|
||||
|
||||
/**
|
||||
* @Description: com.github.penfeizhou.doric.widget
|
||||
* @Author: pengfei.zhou
|
||||
* @CreateDate: 2019-11-12
|
||||
*/
|
||||
@DoricPlugin(name = "List")
|
||||
public class ListNode extends ViewNode<RecyclerView> {
|
||||
private final ListAdapter listAdapter;
|
||||
|
||||
public ListNode(DoricContext doricContext) {
|
||||
super(doricContext);
|
||||
this.listAdapter = new ListAdapter(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected RecyclerView build(JSObject jsObject) {
|
||||
return new RecyclerView(getContext());
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void blend(RecyclerView view, ViewGroup.LayoutParams layoutParams, String name, JSValue prop) {
|
||||
switch (name) {
|
||||
case "itemCount":
|
||||
this.listAdapter.itemCount = prop.asNumber().toInt();
|
||||
break;
|
||||
case "renderItem":
|
||||
this.listAdapter.renderItemFuncId = prop.asString().value();
|
||||
break;
|
||||
case "renderBunchedItemsFuncId":
|
||||
this.listAdapter.renderBunchedItemsFuncId = prop.asString().value();
|
||||
break;
|
||||
case "batchCount":
|
||||
this.listAdapter.batchCount = 15;
|
||||
break;
|
||||
default:
|
||||
super.blend(view, layoutParams, name, prop);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user