下拉菜单(二) PopupWindow 封装实现

上次写的简单粗暴的下拉菜单,写来玩玩的,非常简单粗暴,如果封装起来,可以实现多级联动的二级菜单(有待下一篇来做)。若是没看过的亲,可以去看看,简单粗暴 下拉菜单 dropdownMenu 

如果直接把上次的下拉菜单嵌入到工程里面,会产生一大堆重复的代码。于是,有了此篇下拉菜单(有借鉴到他人所写的popwindow),是用PopupWindow封装实现的,可嵌入到工程里面,跑起来。

好了,话不在多,代码才是硬道理。


xml:就一自定义下拉菜单

<?xml version="1.0" encoding="utf-8"?>
<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"
    tools:context=".SampleActivity">

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

        <LinearLayout
            android:id="@+id/title_back_ll"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:clickable="true"
            android:orientation="horizontal">


            <TextView
                android:id="@+id/title_text"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:paddingLeft="10dp"
                android:gravity="center"
                android:textSize="19sp"
                android:text="132" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_gravity="center"
            android:layout_weight="1"
            android:orientation="horizontal">

            <com.example.yjy.mydropdownmenubutton.DropdownMenuButton
                android:id="@+id/dm_dropdown"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:gravity="center_horizontal|center_vertical"
                android:layout_gravity="center" />
        </LinearLayout>

        <LinearLayout
            android:id="@+id/title_ll_modify"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:clickable="true"
            android:layout_gravity="center"
            android:orientation="horizontal">

            <TextView
                android:id="@+id/title_tv_modify"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_margin="10dp"
                android:layout_gravity="center"
                android:textSize="18sp"
                android:gravity="center_vertical|center_horizontal"
                android:text="132"/>
        </LinearLayout>

    </LinearLayout>

    <View
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="#ccc"/>

    <TextView
        android:id="@+id/textContent"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:textSize="18sp"
        tools:text="内容显示区域"/>

</LinearLayout>


java:就辣么几行

public class MainActivity extends Activity {
    String title_text;
    ListViewAdapter adapter;
    final String[] CARD_RECORD = new String[]{ "123","456", "789" };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        final DropdownMenuButton menu1 = (DropdownMenuButton) findViewById(R.id.dm_dropdown);
        final TextView textContent = (TextView) findViewById(R.id.textContent);

        SharedPreferences sharedPreferences = getSharedPreferences("123456", MODE_PRIVATE);
        String title_string = sharedPreferences.getString("title_text","");
        if (!TextUtils.isEmpty(title_string)){
            title_text = title_string;
        }else {
            title_text = CARD_RECORD[0];
        }
        refrushData(menu1, title_text);

        menu1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                title_text = CARD_RECORD[position];
                textContent.setText(String.format("你选择了:%s", title_text));
                SharedPreferences sharedPreferences = getSharedPreferences("123456", MODE_PRIVATE);
                SharedPreferences.Editor editor = sharedPreferences.edit();
                editor.putString("title_text", title_text);
                menu1.setTitle(title_text);
                editor.commit();
                refrushData(menu1, title_text);
            }
        });
    }

    private void refrushData(DropdownMenuButton menu1,String title_string){
        menu1.setTitle(title_string);
        adapter = new ListViewAdapter(this, Arrays.asList(CARD_RECORD),title_text);
        menu1.setAdapter(adapter);
    }
}

主要的是在封装的view里面,部分代码  java:

 private void init(Context context, AttributeSet attrs) {
        mContext = context;
        String titleText;
//        TypedArray attributes = getContext().obtainStyledAttributes(attrs, R.styleable.DropdownMenu);
//        String titleText = attributes.getString(R.styleable.DropdownMenu_titleText);
//        float textSize = attributes.getDimensionPixelSize(R.styleable.DropdownMenu_titleTextSize, 0);
//        final int textColor = attributes.getColor(R.styleable.DropdownMenu_titleColor, 0xff000000);
//        int titleBgColor = attributes.getColor(R.styleable.DropdownMenu_titleBgColor, 0x00ffffff);
//
//        int listBgColor = attributes.getColor(R.styleable.DropdownMenu_listBgColor, 0x00ffffff);
//
//        final int highLightColor = attributes.getColor(R.styleable.DropdownMenu_titleHighLight, NO_HIGHLIGHT);

        mIconView = new ImageView(mContext);

//        attributes.recycle();

        setLayoutParams(new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
//        setBackgroundColor(titleBgColor);
        setGravity(Gravity.CENTER);

        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View popupWindow = inflater.inflate(R.layout.ddm_popup, (ViewGroup) getParent(), false);

        mPopupWindow = new PopupWindow(popupWindow, LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT, true);
        mPopupWindow.setAnimationStyle(R.style.PopupAnimation);
        mPopupWindow.setTouchable(true);
        mPopupWindow.setOutsideTouchable(true);

        //noinspection deprecation
        mPopupWindow.setBackgroundDrawable(new BitmapDrawable());

        mListView = (ListView) popupWindow.findViewById(R.id.lv_menu);
//        mListView.setBackgroundColor(listBgColor);
        mListView.setVisibility(GONE);
        mListView.setAdapter(mDropdownAdapter = new ListViewAdapter(mContext, Arrays.asList(new String[]{"Empty"}), ""));

        mShadowLayout = (RelativeLayout) popupWindow.findViewById(R.id.rl_menu_shadow);
        mShadowLayout.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                mPopupWindow.dismiss();
            }
        });
        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (mItemClickListener != null) {
                    mItemClickListener.onItemClick(parent, view, position, id);
                }
                mTextTitle.setText(mDropdownAdapter.getTitleString(position));
                mPopupWindow.dismiss();
            }
        });

        mTextTitle = new TextView(mContext);

        LayoutParams titleParams = new LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        titleParams.addRule(CENTER_IN_PARENT, TRUE);
        mTextTitle.setLayoutParams(titleParams);

//        mTextTitle.setText(TextUtils.isEmpty(titleText) ? "<请选择>" : titleText);
//        mTextTitle.setTextColor(textColor);
//        mTextTitle.setBackgroundColor(titleBgColor);
        mTextTitle.setPadding(20, 0, 72, 0);
        mTextTitle.setEllipsize(TextUtils.TruncateAt.END);
        mTextTitle.setGravity(Gravity.CENTER);
//        if (textSize > 0) {
//            mTextTitle.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize);
//        }

        LayoutParams iconParams = new LayoutParams(
                ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        iconParams.addRule(ALIGN_PARENT_RIGHT, TRUE);
        iconParams.addRule(CENTER_VERTICAL, TRUE);
        mIconView.setLayoutParams(iconParams);
        mIconView.setImageResource(R.drawable.sun_submenu_up_normal);
        mIconView.setPadding(20, 12, 32, 10);

        addView(mTextTitle);
        addView(mIconView);

        mPopupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                mIconView.setImageResource(R.drawable.sun_submenu_down_normal);
//                mTextTitle.setTextColor(textColor);
            }
        });

        super.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mPopupWindow.isShowing()) {
                    mPopupWindow.dismiss();
                    mIconView.setImageResource(R.drawable.sun_submenu_down_normal);
//                    mTextTitle.setTextColor(textColor);
                } else {
                    mPopupWindow.showAsDropDown(DropdownMenuButton.this);
                    mPopupWindow.setOutsideTouchable(true);
                    showMenu();
                    mIconView.setImageResource(R.drawable.sun_submenu_up_normal);
//                    if (highLightColor != -1) {
//                        mTextTitle.setTextColor(highLightColor);
//                    }
                }
                if (mSecondClickListener != null) {
                    mSecondClickListener.onClick(DropdownMenuButton.this);
                }
            }
        });
    }

需要完整代码的亲,请点击下载(无需积分) demo

觉得有用的话,请点个赞,happy coding!

有神马问题,请大家留言讨论。一起学习,一起进步。

  • 4
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值