implement android navbar custom left & right view

This commit is contained in:
王劲鹏 2020-01-10 15:56:36 +08:00 committed by osborn
parent 848b7005b7
commit 894f23cd5b
4 changed files with 121 additions and 2 deletions

View File

@ -77,6 +77,13 @@ public class DoricContext {
}
}
public void clearHeadNodes(String type) {
Map<String, ViewNode> map = mHeadNodes.get(type);
if (map != null) {
map.clear();
}
}
public ViewNode targetViewNode(String id) {
if (id.equals(mRootNode.getId())) {
return mRootNode;

View File

@ -72,6 +72,18 @@ public class BaseDoricNavBar extends FrameLayout implements IDoricNavBar {
mTvTitle.setText(title);
}
@Override
public void setLeft(View view) {
mLeftContainer.removeAllViews();
mLeftContainer.addView(view);
}
@Override
public void setRight(View view) {
mRightContainer.removeAllViews();
mRightContainer.addView(view);
}
private void updateTitleMargins() {
try {
int width = mRightContainer.getRight() - mLeftContainer.getLeft();
@ -102,4 +114,5 @@ public class BaseDoricNavBar extends FrameLayout implements IDoricNavBar {
super.onLayout(changed, left, top, right, bottom);
updateTitleMargins();
}
}

View File

@ -15,6 +15,8 @@
*/
package pub.doric.navbar;
import android.view.View;
/**
* @Description: pub.doric.navbar
* @Author: pengfei.zhou
@ -28,4 +30,8 @@ public interface IDoricNavBar {
void setTitle(String title);
void setBackgroundColor(int color);
void setLeft(View view);
void setRight(View view);
}

View File

@ -15,19 +15,22 @@
*/
package pub.doric.plugin;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import com.github.pengfeizhou.jscore.ArchiveException;
import com.github.pengfeizhou.jscore.JSDecoder;
import com.github.pengfeizhou.jscore.JSObject;
import com.github.pengfeizhou.jscore.JavaValue;
import java.util.concurrent.Callable;
import pub.doric.DoricContext;
import pub.doric.async.AsyncResult;
import pub.doric.extension.bridge.DoricMethod;
import pub.doric.extension.bridge.DoricPlugin;
import pub.doric.extension.bridge.DoricPromise;
import pub.doric.navbar.IDoricNavBar;
import pub.doric.shader.ViewNode;
import pub.doric.utils.ThreadMode;
/**
@ -37,6 +40,10 @@ import pub.doric.utils.ThreadMode;
*/
@DoricPlugin(name = "navbar")
public class NavBarPlugin extends DoricJavaPlugin {
private static final String TYPE_LEFT = "navbar_left";
private static final String TYPE_RIGHT = "navbar_right";
public NavBarPlugin(DoricContext doricContext) {
super(doricContext);
}
@ -104,4 +111,90 @@ public class NavBarPlugin extends DoricJavaPlugin {
}
}
}
@DoricMethod(thread = ThreadMode.UI)
public void setLeft(JSDecoder decoder, final DoricPromise promise) {
try {
final JSObject jsObject = decoder.decode().asObject();
getDoricContext().getDriver().asyncCall(new Callable<Object>() {
@Override
public Object call() throws Exception {
String viewId = jsObject.getProperty("id").asString().value();
String type = jsObject.getProperty("type").asString().value();
ViewNode node = ViewNode.create(getDoricContext(), type);
node.setId(viewId);
node.init(new FrameLayout.LayoutParams(0, 0));
node.blend(jsObject.getProperty("props").asObject());
getDoricContext().getDoricNavBar().setLeft(node.getNodeView());
getDoricContext().clearHeadNodes(TYPE_LEFT);
getDoricContext().addHeadNode(TYPE_LEFT, node);
return null;
}
}, ThreadMode.UI).setCallback(new AsyncResult.Callback<Object>() {
@Override
public void onResult(Object result) {
promise.resolve();
}
@Override
public void onError(Throwable t) {
t.printStackTrace();
promise.reject(new JavaValue(t.getLocalizedMessage()));
}
@Override
public void onFinish() {
}
});
} catch (Exception e) {
e.printStackTrace();
promise.reject(new JavaValue(e.getLocalizedMessage()));
}
}
@DoricMethod(thread = ThreadMode.UI)
public void setRight(JSDecoder decoder, final DoricPromise promise) {
try {
final JSObject jsObject = decoder.decode().asObject();
getDoricContext().getDriver().asyncCall(new Callable<Object>() {
@Override
public Object call() throws Exception {
String viewId = jsObject.getProperty("id").asString().value();
String type = jsObject.getProperty("type").asString().value();
ViewNode node = ViewNode.create(getDoricContext(), type);
node.setId(viewId);
node.init(new FrameLayout.LayoutParams(0, 0));
node.blend(jsObject.getProperty("props").asObject());
getDoricContext().getDoricNavBar().setRight(node.getNodeView());
getDoricContext().clearHeadNodes(TYPE_RIGHT);
getDoricContext().addHeadNode(TYPE_RIGHT, node);
return null;
}
}, ThreadMode.UI).setCallback(new AsyncResult.Callback<Object>() {
@Override
public void onResult(Object result) {
promise.resolve();
}
@Override
public void onError(Throwable t) {
t.printStackTrace();
promise.reject(new JavaValue(t.getLocalizedMessage()));
}
@Override
public void onFinish() {
}
});
} catch (Exception e) {
e.printStackTrace();
promise.reject(new JavaValue(e.getLocalizedMessage()));
}
}
}