温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Android 仿京东侧滑筛选实例代码

发布时间:2020-09-22 21:11:11 来源:脚本之家 阅读:401 作者:一花一朝 栏目:移动开发

简单介绍

这个demo写的是仿京东的侧滑筛选页面,点击进入筛选后进入二级筛选,两次侧滑的筛选,还包括ListView+CheckBox滑动冲突,ListView+ GridView显示一行问题解决,接口回调传递数据等

效果图

Android 仿京东侧滑筛选实例代码

Android 仿京东侧滑筛选实例代码

简单得代码介绍

1.首页侧滑用的是安卓官方V4包中的DrawerLayout

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:id="@+id/drawer_layout"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:fitsSystemWindows="true">

  <FrameLayout
    android:id="@+id/main_content"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true">

    <TextView
      android:id="@+id/screenTv"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_gravity="center|top"
      android:layout_marginTop="200dp"
      android:text="仿京东筛选"
      android:textSize="20sp" />
  </FrameLayout>

  <LinearLayout
    android:id="@+id/nav_view"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="end"
    android:fitsSystemWindows="true"
    android:orientation="vertical" />

</android.support.v4.widget.DrawerLayout>

2.一级页面是自定义的layout,作为DrawerLayout的侧滑页面添加进去

menuHeaderView = new RightSideslipLay(ScreeningActivity.this);
navigationView.addView(menuHeaderView);

发现的一个小的技巧想要侧滑不随滑动而滑动,只能点击才出现侧滑的话,可以先锁定
drawer.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED, Gravity.RIGHT);这样就不一直跟着手势侧滑了

3.一级界面 ListVIew嵌套GridView,GridView得做自设配高度的自定义,不然只能显示一行,具体参源码AutoMeasureHeightGridView这个类

4.接下来是解析数据绑定数据了,还算比较简单。定义模型类,京东的筛选默认每项显示数据3个即一行。我现在默认的是都是关闭的,只有第一项是打开的,默认显现9个即3行,点击查看更多就能进入下一级页面。参看图二

5.二级页面是一个PopupWindow,设置了PopupWindow的位置和动画达到,也能像一级界面也样,右边侧滑出来,点击侧滑收回去的效果。

/**

 * 创建PopupWindow
 */
private PopupWindow mMenuPop;
public RightSideslipChildLay mDownMenu;

protected void initPopuptWindow(List<AttrList.Attr.Vals> mSelectData) {
  mDownMenu = new RightSideslipChildLay(getContext(), ValsData, mSelectData);
  if (mMenuPop == null) {
    mMenuPop = new PopupWindow(mDownMenu, LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.FILL_PARENT);
  }
  mMenuPop.setBackgroundDrawable(new BitmapDrawable());
  mMenuPop.setAnimationStyle(R.style.popupWindowAnimRight);
  mMenuPop.setFocusable(true);
  mMenuPop.showAtLocation(RightSideslipLay.this, Gravity.TOP, 100, UiUtils.getStatusBarHeight(mCtx));
  mMenuPop.setOnDismissListener(new PopupWindow.OnDismissListener() {

    @Override
    public void onDismiss() {
      dismissMenuPop();
    }
  });
}

此页面是一个ListView里包含CheckBox,对于CheckBox滑动选中错位的问题在这个demo中也有解决,此法一本万利。可以下载demo来参考。大体思路是CheckBox选中的状态对象的存在需要显示的对象里,设置对象的一个属性,记录CheckBox选中的状态。

6.对于页面件数据的传递使用的接口回调。包括Adapter数据操作的传出和一级页面传入二级页面,二级页面在回传给一级页面显示。一级页面在传出给主页面(这个没有写)也可以用其他数据传递的方式,这只是方法之一。

7.项目下载地址:AndroidScreening_jb51.rar

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持亿速云。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI