android如何实现仿京东商品属性筛选功能

发布时间:2021-06-28 09:41:33 作者:小新
来源:亿速云 阅读:160

这篇文章将为大家详细讲解有关android如何实现仿京东商品属性筛选功能,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。

筛选和属性选择是目前非常常用的功能模块;几乎所有的APP中都会使用;

android如何实现仿京东商品属性筛选功能

点击筛选按钮会弹出一个自己封装好的popupWindow,实用方法非常简单;两行代码直接显示;(当然初始化数据除外)

这里和以前用到的流式布局有些不一样:流式布局

以前使用的是单个分类,而且也没有在项目中大量实用;这个筛选功能除了数据外几乎都是从项目中Copy出来的;

整个popupWindow布局就是一个自定义的ListView,这个自定义的listview主要是控制listview的高度;

如果数据少的话就是自适应,如果数据多了就限制高度为屏幕的一半;

自定义的ListView:

public class CustomHeightListView extends ListView { 
 
  private Context mContext; 
 
  public CustomHeightListView(Context context) { 
    this(context, null); 
  } 
 
  public CustomHeightListView(Context context, AttributeSet attrs) { 
    this(context, attrs, 0); 
  } 
 
  public CustomHeightListView(Context context, AttributeSet attrs, int defStyleAttr) { 
    super(context, attrs, defStyleAttr); 
    init(context); 
  } 
 
  private void init(Context context) { 
    mContext = context; 
  } 
 
  @Override 
  protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 
    try { 
      //最大高度显示为屏幕内容高度的一半 
      Display display = ((Activity) mContext).getWindowManager().getDefaultDisplay(); 
      DisplayMetrics d = new DisplayMetrics(); 
      display.getMetrics(d); 
      //设置控件高度不能超过屏幕高度一半(d.heightPixels / 2,下面有清空按钮所以再减200,也可随意换成自己想要的高度) 
      heightMeasureSpec = MeasureSpec.makeMeasureSpec(d.heightPixels / 2 - 200, MeasureSpec.AT_MOST); 
    } catch (Exception e) { 
      e.printStackTrace(); 
    } 
    //重新计算控件高、宽 
    super.onMeasure(widthMeasureSpec, heightMeasureSpec); 
  } 
}

ListView中每个item是一个流式布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
  android:layout_width="match_parent" 
  android:layout_height="wrap_content" 
  android:orientation="vertical"> 
 
  <TextView 
    android:id="@+id/tv_type_name" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" /> 
 
  <com.example.zheng.flowlayoutdemo.view.SkuFlowLayout 
    android:id="@+id/layout_property" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" /> 
   
</LinearLayout>

整个popupwindow都封装在一个类中,创建的时候只需把数据源传递过去即可,实用的时候直接show就可以了

flowPopWindow = new FlowPopWindow(MainActivity.this, dictList); 
    flowPopWindow.showAsDropDown(ivBack);

当点击确定的时候直接设置一个监听即可:

flowPopWindow.setOnConfirmClickListener(new FlowPopWindow.OnConfirmClickListener() { 
     @Override 
     public void onConfirmClick() { 
      StringBuilder sb = new StringBuilder(); 
      for (FiltrateBean fb : dictList) { 
       List<FiltrateBean.Children> cdList = fb.getChildren(); 
       for (int x = 0; x < cdList.size(); x++) { 
        FiltrateBean.Children children = cdList.get(x); 
        if (children.isSelected()) 
         sb.append(fb.getTypeName() + ":" + children.getValue() + ";"); 
       } 
      } 
      if (!TextUtils.isEmpty(sb.toString())) 
       Toast.makeText(MainActivity.this, sb.toString(), Toast.LENGTH_LONG).show(); 
     } 
    }

关于“android如何实现仿京东商品属性筛选功能”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,使各位可以学到更多知识,如果觉得文章不错,请把它分享出去让更多的人看到。

推荐阅读:
  1. web前端入门到实战:制作仿京东商城-商品列表商品筛选功能
  2. 原生js实现商品筛选功能

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

android

上一篇:Android如何实现仿网易严选底部弹出菜单效果

下一篇:android如何实现仿支付宝、微信密码输入框效果

相关阅读

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

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