ListPopupWindow のサンプルを云々してみた

ええと、ICS 本のサンプルコードは http://www.impressjapan.jp/books/3174 から download 可能ということで 1.4.5 の ListPopupWindow のサンプルの動作確認をしてみた。
サンプルは単一のプロジェクトになっており、確認のためプロジェクトを作って一つ一つコピィする形で作ってみました。
基本的には書籍に載っている

  • res/layout/main.xml
  • res/values/styles.xml
  • res/anim/rolldown.xml
  • res/anim/rollup.xml
  • アクティビティクラスのソースファイル

を追加乃至修正すれば良い形でした。

今気になっていること

ええと、Android Design にあったリストビューの右端に三角背景なボタンがあって、それを押したらリストというかメニュなナニが popup されて、というサンプル。
以下な検証必要なのかどうなのか。

  • 同じ onClick を持ってる OnClickListener 実装なクラスを渡して問題ないか
    • 特に onItemClick でアンカービューが渡されてそれに依る処理が記述可能なのかどうか
  • リストの右端にボタンがあって押したら右端にリストが表示されるような記述はどんな形にすべきなのか

これはちょっと色々確認したいですね。とは言え今日はスデにへろへろなので別途ということで。あと ViewPager で swipe 云々も確認したいですが、これは土曜日になるのかどうなのか。
あとは以下にソースを引用しときます。基本的には ICS なプロジェクトを作って以下なナニを追加乃至修正で動くはず。

res/layout/main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <Button
        android:id="@+id/showpopup"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Show ListPopupWindow1" />

    <Button
        android:id="@+id/showpopup2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Show ListPopupWindow2" />

</LinearLayout>

これ、そのまんまですね。

res/values/styles.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
    
    <style name="ListPopupWindowAnimation" parent="@android:style/Animation">
        <item name="android:windowEnterAnimation">@anim/rolldown</item>
        <item name="android:windowExitAnimation">@anim/rollup</item>
    </style>
    
</resources>

こちら、style タグの name 属性でナニモノか、が決まってるらしく自分の環境ではファイル名が sytyles.xml になってました (滝汗
こちらも中身は変更してません。

res/anim/rolldown.xml
<scale xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="200"
    android:fromXScale="1.0"
    android:fromYScale="0.0"
    android:interpolator="@android:anim/linear_interpolator"
    android:pivotX="50%"
    android:pivotY="0%"
    android:toXScale="1.0"
    android:toYScale="1.0" />

こちらもそのまんまです。別途 interpolator という属性について確認 (というか他の属性も同様ですが) しておく必要あり。

res/anim/rollup.xml
<scale xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="200"
    android:fromXScale="1.0"
    android:fromYScale="1.0"
    android:interpolator="@android:anim/linear_interpolator"
    android:pivotX="50%"
    android:pivotY="0%"
    android:toXScale="1.0"
    android:toYScale="0.0" />
アクティビティクラスのソースファイル
package jp.shuri.yamanetoshi.listpopupwindowsample;

import jp.shuri.yamanetoshi.listpopupwindowsample.R;
import android.app.Activity;
import android.graphics.Color;
import android.graphics.drawable.GradientDrawable;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListPopupWindow;
import android.widget.Toast;

public class ListPopupWindowSampleActivity extends Activity implements OnClickListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        findViewById(R.id.showpopup).setOnClickListener(this);
        findViewById(R.id.showpopup2).setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        
        // Adapter を用意
        String[] data = {"data1", "data2", "data3", "data4"};
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, data);
        
        // ListPopupWindow を生成
        final ListPopupWindow lpw = new ListPopupWindow(this);
        
        // Adapter をセット
        lpw.setAdapter(adapter);
        
        // リストのアイテムがタップされたときのリスナーをセット
        lpw.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> adapter, View v, int position, long id) {
                String s = (String)adapter.getItemAtPosition(position);
                Toast.makeText(ListPopupWindowSampleActivity.this, s, Toast.LENGTH_SHORT).show();
                lpw.dismiss();
            }
        });
        
        // アンカービューをセット
        lpw.setAnchorView(v);
        
        if(v.getId() == R.id.showpopup2) {
            // ポップアップを表示する位置の水平方向のオフセット
            lpw.setHorizontalOffset(20);

            // ポップアップを表示する位置の垂直方向のオフセット
            lpw.setVerticalOffset(20);
            
            // ポップアップを表示するときのアニメーションスタイルをセット
            lpw.setAnimationStyle(R.style.ListPopupWindowAnimation);
            
            // グラデーションの drawable を生成
            GradientDrawable d = new GradientDrawable(GradientDrawable.Orientation.BR_TL, new int[] {Color.BLACK, Color.DKGRAY});
            
            // 背景画像をセット
            lpw.setBackgroundDrawable(d);
            
            // プロンプトビューを用意
            ImageView iv = new ImageView(this);
            iv.setImageResource(R.drawable.ic_launcher);
            
            // プロンプトビューをセット
            lpw.setPromptView(iv);
            
            // プロンプトビューの位置をセット
            lpw.setPromptPosition(ListPopupWindow.POSITION_PROMPT_ABOVE);
        }
        
        // ポップアップを表示
        lpw.show();
    }
}