需求場(chǎng)景舉例:
當(dāng)我們項(xiàng)目中使用了第三方組件的頁面蚓聘,而組件現(xiàn)有的頁面又不能滿足我們項(xiàng)目的設(shè)計(jì)需求缅糟,此時(shí)你該如何做呢魔吐?
向組件開發(fā)方提需求,讓他滿足自己一次综慎?
如果組件是自家人開發(fā)的或許還可以涣仿,那要是其他團(tuán)隊(duì)或者公司的開發(fā)的呢,人家會(huì)鳥你嗎示惊?
很顯然好港,一個(gè)簡(jiǎn)單而特殊的需求,組件開發(fā)者不一定會(huì)給你做改動(dòng)涝涤,那么該怎么辦呢媚狰?
那只有我們自己改嘍!
一些思考:
如何修改ui呢阔拳?
- 修改布局文件
- java代碼中動(dòng)態(tài)修改
好像1不太現(xiàn)實(shí)直接修改aar里面布局文件崭孤,那剩下的就是在java代碼中動(dòng)態(tài)修改嘍类嗤。
如何修改?
關(guān)鍵是拿到Actvity或Fragment實(shí)例辨宠,進(jìn)而獲取想要?jiǎng)討B(tài)進(jìn)行修改的視圖實(shí)例(若要添加視圖元素需要獲取到父視圖實(shí)例)遗锣。
改如何做呢?
ActivityLifecycleCallbacks
用于監(jiān)聽整個(gè)應(yīng)用的Activity
生命周期變化的接口嗤形,接口聲明如下:
public interface ActivityLifecycleCallbacks {
void onActivityCreated(Activity var1, Bundle var2);
void onActivityStarted(Activity var1);
void onActivityResumed(Activity var1);
void onActivityPaused(Activity var1);
void onActivityStopped(Activity var1);
void onActivitySaveInstanceState(Activity var1, Bundle var2);
void onActivityDestroyed(Activity var1);
}
從接口生命可以發(fā)現(xiàn)精偿,所有的抽象方法對(duì)應(yīng)Activity
的生命周期方法,在Activity
每個(gè)生命周期階段都會(huì)回調(diào)接口實(shí)現(xiàn)當(dāng)中相應(yīng)的方法赋兵。還可以發(fā)現(xiàn)每個(gè)方法的參數(shù)都要Activity
笔咽,沒錯(cuò),這個(gè)Activity就是生命周期正在發(fā)生變化的那個(gè)霹期,這樣我們就可以通過這個(gè)Activity
引用叶组,就可以拿到他的View視圖和Fragement實(shí)例等等,我們需要的對(duì)象引用历造,進(jìn)而進(jìn)行動(dòng)態(tài)修改甩十。
具體實(shí)踐舉例
這里的例子是向其他組件的頁面中添加兩個(gè)視圖元素,并響應(yīng)點(diǎn)擊事件吭产。
實(shí)現(xiàn)監(jiān)聽接口
public class BeiKaoActivityLefecycleCallback implements Application.ActivityLifecycleCallbacks {
private IConsultEntryController mController;
private final String CURRENT_ACTIVITY_NAME = "com.nd.module_im.im.activity.ChatActivity";
public BeiKaoActivityLefecycleCallback(Activity activity) {
mController = new ConsultEntryControllerImpl(activity);
}
@Override
public void onActivityCreated(Activity activity, Bundle bundle) {
}
@Override
public void onActivityStarted(Activity activity) {
}
@Override
public void onActivityResumed(Activity activity) {
Log.d(activity.getClass().getSimpleName(), "onActivityResumed");
String canonicalName = activity.getClass().getCanonicalName();
if(CURRENT_ACTIVITY_NAME.equals(canonicalName)){
mController.showFloatEntry(activity);
}
}
@Override
public void onActivityPaused(Activity activity) {
}
@Override
public void onActivityStopped(Activity activity) {
Log.d(activity.getClass().getSimpleName(), "onActivityStopped");
String canonicalName = activity.getClass().getCanonicalName();
if(CURRENT_ACTIVITY_NAME.equals(canonicalName)){
mController.hideFloatEntry(activity);
}
}
@Override
public void onActivitySaveInstanceState(Activity activity, Bundle bundle) {
}
@Override
public void onActivityDestroyed(Activity activity) {
}
}
添加生命周期監(jiān)聽
context.registerActivityLifecycleCallbacks(new BeiKaoActivityLefecycleCallback(getActivity()));
獲取視圖元素并動(dòng)態(tài)修改視圖
ublic class ConsultEntryControllerImpl implements IConsultEntryController, View.OnClickListener {
private TextView floatView = null;
private LinearLayout mLinearLayout;
public ConsultEntryControllerImpl(Activity activity) {
initFloatView(activity);
}
protected void initFloatView(Activity activity) {
floatView = new TextView(activity);
floatView.setBackgroundResource(R.drawable.bg_primary_color_selector);
floatView.setTextColor(AppContextUtil.getColor(R.color.white));
floatView.setTextSize(TypedValue.COMPLEX_UNIT_SP,16);
floatView.setText("人工客服");
floatView.setGravity(Gravity.CENTER);
floatView.setPadding(MixedUtils.dpToPx(activity,8),0,MixedUtils.dpToPx(activity,8),0);
floatView.setOnClickListener(this);
mLinearLayout = (LinearLayout) LayoutInflater.from(activity).inflate(R.layout.layout_consult_entry,null);
mLinearLayout.setOnClickListener(this);
}
@Override
public void showFloatEntry(final Activity activity) {
Fragment fragment = ((AppCompatActivity) activity).getSupportFragmentManager().findFragmentByTag("chat");
if(!fragment.getClass().getCanonicalName().equals("com.nd.module_im.psp.ui.activity.ChatFragment_Psp")){
return;
}
RelativeLayout parent = (RelativeLayout) floatView.getParent();
if(parent != null){
return;
}
RelativeLayout frameLayout = (RelativeLayout) fragment.getView();
RelativeLayout.LayoutParams topBtnParam = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, MixedUtils.dpToPx(activity,56));
topBtnParam.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
RelativeLayout.LayoutParams linnearLayoutParam = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, MixedUtils.dpToPx(activity,28));
linnearLayoutParam.topMargin = MixedUtils.dpToPx(activity,56);
frameLayout.addView(floatView, topBtnParam);
frameLayout.addView(mLinearLayout,linnearLayoutParam);
Log.d("ConsultEntryControllerI", "顯示人工咨詢?nèi)肟趫D標(biāo)");
}
@Override
public void hideFloatEntry(Activity activity) {
RelativeLayout parent = (RelativeLayout) floatView.getParent();
if(parent == null){
return;
}
parent.removeView(floatView);
parent.removeView(mLinearLayout);
}
@Override
public void onClick(View view) {
CounselChatUtils.toXNCounselChat(AppContextUtil.getContext());
}
}