zoukankan      html  css  js  c++  java
  • Android:实现仿 美团/淘宝 多级分类菜单效果

    本例要实现的是诸如美团/淘宝/百度糯米 多级分类菜单效果。当分类数量许多时能够考虑採用两级分类。而诸如美团这样的表现方式是一个不错的选择。

    首先上效果图:

        


    主要代码:

    1. PopupWindow初始化过程:

    popupWindow = new PopupWindow(this);
            View view = LayoutInflater.from(this).inflate(R.layout.popup_layout, null);
            leftLV = (ListView) view.findViewById(R.id.pop_listview_left);
            rightLV = (ListView) view.findViewById(R.id.pop_listview_right);
    
            popupWindow.setContentView(view);
            popupWindow.setBackgroundDrawable(new PaintDrawable());
            popupWindow.setFocusable(true);
    
            popupWindow.setHeight(ScreenUtils.getScreenH(this) * 2 / 3);
            popupWindow.setWidth(ScreenUtils.getScreenW(this));
    
            popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
                @Override
                public void onDismiss() {
                    darkView.startAnimation(animOut);
                    darkView.setVisibility(View.GONE);
    
                    leftLV.setSelection(0);
                    rightLV.setSelection(0);
                }
            });

    2.左側菜单点击事件:

    //左側ListView点击事件
            leftLV.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?

    > parent, View view, int position, long id) { //二级数据 List<SecondClassItem> list2 = firstList.get(position).getSecondList(); //假设没有二级类,则直接跳转 if (list2 == null || list2.size() == 0) { popupWindow.dismiss(); int firstId = firstList.get(position).getId(); String selectedName = firstList.get(position).getName(); handleResult(firstId, -1, selectedName); return; } FirstClassAdapter adapter = (FirstClassAdapter) (parent.getAdapter()); //假设上次点击的就是这一个item,则不进行不论什么操作 if (adapter.getSelectedPosition() == position){ return; } //依据左側一级分类选中情况,更新背景色 adapter.setSelectedPosition(position); adapter.notifyDataSetChanged(); //显示右側二级分类 updateSecondListView(list2, secondAdapter); } });


    3. 右側菜单点击事件:

    //右側ListView点击事件
            rightLV.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    //关闭popupWindow,显示用户选择的分类
                    popupWindow.dismiss();
    
                    int firstPosition = firstAdapter.getSelectedPosition();
                    int firstId = firstList.get(firstPosition).getId();
                    int secondId = firstList.get(firstPosition).getSecondList().get(position).getId();
                    String selectedName = firstList.get(firstPosition).getSecondList().get(position)
                            .getName();
                    handleResult(firstId, secondId, selectedName);
                }
            });

    4.顶部标签点击事件(即显示/隐藏 分类菜单)

     if (popupWindow.isShowing()) {
                popupWindow.dismiss();
            } else {
                popupWindow.showAsDropDown(findViewById(R.id.main_div_line));
                popupWindow.setAnimationStyle(-1);
                //背景变暗
                darkView.startAnimation(animIn);
                darkView.setVisibility(View.VISIBLE);
            }

    5.依据左側点击,刷新右側ListView

    //刷新右側ListView
        private void updateSecondListView(List<SecondClassItem> list2,
                                          SecondClassAdapter secondAdapter) {
            secondList.clear();
            secondList.addAll(list2);
            secondAdapter.notifyDataSetChanged();
        }

    源代码下载(免积分哦):

    http://download.csdn.net/detail/books1958/7992863      
       

  • 相关阅读:
    tomcat使用不同的jdk版本 liunx 装两个jdk
    接下来自己的研究对象
    钉钉小程序开发的所有坑
    java 在web应用中获取本地目录和服务器上的目录不一致的问题
    Python2.7更新pip:UnicodeDecodeError: 'ascii' codec can't decode byte 0xb7 in position 7: ordinal not in range(128)
    vue项目中禁止移动端双击放大,双手拉大放大的方法
    JZ56 删除链表中重复的结点
    JZ55 链表中环的入口结点
    JZ54 字符流中第一个不重复的字符
    JZ53 表示数值的字符串
  • 原文地址:https://www.cnblogs.com/mfrbuaa/p/5220255.html
Copyright © 2011-2022 走看看