Android弹出菜单--PopupWindow

2018-07-06  本文已影响0人  颤抖的闪电

布局

按钮主界面

zuji_pop_more_widget.xml

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

    <ImageView
        android:layout_width="18sp"
        android:layout_height="18sp"
        android:layout_gravity="right"
        android:layout_marginBottom="-3sp"
        android:layout_marginRight="20dp"
        android:src="@drawable/mapbox_user_puck_icon" />

    <ListView
        android:id="@+id/lv_toptitle_menu"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginRight="10dp"
        android:background="@drawable/zuji_more_pop_window_bg" />
</LinearLayout>

zuji_pop_more_item.xml

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

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:orientation="horizontal">

        <ImageView
            android:id="@+id/iv_menu_item"
            android:layout_width="18dp"
            android:layout_height="18dp"
            android:layout_gravity="center_vertical"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="10dp"
            android:src="@drawable/zuji_lib_list_map_icon_first" />

        <TextView
            android:id="@+id/tv_menu_item"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:layout_marginRight="10dp"
            android:gravity="left"
            android:text="测试测试测试测试"
            android:textColor="#212121"
            android:textSize="14dp" />

    </LinearLayout>
</LinearLayout>

按钮动画
res/values/style.xml

<!--右上角点击更多-->
    <style name="PopupMoreAnimation" parent="android:Animation" mce_bogus="1">
        <item name="android:windowEnterAnimation">@anim/popup_enter</item>
        <item name="android:windowExitAnimation">@anim/popup_exit</item>
    </style>

弹出:
res/anim/popup_enter.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <scale
        android:duration="150"
        android:fromXScale="0.6"
        android:fromYScale="0.6"
        android:pivotX="50%"
        android:pivotY="50%"
        android:toXScale="1.0"
        android:toYScale="1.0" />
    <alpha
        android:duration="50"
        android:fromAlpha="0.0"
        android:interpolator="@android:anim/decelerate_interpolator"
        android:toAlpha="1.0" />
</set>

退出:
res/anim/popup_exit.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <scale
        android:duration="150"
        android:fromXScale="1.0"
        android:fromYScale="1.0"
        android:pivotX="50%"
        android:pivotY="50%"
        android:toXScale="0.5"
        android:toYScale="0.5" />
    <alpha
        android:duration="150"
        android:fromAlpha="1.0"
        android:interpolator="@android:anim/accelerate_interpolator"
        android:toAlpha="0.0" />
</set>

自定义MorePopwindow

import android.app.Activity;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Adapter;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.tantu.module.common.utils.LanguageChecker;
import com.zuji.entrance.adapter.ZujiContentAdapter;
import com.zuji.entrance.datamodel.ZujiContentDM;
import com.zuji.entrance.fragment.ZujiContentFragment;
import com.zuji.entrance.fragment.ZujiMainFragment;
import com.zuji.library.R;
import com.zuji.library.ZujiManager;
import com.zuji.library.permission.ThirdPermissionGuideActivity;
import com.zuji.library.react.IMainFramework;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by fangyc on 2018/7/4.
 */

public class MorePopwindow extends PopupWindow {
    Activity mContext;
    private View conentView;
    private ListView lvContent;
    private MyAdapter myAdapter;

    public MorePopwindow(Activity context, List<MenuPopwindowBean> list) {
        this.mContext = context;
        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        conentView = inflater.inflate(R.layout.zuji_pop_more_widget, null);

        lvContent = (ListView) conentView.findViewById(R.id.lv_toptitle_menu);
        myAdapter = new MyAdapter(context, list);
        lvContent.setAdapter(myAdapter);

//        int h = context.getWindowManager().getDefaultDisplay().getHeight();
        int w = context.getWindowManager().getDefaultDisplay().getWidth();

        // 设置SelectPicPopupWindow的View
        this.setContentView(conentView);
        // 设置SelectPicPopupWindow弹出窗体的宽
        this.setWidth(w / 3 + 30);
//        this.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        // 设置SelectPicPopupWindow弹出窗体的高
        this.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        // 设置SelectPicPopupWindow弹出窗体可点击
        this.setFocusable(true);
        this.setOutsideTouchable(true);
        // 刷新状态
        this.update();
        // 实例化一个ColorDrawable颜色为半透明
        ColorDrawable dw = new ColorDrawable(0000000000);
        // 点back键和其他地方使其消失,设置了这个才能触发OnDismisslistener ,设置其他控件变化等操作
        this.setBackgroundDrawable(dw);
        // 设置SelectPicPopupWindow弹出窗体动画效果
//        this.setAnimationStyle(R.style.PopupMoreAnimation);
    }


    public MyAdapter getMyAdapter() {
        return myAdapter;
    }

    public void setOnItemClick(AdapterView.OnItemClickListener myOnItemClickListener) {
        lvContent.setOnItemClickListener(myOnItemClickListener);
    }

    /**
     *
     * @param fragment
     * @param view
     * @param gray_layout 黑色蒙层
     */
    public static void showMainPageMore(final ZujiMainFragment fragment, View view, final View gray_layout) {
        gray_layout.setVisibility(View.VISIBLE);
        int[] icons;
        Resources resources = fragment.getActivity().getResources();
        final String feedback = resources.getString(R.string.feedback);
        final String tracker_switch = resources.getString(R.string.tracker_switch);
        final String tracker_permissions = resources.getString(R.string.tracker_permissions);
        String[] texts;
        if (LanguageChecker.isZH()) {
            icons = new int[]{R.drawable.zuji_more_feedback, R.drawable.zuji_more_setting_switch, R.drawable.zuji_more_setting_switch};
            texts = new String[]{feedback, tracker_switch, tracker_permissions,};
        } else {
            icons = new int[]{R.drawable.zuji_more_setting_switch, R.drawable.zuji_more_setting_switch};
            texts = new String[]{tracker_switch, tracker_permissions};
        }

        List<MorePopwindow.MenuPopwindowBean> list = new ArrayList<>(texts.length);
        MorePopwindow.MenuPopwindowBean bean = null;
        for (int i = 0; i < icons.length; i++) {
            bean = new MorePopwindow.MenuPopwindowBean();
            bean.setIcon(icons[i]);
            bean.setText(texts[i]);
            list.add(bean);
        }
        final MorePopwindow morePopwindow = new MorePopwindow(fragment.getActivity(), list);
        morePopwindow.setOnDismissListener(new OnDismissListener() {
            @Override
            public void onDismiss() {
                gray_layout.setVisibility(View.GONE);
                morePopwindow.dismiss();
            }
        });
        morePopwindow.setOnItemClick(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                                    int position, long id) {
                Adapter adapter = parent.getAdapter();
                MorePopwindow.MenuPopwindowBean thisBean = (MorePopwindow.MenuPopwindowBean) adapter.getItem(position);
                String selected = thisBean.getText();

                if (selected.equals(feedback)) {
                    trackFeedBackItemClicked(true);
                } else if (selected.equals(tracker_switch)) {
                    IMainFramework mainFramework = ZujiManager.getInstance().getMainFramework();
                    if (mainFramework != null) {
                        mainFramework.goTrackSwitchFragment(fragment.getActivity());
                    }
                } else if (selected.equals(tracker_permissions)) {
                    ThirdPermissionGuideActivity.startActivityFromFragment(fragment);
                }
                morePopwindow.dismiss();
            }

        });
        morePopwindow.showPopupWindow(view);
    }

    /**
     * @param fragment
     * @param view
     * @param titlebarView 用于点击"管理/删除"时,设置标题栏右上角"完成"
     * @param gray_layout  黑色蒙层
     * @param mAdapter     用于点击"管理/删除"时,刷新界面
     */
    public static void showContentPageMore(final ZujiContentFragment fragment, View view, final HeaderBarView titlebarView, final View gray_layout, final ZujiContentAdapter mAdapter) {
        gray_layout.setVisibility(View.VISIBLE);
        int[] icons;
        Resources resources = fragment.getActivity().getResources();
        final String feedback = resources.getString(R.string.feedback);
        final String manager = resources.getString(R.string.manager);
        final String hometown = resources.getString(R.string.hometown);
        final String tracker_switch = resources.getString(R.string.tracker_switch);
        final String tracker_permissions = resources.getString(R.string.tracker_permissions);
        String[] texts;
        if (LanguageChecker.isZH()) {
            icons = new int[]{R.drawable.zuji_more_feedback, R.drawable.zuji_more_manage_delete, R.drawable.zuji_more_hometown, R.drawable.zuji_more_setting_switch, R.drawable.zuji_more_setting_switch};
            texts = new String[]{feedback, manager, hometown, tracker_switch, tracker_permissions};
        } else {
            icons = new int[]{R.drawable.zuji_more_manage_delete, R.drawable.zuji_more_hometown, R.drawable.zuji_more_setting_switch, R.drawable.zuji_more_setting_switch};
            texts = new String[]{manager, hometown, tracker_switch, tracker_permissions};
        }

        List<MorePopwindow.MenuPopwindowBean> list = new ArrayList<>(texts.length);
        MorePopwindow.MenuPopwindowBean bean = null;
        for (int i = 0; i < icons.length; i++) {
            bean = new MorePopwindow.MenuPopwindowBean();
            bean.setIcon(icons[i]);
            bean.setText(texts[i]);
            list.add(bean);
        }
        final MorePopwindow morePopwindow = new MorePopwindow(fragment.getActivity(), list);
        morePopwindow.setOnDismissListener(new OnDismissListener() {
            @Override
            public void onDismiss() {
                gray_layout.setVisibility(View.GONE);
                morePopwindow.dismiss();
            }
        });
        morePopwindow.setOnItemClick(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                                    int position, long id) {

                Adapter adapter = parent.getAdapter();
                MorePopwindow.MenuPopwindowBean thisBean = (MorePopwindow.MenuPopwindowBean) adapter.getItem(position);
                String selected = thisBean.getText();
                if (selected.equals(feedback)) {
                    trackFeedBackItemClicked(true);
                } else if (selected.equals(manager)) {
                    ZujiContentDM.setEditing(true);//设置成编辑状态
                    mAdapter.notifyDataSetChanged();
                    titlebarView.setRightText(fragment.getActivity().getResources().getString(R.string.done));
                    titlebarView.setRightDrawableVisibility(View.GONE);
                    titlebarView.setRightTextOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            ZujiContentDM.setEditing(false);//设置成非编辑状态
                            mAdapter.notifyDataSetChanged();
                            titlebarView.setRightText("");
                            titlebarView.setRightDrawableVisibility(View.VISIBLE);
                        }
                    });
                } else if (selected.equals(hometown)) {
                    IMainFramework mainFramework = ZujiManager.getInstance().getMainFramework();
                    if (mainFramework != null) {
                        mainFramework.goZujiHometownFragment(fragment.getActivity());
                    }
                } else if (selected.equals(tracker_switch)) {
                    IMainFramework mainFramework = ZujiManager.getInstance().getMainFramework();
                    if (mainFramework != null) {
                        mainFramework.goTrackSwitchFragment(fragment.getActivity());
                    }
                } else if (selected.equals(tracker_permissions)) {
                    ThirdPermissionGuideActivity.startActivityFromFragment(fragment);
                }
                morePopwindow.dismiss();
            }

        });
        morePopwindow.showPopupWindow(view);
    }


    /**
     * 用户反馈/小调查
     *
     * @param dataNotEmpty
     */
    public static void trackFeedBackItemClicked(boolean dataNotEmpty) {
        if (dataNotEmpty) {
            //去调查问卷
            IMainFramework mainFramework = ZujiManager.getInstance().getMainFramework();
            if (mainFramework != null) {
                mainFramework.goZujiFeedback();
            }
        } else {
            //去探途的反馈页面
            IMainFramework mainFramework = ZujiManager.getInstance().getMainFramework();
            if (mainFramework != null) {
                mainFramework.goTanTuFeedback();
            }
        }
    }

    static class MyAdapter extends BaseAdapter {
        private List<MenuPopwindowBean> list;
        private LayoutInflater inflater;

        public MyAdapter(Context context, List<MenuPopwindowBean> list) {
            inflater = LayoutInflater.from(context);
            this.list = list;
        }

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

        @Override
        public Object getItem(int position) {
            return list.get(position);
        }

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

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            Holder holder = null;
            if (convertView == null) {
                convertView = inflater.inflate(R.layout.zuji_pop_more_item, null);
                holder = new Holder();
                holder.imgItem = convertView.findViewById(R.id.iv_menu_item);
                holder.tvItem = convertView.findViewById(R.id.tv_menu_item);
                convertView.setTag(holder);
            } else {
                holder = (Holder) convertView.getTag();
            }
            holder.imgItem.setImageResource(list.get(position).getIcon());
            holder.tvItem.setText(list.get(position).getText());
            return convertView;
        }

        static class Holder {
            ImageView imgItem;
            TextView tvItem;

            public ImageView getImgItem() {
                return imgItem;
            }

            public TextView getTvItem() {
                return tvItem;
            }
        }
    }

    /**
     * 显示popupWindow
     *
     * @param parent
     */
    private void showPopupWindow(View parent) {
        if (!this.isShowing()) {
            // 以下拉方式显示popupwindow
            this.showAsDropDown(parent);
        } else {
            this.dismiss();
        }
    }


    public static class MenuPopwindowBean {
        private int icon;
        private String text;

        public int getIcon() {
            return icon;
        }

        public void setIcon(int icon) {
            this.icon = icon;
        }

        public String getText() {
            return text;
        }

        public void setText(String text) {
            this.text = text;
        }
    }
}

    public static class MenuPopwindowBean {
        private int icon;
        private String text;

        public int getIcon() {
            return icon;
        }

        public void setIcon(int icon) {
            this.icon = icon;
        }

        public String getText() {
            return text;
        }

        public void setText(String text) {
            this.text = text;
        }
    }
}

在Activity.java中调用

MorePopwindow.showMainPageMore(ZujiMainFragment.this, view, gray_layout);

黑色背景:

 View gray_layout;//黑色背景遮罩层
 gray_layout = view.findViewById(R.id.gray_layout);
<!--黑色背景遮罩层,平时隐藏 -->
    <View
        android:id="@+id/gray_layout"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:background="#66000000"
        android:visibility="gone"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

感谢:
Android开发学习(14)Popupwindow右上角弹出菜单

上一篇下一篇

猜你喜欢

热点阅读