前言

对于多菜单挑选,咱们先看两个竞品,分别是美团和携程的

竞品作用图
一站式筛选:Android自定义View多菜单封装解决方案之使用篇
一站式筛选:Android自定义View多菜单封装解决方案之使用篇
  • 美团切换的方案,是先封闭已展开的,封闭之后再展开新的,有点相当于有几个tab,就有几个Menu布局
  • 携程切换方案就比较生硬,没有动画,看起来很难过
作用图

一站式筛选:Android自定义View多菜单封装解决方案之使用篇

增加依赖

  • github的地址:github.com/Peakmain/Ba…
  • Step 1. Add the JitPack repository to your build file

Add it in your root build.gradle at the end of repositories:

allprojects {
    repositories {
        ...
        maven { url 'https://jitpack.io' }
    }
}
  • Step 2. Add the dependency
implementation 'com.github.Peakmain:BasicUI:+'
  • Step 3.some probleam

    假如你的gradle版本比3.5.3高,可能会呈现以下几个问题:

    1、Entry name ‘AndroidManifest.xml’ collided

    解决办法:在gradle.properties增加以下代码

    android.useNewApkCreator=false
    

    2、假如装置失利,用adb install装置报错提示如下

    failed to install app-debug.apk: Failure [INSTALL_PARSE_FAILED_UNEXPECTED_EXCEPTION: Failed to parse /data/app/vmdl1335956833.tmp/base.apk: Corrupt XML binary file]

    解决办法:在增加依赖的build.gradle中增加以下代码

    android{
        packagingOptions {
         exclude 'AndroidManifest.xml'
      }
    }
    

如何运用多菜单挑选

一、增加布局
 <com.peakmain.ui.widget.menu.ListMenuView
    android:id="@+id/list_data_screen_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />
二、设置适配器

适配器承继BaseListMenuAdapter,会重写三个办法:

  1. getTitleLayoutId: 设置标题布局
  2. getMenuLayoutId(int position):设置菜单布局,可根据不同position设置不同的布局
  3. setMenuContent(View menuView, final int position):设置菜单里边的内容。menu是每个方位的View

默许情况下,翻开菜单的时分,标题字体的色彩#01a8e3,封闭菜单的时分,标题字体的色彩为Color.BLACK,假如你想修改翻开菜单或许封闭菜单的时分标题款式,你能够重写以下两个办法

  1. openMenu(@NonNull View tabView)
  2. closeMenu(@NonNull View tabView)
1、getTitleLayoutId()设置标题布局
    @Override
    public int getTitleLayoutId() {
        return R.layout.ui_list_data_screen_tab;
    }
2、getMenuLayoutId(int position):设置菜单布局,可根据不同position设置不同的布局
    @Override
    protected int getMenuLayoutId(int position) {
        if (position == 0)//引荐排序
            return R.layout.layout_menu_recommend_sort;
        else if (position == 1 || position == 2)//酒店品牌和抢手地点
            return R.layout.layout_menu_brand;
        else
            //综合挑选
            return R.layout.layout_categorize_screen;
    }
3、setMenuContent(View menuView, final int position):设置菜单里边的内容
@Override
protected void setMenuContent(View menuView, final int position) {
    if (menuView == null) return;
    if (position == 0) {
        RecyclerView recyclerView = menuView.findViewById(R.id.recycler_view);
        recyclerView.setLayoutManager(new LinearLayoutManager(mContext));
        recyclerView.setAdapter(new MenuRecommendSortAdapter(mContext, mRecommendSortList));
    } else if (position == 1) {
        TextView tvBrandTitle = menuView.findViewById(R.id.tv_brand_title);
        tvBrandTitle.setText("品牌偏好");
        RecyclerView recyclerView = menuView.findViewById(R.id.recycler_view);
        recyclerView.setLayoutManager(new GridLayoutManager(mContext, 3));
        recyclerView.setAdapter(new MenuBrandAdapter(mContext, mBrandList));
    } else if (position == 2) {
        TextView tvBrandTitle = menuView.findViewById(R.id.tv_brand_title);
        tvBrandTitle.setText("抢手住宿地");
        RecyclerView recyclerView = menuView.findViewById(R.id.recycler_view);
        recyclerView.setLayoutManager(new GridLayoutManager(mContext, 3));
        recyclerView.setAdapter(new MenuHotCityAdapter(mContext, mCityList));
    } else {
         //综合挑选
        RecyclerView rvLeft = menuView.findViewById(R.id.rv_left);
        rvLeft.setLayoutManager(new LinearLayoutManager(mContext));
        MenuLeftRecyclerAdapter leftRecyclerAdapter =
                new MenuLeftRecyclerAdapter(mContext, mLeftMenuList);
        rvLeft.setAdapter(leftRecyclerAdapter);
        RecyclerView rvRight = menuView.findViewById(R.id.rv_right);
        rvRight.setLayoutManager(new LinearLayoutManager(mContext));
        rvRight.setAdapter(new MenuRightRecyclerAdapter(mContext, mCategoryRightBeans));
        LinearLayoutManager rvRightLayoutManager = (LinearLayoutManager) rvRight.getLayoutManager();
        leftRecyclerAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(int position) {
                int selectPosition = leftRecyclerAdapter.mSelectPosition;
                if (selectPosition == position) return;
                leftRecyclerAdapter.setSelectItem(position);
                rvRightLayoutManager
                        .scrollToPositionWithOffset(position, 0);
            }
        });
        rvRight.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                int firstVisibleItemPosition = rvRightLayoutManager.findFirstVisibleItemPosition();
                if (firstVisibleItemPosition != -1) {
                    rvLeft.smoothScrollToPosition(firstVisibleItemPosition);
                    leftRecyclerAdapter.setSelectItem(firstVisibleItemPosition);
                }
            }
        });
    }
}
4、重写翻开菜单时标题的款式
@Override
public void openMenu(@NonNull View tabView) {
    super.openMenu(tabView);
    ((ImageView)tabView.findViewById(R.id.iv_down)).setImageResource(R.drawable.ic_triangle_up);
}
5、重写封闭菜单时标题的款式
@Override
public void closeMenu(@NonNull View tabView) {
    TextView textView = tabView.findViewById(R.id.tv_menu_tab_title);
    textView.setTextColor(ContextCompat.getColor(mContext,R.color.color_272A2B));
    ((ImageView)tabView.findViewById(R.id.iv_down)).setImageResource(R.drawable.ic_triangle_down);
}
  • 完整的示例代码:ListMenuAdapter.java
三、设置adapter即可,需要将标题调集传进去
  mMenuView = findViewById(R.id.list_data_screen_view);
  mMenuView.setAdapter(adapter);//自己的适配器

总结

  • 上面中咱们首要介绍自定义View实现多菜单挑选的封装的运用,用户不需要关怀底层翻开菜单、封闭菜单和切换菜单的动画作用
  • 运用也是非常简略的,就三步,一、运用布局ListMenuView,二、为ListMenuView设置适配器,三、调用listMenuView的setAdapter将适配器和View进行绑定
  • 假如我们觉得有协助,欢迎我们点个小星星哦
  • 项目的github地址:github.com/Peakmain/Ba…