Skip to content

Commit

Permalink
Add a new scheme to set relative rule.
Browse files Browse the repository at this point in the history
  • Loading branch information
ittianyu committed Dec 28, 2018
1 parent 5400910 commit 97ca622
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,15 @@ protected void initProps() {
};
twName = new TextWidget(context, lifecycle);
twName.id(R.id.tw_name);
// RelativeWidget root = new RelativeWidget(context, lifecycle,
// new WidgetAndProps(twId, new Prop(RelativeLayout.CENTER_IN_PARENT, Prop.TRUE)),
// new WidgetAndProps(twName, new Prop(RelativeLayout.BELOW, R.id.tw_id),
// new Prop(RelativeLayout.CENTER_HORIZONTAL, Prop.TRUE))
// );
RelativeWidget root = new RelativeWidget(context, lifecycle,
new WidgetAndProps(twId, new Prop(RelativeLayout.CENTER_IN_PARENT, Prop.TRUE)),
new WidgetAndProps(twName, new Prop(RelativeLayout.BELOW, R.id.tw_id),
new Prop(RelativeLayout.CENTER_HORIZONTAL, Prop.TRUE))
twId.addRule(RelativeLayout.CENTER_IN_PARENT, Prop.TRUE),
twName.addRule(RelativeLayout.BELOW, R.id.tw_id)
.addRule(RelativeLayout.CENTER_HORIZONTAL, Prop.TRUE)
);
return StateUtils.create(root);
}
Expand Down
13 changes: 13 additions & 0 deletions docs/base/6.RelativeWidget.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,19 @@ StatefulWidget<RelativeLayout, RelativeWidget>
}
```

#### relativeRule 2 ####

上面的那种方式似乎有点繁琐,热心的 [贵州穿青人](https://github.com/liyujiang-gzu) 提出了一个更好的方案,于是,你可以写成这样。 当然,如果外面套的不是 RelativeLayout,这设置并不会起作用。

```
RelativeWidget root = new RelativeWidget(context, lifecycle,
twId.addRule(RelativeLayout.CENTER_IN_PARENT, Prop.TRUE),
twName.addRule(RelativeLayout.BELOW, R.id.tw_id)
.addRule(RelativeLayout.CENTER_HORIZONTAL, Prop.TRUE)
);
```


#### initWidget ####

和上一节类似
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
import android.view.View;
import android.view.ViewGroup;

import android.widget.RelativeLayout;
import android.widget.RelativeLayout.LayoutParams;
import com.ittianyu.relight.utils.DensityUtils;
import com.ittianyu.relight.utils.ViewUtils;

Expand Down Expand Up @@ -40,6 +42,7 @@ public abstract class BaseAndroidWidget<V extends View, T extends BaseAndroidWid
public Integer weight;
public Integer visibility;
public View.OnClickListener onClickListener;
private RelativeRule relativeRule;

public BaseAndroidWidget(Context context, Lifecycle lifecycle) {
super(context, lifecycle);
Expand Down Expand Up @@ -267,6 +270,16 @@ public T weight(Integer weight) {
return self();
}

public T addRule(int verb, int rule) {
relative().add(verb, rule);
return self();
}

public T removeRule(int verb) {
relative().remove(verb);
return self();
}

private void updateMargin() {
ViewUtils.setMargin(view, marginStart, marginTop, marginEnd, marginBottom);
}
Expand Down Expand Up @@ -358,6 +371,9 @@ public void updateProps(V view) {
updatePadding();
onClickListener(onClickListener);
updateVisible();
if (relativeRule != null) {
relativeRule.apply(view.getLayoutParams());
}
}

private void updateVisible() {
Expand All @@ -379,4 +395,41 @@ public V createView(Context context) {
return null;
}

public RelativeRule relative() {
if (relativeRule == null) {
relativeRule = new RelativeRule();
}
return relativeRule;
}

public class RelativeRule {
private RelativeLayout.LayoutParams params;

RelativeRule() {
params = new RelativeLayout.LayoutParams(wrapContent, wrapContent);
}

public RelativeRule add(int verb, int subject) {
params.addRule(verb, subject);
return this;
}

public RelativeRule remove(int verb) {
params.removeRule(verb);
return this;
}

final void apply(ViewGroup.LayoutParams lp) {
if (!(lp instanceof RelativeLayout.LayoutParams)) {
return;
}
RelativeLayout.LayoutParams rlp = (LayoutParams) lp;
int[] rules = params.getRules();
for (int i = 0; i < rules.length; i++) {
rlp.addRule(i, rules[i]);
}
rlp.alignWithParent = params.alignWithParent;
view.setLayoutParams(rlp);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,11 @@ public class RelativeWidget extends ViewGroupWidget<RelativeLayout, RelativeWidg
protected WidgetAndProps[] childrenAndProps;

public RelativeWidget(Context context, Lifecycle lifecycle) {
this(context, lifecycle, (WidgetAndProps) null);
super(context, lifecycle);
}

public RelativeWidget(Context context, Lifecycle lifecycle, Widget... children) {
super(context, lifecycle, children);
}

public RelativeWidget(Context context, Lifecycle lifecycle, WidgetAndProps... childrenAndProps) {
Expand Down

0 comments on commit 97ca622

Please sign in to comment.