1. 程式人生 > >基於PopWindow+Listview實現彈窗選擇的介面實現

基於PopWindow+Listview實現彈窗選擇的介面實現

最近在Android專案中用到了基於彈窗(PopWIndow)+ Listview結合的彈窗選項框的介面。

1. MainActivity

package com.steven.popwindowtest;

import android.app.ActionBar;
import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;

public class MainActivity extends Activity implements View.OnClickListener {
    private LinearLayout llGridCode;
    private TextView tvGridCode;
    private ImageView ivGridCode;
    private Context mContext;
    private PopupWindow mPopupWindow;
    private ListView lvPopWindowList;
    //資料來源
    private String[] countryGridCode = {"Test A", "Test B", "Test C", "Test D", "Test E", "Test F", "Test G", "Test H", "Test I"};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_grid_param_setting);
        mContext = this;
        llGridCode = (LinearLayout) findViewById(R.id.ll_grid_code);
        tvGridCode = (TextView) findViewById(R.id.tv_grid_code);
        ivGridCode = (ImageView) findViewById(R.id.iv_grid_code);
        llGridCode.setOnClickListener(this);        //設定監聽器,彈出彈窗
        initPopupWindow();                          //初始化PopWindow,給彈窗設定ListView
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.ll_grid_code:
                showPopWindow(llGridCode);          //顯示彈窗
        }
    }

    /**
     * Description: 初始化PopWindow,以及給PopWindow中的ListView設定adapter,以及Item監聽
     */
    private void initPopupWindow() {
        View view;
        view = LayoutInflater.from(mContext).inflate(R.layout.grid_code_pop_window_listview, null);
        lvPopWindowList = (ListView) view.findViewById(R.id.lv_pop_window);
        lvPopWindowList.setDivider(null);                                                       //取消ListView分隔線
        lvPopWindowList.setVerticalScrollBarEnabled(false);                                     //隱藏側滑欄
        lvPopWindowList.setAdapter(new GridCodePopWindowAdapter(countryGridCode, mContext));    //給彈窗ListView設定adapter

        //設定Item監聽
        lvPopWindowList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                //點選彈出彈框
                if (mPopupWindow != null && mPopupWindow.isShowing()) {
                    mPopupWindow.dismiss();
                }
                tvGridCode.setText(countryGridCode[position]);      
            }
        });

        mPopupWindow = new PopupWindow(view, ActionBar.LayoutParams.WRAP_CONTENT, ActionBar.LayoutParams.WRAP_CONTENT, true);   //生成PopWindow
        mPopupWindow.setOutsideTouchable(true);
        /** 為其設定背景,使得其內外焦點都可以獲得 */
        mPopupWindow.setBackgroundDrawable(new ColorDrawable());
        mPopupWindow.setFocusable(true);
    }

    //設定彈窗基於標題欄的顯示位置
    public void showPopWindow(View view) {
        //popupwindow相對view位置x軸偏移量
        View viewTemp = mPopupWindow.getContentView();
        viewTemp.measure(0, 0);
        int width = viewTemp.getMeasuredWidth();
        int xOffset = (view.getWidth() - width) / 2;
        mPopupWindow.showAsDropDown(view, xOffset, 0);
    }
}
2. GridCodePopWindowAdapter 介面卡
package com.steven.popwindowtest;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

public class GridCodePopWindowAdapter extends BaseAdapter {
    private String[] countryGridCode;
    private Context context;

    public GridCodePopWindowAdapter(String[] countryGridCode, Context context) {
        this.countryGridCode = countryGridCode;
        this.context = context;
    }

    @Override
    public int getCount() {
        return countryGridCode == null ? 0 : countryGridCode.length;
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        View view;
        /*  快取子佈局檔案中的控制元件物件*/
        if (convertView == null) {
            view = LayoutInflater.from(context).inflate(R.layout.grid_code_pop_window_listview_item, null, false);
            holder = new ViewHolder();
            holder.tvCountryGridCode = (TextView) view.findViewById(R.id.tv_pop_window_item);
            view.setTag(holder);
        }
        //快取已滑入ListView中的item view
        else {
            view = convertView;
            holder = (ViewHolder) view.getTag();
        }
        
        holder.tvCountryGridCode.setText(countryGridCode[position]);

        return view;
    }

    class ViewHolder {
        TextView tvCountryGridCode;
    }
}


3.