Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android Afinal框架學習(二) FinalActivity 一個IOC框架

Android Afinal框架學習(二) FinalActivity 一個IOC框架

編輯:關於Android編程

 

對應的源碼:

net.tsz.afinal.annotation.view.*

FinalActivity

FinalActivity是一個基礎類,結合注解實現了,依賴注入(view的資源id,常用的監聽器), 利用set方式注入

完全注解方式就可以進行UI綁定和事件綁定,無需findViewById和set event Listener

這個過程:initInjectedView>findViewById、setListener(activity, activity.field, eventLocalMethod, eventType)>

1.根據id得到view對象,設置到activity對應的field中

2. switch(eventType) {

case event:

view.set event listener(new EventListener(activity, eventLocalMethod));

EventListener implements many event listener>

在對應的event的重寫方法中,反射調用eventLocalMethod

這些eventLocalMethod 的參數需要與 對應的系統 event method 的參數一致

 

...

}

注解有哪些類型:

 

@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME) 
public @interface ViewInject {
	public int id();
	public String click() default ;
	public String longClick() default ;
	public String itemClick() default ;
	public String itemLongClick() default ;
	public Select select() default @Select(selected=) ;
}

 

調用注解:

package com.stone.afinal;

import net.tsz.afinal.FinalActivity;
import net.tsz.afinal.annotation.view.Select;
import net.tsz.afinal.annotation.view.ViewInject;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;

public class MainActivity extends FinalActivity {
	@ViewInject(
			id = R.id.imageview, 
			click = myClick,
			longClick = myLongClick)
	ImageView imageview;
	
	@ViewInject(
			id = R.id.listview, 
			click = myClick,
			itemLongClick = myItemLongClick,
			itemClick = myItemClick,
			longClick = myLongClick,
			select = @Select(selected=myItemSelected, noSelected=myNoSelected))
	ListView listView;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);//繼承FinalActivity,setContentView中初始化了
	}
	 
	public void myClick(View v) {
		System.out.println(--myClick--);
	}
	public void myLongClick(View v) {
		System.out.println(--myLongClick--);
	}
	public void myItemLongClick(AdapterView arg0, View arg1, int arg2,long arg3) {
		System.out.println(--myItemLongClick--);
	}
	public void myItemClick(AdapterView arg0, View arg1, int arg2, long arg3) {
		System.out.println(--myItemClick--);
	}
	public void myItemSelected(AdapterView arg0, View arg1, int arg2,long arg3) {
		System.out.println(--myItemSelected--);
	}
	
	public void myNoSelected(AdapterView arg0) {
		System.out.println(--myNoSelected--);
	}
}

 

在其他侵入式框架下使用(如ActionBarShelock)

     protected void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
        setContentView(view);
        FinalActivity.initInjectedView(this);
     }

在Fragment中使用

     public View onCreateView(LayoutInflater inflater, ViewGroup container,
          Bundle savedInstanceState) {
       View viewRoot = inflater.inflate(R.layout.map_frame, container, false);
       FinalActivity.initInjectedView(this,viewRoot);
    }

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved