栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Android侧滑+菜单筛选多选、单选(多类型筛选,类似京东)+显示在ToolBar下方,ToolBar联动DrawerLayout

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Android侧滑+菜单筛选多选、单选(多类型筛选,类似京东)+显示在ToolBar下方,ToolBar联动DrawerLayout

这种UI电商比较常见,我这个是在侧滑界面写的,效果图先看:



布局侧滑写了两个,一左一右,左边是单选,右边是多选



    
    

    
        
            
                
                    
                    
                    
                    
                    
                    
                    
                
            

        
        
            
                
                    
                    
                    
                    
                    
                    
                    
                
            

        
    

自己模拟一个实体类

public  class CeHuaBean{
    private oneTypeGroup oneGroupEntity;
    private TwoTypeGroup TwoGroupEntity;
    private TreeTypeGroup TreeGroupEntity;

    public CeHuaBean(oneTypeGroup oneGroupEntity, TwoTypeGroup twoGroupEntity, TreeTypeGroup treeGroupEntity) {
        this.oneGroupEntity = oneGroupEntity;
        TwoGroupEntity = twoGroupEntity;
        TreeGroupEntity = treeGroupEntity;
    }

    public oneTypeGroup getoneGroupEntity() {
        return oneGroupEntity;
    }

    public void setoneGroupEntity(oneTypeGroup oneGroupEntity) {
        this.oneGroupEntity = oneGroupEntity;
    }

    public TwoTypeGroup getTwoGroupEntity() {
        return TwoGroupEntity;
    }

    public void setTwoGroupEntity(TwoTypeGroup twoGroupEntity) {
        TwoGroupEntity = twoGroupEntity;
    }

    public TreeTypeGroup getTreeGroupEntity() {
        return TreeGroupEntity;
    }

    public void setTreeGroupEntity(TreeTypeGroup treeGroupEntity) {
        TreeGroupEntity = treeGroupEntity;
    }

    public static class GroupEntity {
        private List groupItemList;

        public GroupEntity(List groupItemList) {
            this.groupItemList = groupItemList;
        }
    }


    public static class GroupItemListDTO {
        private String code;
        private String name;
        private boolean isSelect;

        public GroupItemListDTO(String code, String name) {
            this.code = code;
            this.name = name;
        }

        public String getCode() {
            return code;
        }

        public void setCode(String code) {
            this.code = code;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public boolean isSelect() {
            return isSelect;
        }

        public void setSelect(boolean select) {
            isSelect = select;
        }
    }

    public static class OneTypeGroup{
        private  GroupEntity groupEntity;
        private String groupName;

        public oneTypeGroup(CeHuaBean.GroupEntity groupEntity, String groupName) {
            this.groupEntity = groupEntity;
            this.groupName = groupName;

        }

        public GroupEntity getGroupEntity() {
            return groupEntity;
        }

        public void setGroupEntity(GroupEntity groupEntity) {
            this.groupEntity = groupEntity;
        }

        public String getGroupName() {
            return groupName;
        }

        public void setGroupName(String groupName) {
            this.groupName = groupName;
        }
    }
    public static class TwoTypeGroup{
        private  GroupEntity groupEntity;
        private String groupName;

        public TwoTypeGroup(CeHuaBean.GroupEntity groupEntity, String groupName) {
            this.groupEntity = groupEntity;
            this.groupName = groupName;
        }
        public GroupEntity getGroupEntity() {
            return groupEntity;
        }

        public void setGroupEntity(GroupEntity groupEntity) {
            this.groupEntity = groupEntity;
        }

        public String getGroupName() {
            return groupName;
        }

        public void setGroupName(String groupName) {
            this.groupName = groupName;
        }
    }
    public static class TreeTypeGroup{
        private  GroupEntity groupEntity;
        private String groupName;

        public TreeTypeGroup(CeHuaBean.GroupEntity groupEntity, String groupName) {
            this.groupEntity = groupEntity;
            this.groupName = groupName;
        }
        public GroupEntity getGroupEntity() {
            return groupEntity;
        }

        public void setGroupEntity(GroupEntity groupEntity) {
            this.groupEntity = groupEntity;
        }

        public String getGroupName() {
            return groupName;
        }

        public void setGroupName(String groupName) {
            this.groupName = groupName;
        }
    }
}

这个单选的Adapter

public class CeHuaCommonAdapterOnly extends RecyclerView.Adapter {
    private final Context mContext;
    private final List mData;
    private  List a;

    public CeHuaCommonAdapterOnly(Context context, List ceHuaBeans) {
        this.mContext = context;
        this.mData = ceHuaBeans;
        this.a = ceHuaBeans;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View inflate = LayoutInflater.from(mContext).inflate(R.layout.cehua_item, viewGroup, false);
        ViewHolder viewHolder = new ViewHolder(inflate);

        return viewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull CeHuaCommonAdapterOnly.ViewHolder viewHolder, int i) {
        viewHolder.tv.setText(mData.get(i).getName());
        isSelectfun(i, viewHolder.tv);
        viewHolder.itemView.setonClickListener(new View.onClickListener() {
            @Override
            public void onClick(View v) {
                for (int j = 0; j 

多选Adapter

public class CeHuaCommonAdapter extends RecyclerView.Adapter{
   private final Context mContext;
    private final List mData;

    public CeHuaCommonAdapter(Context context, List ceHuaBeans){
        this.mContext=context;
        this.mData = ceHuaBeans;
    }
    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull  ViewGroup viewGroup, int i) {
        View inflate = LayoutInflater.from(mContext).inflate(R.layout.cehua_item, viewGroup ,false);
        ViewHolder viewHolder=new ViewHolder(inflate);

        return viewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull CeHuaCommonAdapter.ViewHolder viewHolder, int i) {
            viewHolder.tv.setText(mData.get(i).getName());
            viewHolder.itemView.setonClickListener(new View.onClickListener() {
                @Override
                public void onClick(View v) {
                    if(mData.get(i).isSelect()){
                        viewHolder.tv.setBackgroundResource(R.drawable.filter_item_bg);
                        mData.get(i).setSelect(false);
                    }else{
                        viewHolder.tv.setBackgroundResource(R.drawable.filter_item_selected_bg);
                        mData.get(i).setSelect(true);
                    }
                }
            });


    }

    @Override
    public int getItemCount() {
        return mData.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        private TextView tv;
        public ViewHolder(View itemView){
            super(itemView);
            tv = itemView.findViewById(R.id.itemText);
        }
    }
}

主要Activity,主要写的模拟数据了

public class CeHuaActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ce_hua);
        //右侧多选菜单
        TextView tv1 = findViewById(R.id.tv1);
        TextView tv2 = findViewById(R.id.tv2);
        TextView tv3 = findViewById(R.id.tv3);
        RecyclerView recy1 = findViewById(R.id.recy1);
        RecyclerView recy2 = findViewById(R.id.recy2);
        RecyclerView recy3 = findViewById(R.id.recy3);
        Toolbar toolbar = findViewById(R.id.toolbar);
        DrawerLayout draw = findViewById(R.id.draw);
        //模拟数据
        List oneList = new ArrayList<>();
        oneList.add(new CeHuaBean.GroupItemListDTO("1", "星座型"));
        oneList.add(new CeHuaBean.GroupItemListDTO("2", "水瓶座"));
        oneList.add(new CeHuaBean.GroupItemListDTO("3", "摩羯座"));
        oneList.add(new CeHuaBean.GroupItemListDTO("4", "天秤座"));

        CeHuaBean.oneTypeGroup oneTypeGrop = new CeHuaBean.oneTypeGroup(new CeHuaBean.GroupEntity(oneList), "标题1");

        List twoList = new ArrayList<>();
        twoList.add(new CeHuaBean.GroupItemListDTO("1", "血型"));
        twoList.add(new CeHuaBean.GroupItemListDTO("2", "A型"));
        twoList.add(new CeHuaBean.GroupItemListDTO("3", "B型"));
        twoList.add(new CeHuaBean.GroupItemListDTO("4", "AB型"));

        CeHuaBean.TwoTypeGroup twoTypeGrop = new CeHuaBean.TwoTypeGroup(new CeHuaBean.GroupEntity(twoList), "标题2");
        //模拟数据
        List treeList = new ArrayList<>();
        treeList.add(new CeHuaBean.GroupItemListDTO("1", "学历型"));
        treeList.add(new CeHuaBean.GroupItemListDTO("2", "本科"));
        treeList.add(new CeHuaBean.GroupItemListDTO("3", "研究生"));
        treeList.add(new CeHuaBean.GroupItemListDTO("4", "硕士"));
        CeHuaBean.TreeTypeGroup treeTypeGrop = new CeHuaBean.TreeTypeGroup(new CeHuaBean.GroupEntity(treeList), "标题3");
        //模拟数据
        CeHuaBean ceHuaBean = new CeHuaBean(oneTypeGrop, twoTypeGrop, treeTypeGrop);
        tv1.setText(ceHuaBean.getoneGroupEntity().getGroupName());
        tv2.setText(ceHuaBean.getTwoGroupEntity().getGroupName());
        tv3.setText(ceHuaBean.getTreeGroupEntity().getGroupName());
        RecyclerView.LayoutManager layoutManager = new GridLayoutManager(this, 3);
        CeHuaCommonAdapter ceHuaCommonAdapter = new CeHuaCommonAdapter(this, oneList);
        recy1.setLayoutManager(layoutManager);
        recy1.setAdapter(ceHuaCommonAdapter);

        //布局管理器  及适配器
        RecyclerView.LayoutManager layoutManager2 = new GridLayoutManager(this, 3);
        CeHuaCommonAdapter ceHuaCommonAdapter2 = new CeHuaCommonAdapter(this, twoList);
        recy2.setLayoutManager(layoutManager2);
        recy2.setAdapter(ceHuaCommonAdapter2);

        RecyclerView.LayoutManager layoutManager3 = new GridLayoutManager(this, 3);
        CeHuaCommonAdapter ceHuaCommonAdapter3 = new CeHuaCommonAdapter(this, treeList);
        recy3.setLayoutManager(layoutManager3);
        recy3.setAdapter(ceHuaCommonAdapter3);

        //左侧单选菜单
        TextView tvLeft1 = findViewById(R.id.tvleft1);
        TextView tvLeft2 = findViewById(R.id.tvleft2);
        TextView tvLeft3 = findViewById(R.id.tvleft3);
        RecyclerView recyLeft1 = findViewById(R.id.recyleft1);
        RecyclerView recyLeft2 = findViewById(R.id.recyleft2);
        RecyclerView recyLeft3 = findViewById(R.id.recyleft3);
        //模拟数据
        List oneLeftList = new ArrayList<>();
        oneLeftList.add(new CeHuaBeanLeft.GroupItemListDTO("1", "星座型"));
        oneLeftList.add(new CeHuaBeanLeft.GroupItemListDTO("2", "水瓶座"));
        oneLeftList.add(new CeHuaBeanLeft.GroupItemListDTO("3", "摩羯座"));
        oneLeftList.add(new CeHuaBeanLeft.GroupItemListDTO("4", "天秤座"));

        CeHuaBeanLeft.oneTypeGroup oneLeftTypeGrop = new CeHuaBeanLeft.oneTypeGroup(new CeHuaBeanLeft.GroupEntity(oneLeftList), "标题1");

        List twoLeftList = new ArrayList<>();
        twoLeftList.add(new CeHuaBeanLeft.GroupItemListDTO("5", "血型"));
        twoLeftList.add(new CeHuaBeanLeft.GroupItemListDTO("6", "A型"));
        twoLeftList.add(new CeHuaBeanLeft.GroupItemListDTO("7", "B型"));
        twoLeftList.add(new CeHuaBeanLeft.GroupItemListDTO("8", "AB型"));

        CeHuaBeanLeft.TwoTypeGroup twoLeftTypeGrop = new CeHuaBeanLeft.TwoTypeGroup(new CeHuaBeanLeft.GroupEntity(twoLeftList), "标题2");
        //模拟数据
        List treeLeftList = new ArrayList<>();
        treeLeftList.add(new CeHuaBeanLeft.GroupItemListDTO("9", "学历型"));
        treeLeftList.add(new CeHuaBeanLeft.GroupItemListDTO("10", "本科"));
        treeLeftList.add(new CeHuaBeanLeft.GroupItemListDTO("11", "研究生"));
        treeLeftList.add(new CeHuaBeanLeft.GroupItemListDTO("12", "硕士"));
        CeHuaBeanLeft.TreeTypeGroup treeLeftTypeGrop = new CeHuaBeanLeft.TreeTypeGroup(new CeHuaBeanLeft.GroupEntity(treeLeftList), "标题3");
        //模拟数据
        CeHuaBeanLeft ceHuaBeanLeft = new CeHuaBeanLeft(oneLeftTypeGrop, twoLeftTypeGrop, treeLeftTypeGrop);
        tvLeft1.setText(ceHuaBeanLeft.getoneGroupEntity().getGroupName());
        tvLeft2.setText(ceHuaBeanLeft.getTwoGroupEntity().getGroupName());
        tvLeft3.setText(ceHuaBeanLeft.getTreeGroupEntity().getGroupName());
        RecyclerView.LayoutManager layoutManagerLeft = new GridLayoutManager(this, 3);
        CeHuaCommonAdapterOnly ceHuaCommonAdapterLeft = new CeHuaCommonAdapterOnly(this, oneLeftList);
        recyLeft1.setLayoutManager(layoutManagerLeft);
        recyLeft1.setAdapter(ceHuaCommonAdapterLeft);

        //布局管理器  及适配器
        RecyclerView.LayoutManager layoutManagerLeft2 = new GridLayoutManager(this, 3);
        CeHuaCommonAdapterOnly ceHuaCommonAdapterLeft2 = new CeHuaCommonAdapterOnly(this, twoLeftList);
        recyLeft2.setLayoutManager(layoutManagerLeft2);
        recyLeft2.setAdapter(ceHuaCommonAdapterLeft2);

        RecyclerView.LayoutManager layoutManagerLeft3 = new GridLayoutManager(this, 3);
        CeHuaCommonAdapterOnly ceHuaCommonAdapterLeft3 = new CeHuaCommonAdapterOnly(this, treeLeftList);
        recyLeft3.setLayoutManager(layoutManagerLeft3);
        recyLeft3.setAdapter(ceHuaCommonAdapterLeft3);


        //toolbar
        toolbar.setTitle("菜单筛选");
        setSupportActionBar(toolbar);
        ActionBarDrawerToggle mActionBarDrawerToggle = new ActionBarDrawerToggle(this, draw, toolbar, R.string.com_sswl_open_drawer, R.string.com_sswl_close_drawer);
        draw.addDrawerListener(mActionBarDrawerToggle);
        mActionBarDrawerToggle.syncState();


    }
}

选中背景及未选中背景

选择的



    
    
    

未选择的



    
    


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/356984.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号