SQLの窓

2012年09月21日


android-binding を使用した、固定値、イベント、オプションメニューの構築


Google ドライブからダウンロード



バインドの方法としては、BindingActivityV30 + inflateAndBind を使うのが最新なのですが、作者さんの最新のサンプルがまだそれを使って無いのでここでは利用していません。それを利用すると、Android の最新バージョンで ActionBar を使う事を簡単に変更できるようです( メイン画面とメニューと ActionBar を一つの XML で定義します / 動作確認はしました )

プロジェクト作成の基本部分

android-binding を使って Windows C#(XAML) のようなバインド処理の実装

画面定義
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:binding="http://www.gueei.com/android-binding/"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:padding="@dimen/padding_medium"
        binding:text="hello"
        tools:context=".P1Activity" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        binding:text="button_value" 
        binding:onClick="Button_Click_1"/>

</RelativeLayout>



メニュー定義
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:binding="http://www.gueei.com/android-binding/">
    <item android:id="@+id/menu_settings"
        binding:title="bind_menu"
        android:orderInCategory="100"
        android:showAsAction="never" 
        binding:onClick="Menu_Click_1"/>
</menu>



BindingActivity

bindModel.button_value.set("ボタン"); は、クラス内で初期設定するのでは無く、外から設定した例です。また、メイン画面用のオブジェクトをメニューに引き渡して、メニュー処理から本体画面のバインド処理を行えるようにしています。( 別に分ける必要はありませんが、分けたとしたらこんな感じかと思います )

package winofsql.jp;

import gueei.binding.Binder;
import gueei.binding.app.BindingActivity;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.support.v4.app.NavUtils;

public class P1Activity extends BindingActivity  {

    private BindModel bindModel = new BindModel();
    private BindModelMenu bindModelMenu = new BindModelMenu(bindModel);
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        bindModel.button_value.set("ボタン");
        this.setAndBindRootView(R.layout.activity_p1, bindModel);
        this.setAndBindOptionsMenu(R.menu.activity_p1, bindModelMenu);
    }
    
}


メイン画面のバインド用クラス
package winofsql.jp;

import android.view.View;
import gueei.binding.Command;
import gueei.binding.observables.StringObservable;
import gueei.binding.pojo.PojoViewModel;
import gueei.binding.pojo.PojoViewModelHelper;

public class BindModel implements PojoViewModel {

    private PojoViewModelHelper helper = new PojoViewModelHelper();
    
    public final StringObservable hello = new StringObservable("こんにちは");
    public final StringObservable button_value = new StringObservable();

    public final Command Button_Click_1 = new Command() {
        @Override
        public void Invoke(View arg0, Object... arg1) {
        	hello.set("はじめまして");
        }
    };
    
    @Override
    public PojoViewModelHelper getHelper() {
        return helper;
    }
 
    @Override
    public void notifyPropertyChanged( String propertyName ) {
        helper.notifyPropertyChanged( propertyName );
    }

}


メニュー用のバインド用クラス
※ メイン画面用の中に書いて共用してもいいです
package winofsql.jp;

import android.view.View;
import gueei.binding.Command;
import gueei.binding.observables.StringObservable;
import gueei.binding.pojo.PojoViewModel;
import gueei.binding.pojo.PojoViewModelHelper;

public class BindModelMenu implements PojoViewModel {

    private PojoViewModelHelper helper = new PojoViewModelHelper();
	private BindModel bindModel;
    
    public final StringObservable bind_menu = new StringObservable("バインドメニュー");

    public final Command Menu_Click_1 = new Command() {
        @Override
        public void Invoke(View arg0, Object... arg1) {
        	BindModelMenu.this.bindModel.hello.set("メニューから変更");
        }
    };

	public BindModelMenu(BindModel bindModel) {
		this.bindModel = bindModel;
	}
 
    @Override
    public PojoViewModelHelper getHelper() {
        return helper;
    }
 
    @Override
    public void notifyPropertyChanged( String propertyName ) {
        helper.notifyPropertyChanged( propertyName );
    }

}


【Androidの最新記事】
posted by lightbox at 2012-09-21 14:25 | Android | このブログの読者になる | 更新情報をチェックする
container 終わり



フリーフォントで簡単ロゴ作成
フリーフォントでボタン素材作成
フリーフォントで吹き出し画像作成
フリーフォントではんこ画像作成
ほぼ自由に利用できるフリーフォント
フリーフォントの書体見本とサンプル
画像を大きく見る為のウインドウを開くボタンの作成

CSS ドロップシャドウの参考デモ
イラストAC
ぱくたそ
写真素材 足成
フリーフォント一覧
utf8 文字ツール
右サイド 終わり
base 終わり