คอนโทรลเลอร์มุมมองจัดการชุดมุมมองที่ประกอบขึ้นเป็นส่วนหนึ่งของส่วนต่อประสานผู้ใช้แอปของคุณมันมีจุดมุ่งหมายเพื่อให้ UI พัฒนาการเปลี่ยนแปลงที่ชัดเจนและยืดหยุ่นมากขึ้น
ภาษาอังกฤษ | 中文 | Android 复杂界面开发实践之 ViewController: 介绍
เป็นแนวปฏิบัติที่ดีฉันขอแนะนำให้คุณเรียกใช้หรือดูรหัสสาธิตโดยตรง
คุณสามารถนำเข้าไฟล์การพึ่งพาหรือคัดลอกไปยังโครงการของคุณโดยตรงจนถึงตอนนี้มีเพียงไฟล์เดียวใน LIB นี้
เพิ่มที่เก็บ 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.