ViewController
0.9.1
视图控制器管理一组构成应用程序用户界面的一部分的视图,旨在使UI开发更清晰和灵活。
英语| 中文| Android复杂界面开发实践之viewController:介绍
作为一个很好的做法,我建议您直接运行或观看演示代码。
到目前为止,您可以将依赖关系或复制源文件直接导入到您的项目中。
将jitpack存储库添加到您的构建文件
allprojects {
repositories {
.. .
maven { url " https://jitpack.io " }
}
}添加依赖关系
dependencies {
compile ' com.github.maoruibin:ViewController:0.9.1 '
}就像此演示图像一样,我们应该开发四个组件。
现在,我想开发评论组件作为演示,我将通过代码注释介绍该点。
// 1、every component mast extends ViewController
public class HouseCommentViewController extends ViewController < List < String >>
// 2、indicate layout id for this component
@ Override
protected int resLayoutId () {
return R . layout . detail_comment_layout ;
}
// init this component's view element
@ Override
protected void onCreatedView ( View view ) {
mLlContainer = view . findViewById ( R . id . ll_container );
...
}
// bind data to this view component
@ Override
protected void onBindView ( List < String > comments ) {
for ( String comment : comments ) {
TextView view = new TextView ( getContext ());
view . setBackgroundResource ( R . color . bk_item );
view . setText ( comment );
int padding = Utils . dp2px ( 16 );
view . setPadding ( padding , padding , padding , padding );
mLlContainer . addView ( view );
}
}现在,我们完成了一个简单的视图组件,您可以观看通过演示代码实现的左组件。
我们已经完成了四个用于房屋细节UI的组件。
HousePhotoViewController //House picture component
HouseParamViewController //House param info component
HouseDescViewController //House description component
HouseCommentViewController //House comment component左边的工作是组装。组装的核心是
每个视图控制器都支持一种将所有者视图附加到根布局的方法,因此活动应具有根布局来填充所有视图。
Java代码就是这样
// 1、define ViewController instance
private ViewController < List < String >> mHousePhotoViewController ;
private ViewController < HouseDetail . Param > mHouseParamViewController ;
private ViewController < List < String >> mHouseCommentViewController ;
private ViewController < String > mHouseDescViewController ;
// 2、init instance
mHousePhotoViewController = new HousePhotoViewController ( this );
mHouseParamViewController = new HouseParamViewController ( this );
mHouseDescViewController = new HouseDescViewController ( this );
mHouseCommentViewController = new HouseCommentViewController ( this );
// 3、attach view controller to activity root, usually the best choose for root is a vertical LinearLayout.
mHousePhotoViewController . attachRoot ( mLlContainer );
mHouseParamViewControler . attachRoot ( mLlContainer );
mHouseDescViewControler . attachRoot ( mLlContainer );
mHouseCommentViewControler . attachRoot ( mLlContainer );
// 4 、mock get data
getData ();
// 5、fill data to UI
fillData ();
// 6、fill data to different view controller
private void fillData ( HouseDetail detail ) {
mHousePhotoViewController . fillData ( detail . photos );
mHouseParamViewController . fillData ( detail . param );
mHouseDescViewController . fillData ( detail . desc );
mHouseCommentViewController . fillData ( detail . comments );
}而现在,一个复杂的UI已将四个组件拆分了,以这种方式,每个组件仅涉及所有者逻辑。
而且,如果其他活动或片段具有相同的组件需求,则可以直接重复使用代码,很好!
http://gudong.name
https://github.com/maoruibin
Copyright 2016 咕咚
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.