基于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.基于PopWindow+Listview实现弹窗选择的界面实现_第1张图片

你可能感兴趣的:(Android初步)