프로그래밍/안드로이드

안드로이드 - 목록을 팝업으로 보여주는 ListPopupWindow

가카리 2015. 8. 23. 21:38
반응형

 

어떤 목록을 보여줄 때 팝업으로 보여주고 싶다면 ListPopupWindow 위젯을 사용해야한다.

팝업 메뉴와 유사하지만 목록을 어댑터로 받는 점이 특이하다.

 

void setWidth(int width)

void setHeight(int height)

void setContentWidth(int width)

 

폭과 높이는 픽셀 단위로 지정한다.

 

    void setAdapter(ListAdapter adapter)

    void setAnchorView(View anchor)

 

위의 메소드는 어댑터와 앵커 뷰를 지정한다.

 

    void setModal(boolean modal)

 

위의 메소드는 팝업 목록의 동작 방식을 지정한다. 모달일 때는 선택을 해도 팝업이 자동으로 닫히지 않으며 팝업 바깥을 눌러야 닫힌다.

 

    void setOnItemSelectedListener(AdapterView.OnItemSelectedListener selectedListener)

    void setOnItemClickListener(AdapterView.OnItemClickListener clickListener)

 

위 메소드를 통해서 선택 변경이나 클릭 동작에 대한 이벤트 리스너를 등록한다.

 

activity_list_popup_window_test.xml

 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

xmlns:tools="http://schemas.android.com/tools"

android:layout_width="match_parent"

android:layout_height="match_parent"

    android:orientation="vertical"

>

 

<Button

android:id="@+id/btn"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:onClick="mOnClick"

android:text="Button"/>

 

</LinearLayout>

 

 

ListPopupWindowTest.java

package com.example.ch13_lispopupwindow;

 

import android.app.Activity;

import android.graphics.Color;

import android.os.Bundle;

import android.view.View;

import android.widget.AdapterView;

import android.widget.ArrayAdapter;

import android.widget.Button;

import android.widget.ListPopupWindow;

 

public class ListPopupWindowTest extends Activity {

    Button mBtn;

    ListPopupWindow mList;

    public String[] Colors = {

        "Red", "Green", "Blue", "Yellow", "Cyan", "Magenta"    

    };

    

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_list_popup_window_test);

      

        mBtn = (Button)findViewById(R.id.btn);

        mList = new ListPopupWindow(this);

        mList.setWidth(300);

        mList.setHeight(300);

        mList.setAnchorView(mBtn);//버튼이 눌리면 리스트팝업윈도우가 나온다.

        mList.setAdapter(new ArrayAdapter<String>(this,

                android.R.layout.simple_list_item_1, Colors));//어댑터 연결

        mList.setModal(true);//선택을 해도 자동으로 팝업이 닫히지 않게 한다.

        

        mList.setOnItemClickListener(new AdapterView.OnItemClickListener() {

 

            @Override

            public void onItemClick(AdapterView<?> arg0, View arg1, int position,

                    long id) {

                // TODO Auto-generated method stub

                //선택된 값에 따라 버튼의 백그라운드 색을 바꾼다.

                switch(position){

                case 0:

                    mBtn.setBackgroundColor(Color.RED);

                    break;

                case 1:

                    mBtn.setBackgroundColor(Color.GREEN);

                    break;

                case 2:

                    mBtn.setBackgroundColor(Color.BLUE);

                    break;

                case 3:

                    mBtn.setBackgroundColor(Color.YELLOW);

                    break;

                case 4:

                    mBtn.setBackgroundColor(Color.CYAN);

                    break;

                case 5:

                    mBtn.setBackgroundColor(Color.MAGENTA);

                    break;

                }

                

            }

        

        });

    

    }

 

    public void mOnClick(View v){

        if(mList.isShowing()){

            mList.dismiss();

        }else{

            mList.show();

        }

    }

 

}

 

 

실행 화면

 

 

반응형