Android 的數據綁定

BarneyMarcu 7年前發布 | 6K 次閱讀 安卓開發 Android開發 移動開發

效果圖

完成這么多的UI操作,其實指需要很少的代碼,現在附上全部關鍵代碼。

首先是布局文件:activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
 xmlns:ng="

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_margin="10dp"
    android:tag="ng:user:name"
    android:text="Hello World!" />

<EditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="userName"
    android:padding="10dp"
    android:tag="ng:user:name" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_margin="10dp"
    android:tag="ng:user:age" />

<ProgressBar
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    style="@style/Base.Widget.AppCompat.ProgressBar.Horizontal"
    android:progress="10"
    android:tag="ng:user:age"/>

<SeekBar
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:progress="0"
    android:tag="ng:user:age"/>

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <Switch
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:text="男"
        android:tag="ng:user:isMale"
        android:layout_margin="10dp"/>

    <CheckBox
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:tag="ng:user:isMale"
        android:text="男"
        android:layout_margin="10dp" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        android:tag="ng:user:isMale"
        android:text="Hello World!" />

</LinearLayout>

<LinearLayout
    android:id="@+id/rv_list"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:tag="nglist:user:list">

    <com.autonavi.jacklee.ngandroid.angular.view.NgItemView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:tag="ng:student"
        ng:ngRecyclerViewItemList="@layout/item_student" />

    <com.autonavi.jacklee.ngandroid.angular.view.NgItemView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:tag="ng:title"
        ng:ngRecyclerViewItemList="@layout/item_tag" />

    <com.autonavi.jacklee.ngandroid.angular.view.NgItemView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:tag="ng:content"
        ng:ngRecyclerViewItemList="@layout/item_content" />

</LinearLayout>


</LinearLayout></code></pre>

其中java代碼部分如下:

public class MainActivity extends AppCompatActivity implements CommonAdapter.CommonAdapterInterface{
private NgGo ngGo;
private NgModel ngUser;
private List list;
private LinearLayout ll_container;
private Handler handler = new Handler(){
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
if((int)ngUser.getValue("age") >= 100){
            ngUser.addParams("age", 0 );
        }else{
            ngUser.addParams("age", ((int)ngUser.getValue("age")) + 2 );
        }

    NgModel user = ((List<NgModel>)ngUser.getValue("list")).get(0);
    user.addParams("name", "Jack" + ((int)ngUser.getValue("age")));
}

};

@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ll_container = (LinearLayout) findViewById(R.id.ll_container); ngGo = new NgGo(ll_container);

ngUser = new NgModel("user");

ngGo.addNgModel(ngUser);

ngGo.start();

}

@Override protected void onResume() { super.onResume(); initNgGo(); }

private void initNgGo(){

ngUser.addParams("name", "Jhon");
ngUser.addParams("sex", "nan");
ngUser.addParams("age", 14);
ngUser.addParams("isMale", false);

list = new ArrayList<>();
for(int i = 0; i<10; i++){
    if(i%3 == 0){
        NgModel ngUser = new NgModel("student");
        ngUser.addParams("name", "Jack" + i);
        list.add(ngUser);
    }else if(i%3 == 1){
        NgModel ngUser = new NgModel("title");
        ngUser.addParams("name", "title" + i);
        list.add(ngUser);
    }else{
        NgModel ngUser = new NgModel("content");
        ngUser.addParams("name", "Content" + i);
        list.add(ngUser);
    }
}

ngUser.addParams("list", list);

Message msg = Message.obtain();
handler.sendMessageDelayed(msg, 1000);

CommonAdapter adapter = ngGo.getRecyclerAdapter(R.id.rv_list);
adapter.setCommonAdapterInterface(this);

}

@Override public void handleItem(int id, CommonAdapter.CommonHolder holder, final int position) { switch (id){ case R.id.rv_list: if(list.get(position).getTag().equals("student")){ holder.getView(R.id.iv_head).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) {

                }
            });
        }

        holder.getItemView().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(getApplicationContext(), position + "_item", Toast.LENGTH_SHORT).show();
            }
        });
        break;
}

}</code></pre>

}

以上就是所有代碼,是不是感覺代碼很少?哈哈哈

現在開始介紹AngularAndroid

一 需要記住的幾個對象

  1. NgGo:這個是AngularAndroid渲染的具體執行者
  2. NgModel:這個是數據綁定中的綁定對象,只要它的屬性值產生了變化,那么只要綁定在它身上的View就會做出相應的改變
  3. CommonAdapter:這個是RecyclerView的通用適配器
  4. NgItemView:這個是RecyclerView的Item,NgGo在監測到LinearLayout的tag中以nglist開頭,會自動將LinearLayout轉為RecyclerView,LinearLayout有多少個NgItemView,RecyclerView就有多少種item類型。

二 使用步驟

1.初始化NgGo對象 NgGo ngg = new NgGo(View parent);

parent這個view對象,類似與angularjs中的控制域,可能一個頁面中有不同的邏輯部分,這個時候,需要多個邏輯對象,這樣的話,每個邏輯操作對象都對應一個控制域。一般這樣的情況比較少。

2.初始化NgModel對象 NgModel user = new NgModel("user");

其中“user”這個參數,對應xml中"ng:user:name"的user,也就是NgModel需要指定對象名字。

3.將NgModel添加到NgGo中,交給NgGo去控制 ngg.addNgModel(user);

4.NgGo開始渲染 ngg.start();

至此,數據綁定完成,現在嘗試改變user的屬性值:user.addParams("name", "Jhon");

然后運行程序,是不是發現只要xml中tag為"ng:user:name"的view都顯示"Jhon"?

因為是雙向綁定,所以,當view的文本發生改變時,對應的NgModel的相應屬性也會發生變化。倘若Editext和TextView的tag都設置為"ng:user:name"時,會發現,TextView的值是跟著EditText的值動態改變的。

有人會問這有什么用?現在舉例一個最簡單的場景:

現在要做一個登錄頁面:

原始做法:

1.實例化帳號和密碼兩個EditText

3.點擊登錄時,判斷EditText的輸入值是否符合規定。

現在的做法:

1.點擊登錄時,判斷user對象的帳號(account)和密碼(password)是否符合規定

有木有發現,全程不用關注View對象,只需要關注具體的邏輯對象User,思維不用來回切

 本文由用戶 BarneyMarcu 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!