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.