ViewController
0.9.1
View Controller는 앱 사용자 인터페이스의 일부를 구성하는 뷰 세트를 관리하며 UI 개발을보다 명확하고 유연하게 개발하도록하는 것을 목표로합니다.
영어 | 中文 | Android 复杂界面开发实践之 viewController : 介绍
모범 사례로서 직접 데모 코드를 실행하거나 시청하는 것이 좋습니다.
종속성 또는 소스 파일을 프로젝트로 직접 가져올 수 있습니다.
Jitpack 저장소를 빌드 파일에 추가하십시오
allprojects {
repositories {
.. .
maven { url " https://jitpack.io " }
}
}종속성을 추가하십시오
dependencies {
compile ' com.github.maoruibin:ViewController:0.9.1 '
}이 데모 이미지와 마찬가지로 4 가지 구성 요소를 개발해야합니다.
이제 댓글 구성 요소를 데모로 개발하고 싶습니다. 코드별로 포인트를 소개합니다.
// 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에 대한 4 가지 구성 요소를 완성했습니다.
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는 4 가지 구성 요소를 분할했습니다. 이런 식으로 모든 구성 요소는 소유자 논리 만 다루었습니다.
다른 활동이나 조각이 동일한 구성 요소가 구현 해야하는 경우 코드를 직접 재사용 할 수 있습니다.
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.