ViewController
0.9.1
ビューコントローラーは、アプリのユーザーインターフェイスの一部を構成する一連のビューを管理します。これは、UIの開発をより明確で柔軟にすることを目的としています。
英語| 中文| Android biewController:介绍
良い練習として、デモコードを直接実行または視聴することをお勧めします。
依存関係をインポートしたり、ソースファイルをプロジェクトに直接コピーできます。これまでのところ、このLIBに1つのファイルのみです。
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.