ExpandGrideView打开与隐藏效果

因项目中用到九宫格展开与隐藏的功能,利用recyclerView简单的实现了下。
效果

expandgrideview_01.png
expandgrideview_02.png

MainActivity

 private void initView() {
        recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
    }
    private void initData() {
        List<CategoryEntity.ChildEntity> childList = new ArrayList<>();
        CategoryEntity.ChildEntity childEntity1 = new CategoryEntity.ChildEntity();
        childEntity1.setId("1");
        childEntity1.setName("child1");
        CategoryEntity.ChildEntity childEntity2 = new CategoryEntity.ChildEntity();
        childEntity2.setId("2");
        childEntity2.setName("child2");
        CategoryEntity.ChildEntity childEntity3 = new CategoryEntity.ChildEntity();
        childEntity3.setId("3");
        childEntity3.setName("child3");
        CategoryEntity.ChildEntity childEntity4 = new CategoryEntity.ChildEntity();
        childEntity4.setId("4");
        childEntity4.setName("child4");
        CategoryEntity.ChildEntity childEntity5 = new CategoryEntity.ChildEntity();
        childEntity5.setId("5");
        childEntity5.setName("child5");
        CategoryEntity.ChildEntity childEntity6 = new CategoryEntity.ChildEntity();
        childEntity6.setId("6");
        childEntity6.setName("child6");
        CategoryEntity.ChildEntity childEntity7 = new CategoryEntity.ChildEntity();
        childEntity7.setId("7");
        childEntity7.setName("child7");
        childList.add(childEntity1);
        childList.add(childEntity2);
        childList.add(childEntity3);
        childList.add(childEntity4);
        childList.add(childEntity5);
        childList.add(childEntity6);
        childList.add(childEntity7);
        CategoryEntity categoryEntity = new CategoryEntity();
        categoryEntity.setId("1");
        categoryEntity.setName("标题1");
        categoryEntity.setChild(childList);
        List<CategoryEntity> mList = new ArrayList<>();//数据太多数组中只加一组数据了
        mList.add(categoryEntity);
        MainAdapter adapter = new MainAdapter(this,mList);
        recyclerView.setAdapter(adapter);
    }

MainAdapter代码

    private List<CategoryEntity> mList;
    private Context mContext;
    public MainAdapter(Context context, List<CategoryEntity> list){
        this.mContext = context;
        this.mList = list;
    }
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_main,parent,false);
        ViewHolder vh = new ViewHolder(view);
        return vh;
    }
    @Override
    public void onBindViewHolder(final ViewHolder holder, int position) {
        final CategoryEntity entity = getItem(position);
        holder.tvTitle.setText(entity.getName());
        final MyGrideAdapter myGrideAdapter =  initGridView(holder,entity);
        holder.tvMore.setText(entity.showMore? "收起":"更多");
        holder.ivArrow.setImageResource(entity.showMore? R.mipmap.arrow_up:R.mipmap.arrow_down);
        holder.llMore.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                entity.showMore = !entity.showMore;
                myGrideAdapter.setShowMore(entity.showMore);
                holder.tvMore.setText(entity.showMore?"收起":"更多");
                holder.ivArrow.setImageResource(entity.showMore?R.mipmap.arrow_up:R.mipmap.arrow_down);
                notifyItemChanged(mList.indexOf(entity));
            }
        });
    }
    private MyGrideAdapter initGridView(ViewHolder holder, CategoryEntity entity) {
        List<CategoryEntity.ChildEntity> child = entity.getChild();
        MyGrideAdapter myGrideAdapter = new MyGrideAdapter(entity);
        holder.childGrideView.setAdapter(myGrideAdapter);
        return myGrideAdapter;
    }
    @Override
    public int getItemCount() {
        return mList==null? 0:mList.size();
    }
    public CategoryEntity getItem(int position){
        if(mList!=null){
           return  mList.get(position);
        }
        return null;
    }
    class ViewHolder extends RecyclerView.ViewHolder{
        private final TextView tvTitle,tvMore;

        private CustomGridView childGrideView;
        private final ImageView ivArrow;

        private final LinearLayout llMore;

        public ViewHolder(View itemView) {
            super(itemView);

            tvTitle = (TextView)itemView.findViewById(R.id.tv_title);
            tvMore = (TextView)itemView.findViewById(R.id.tv_more);
            ivArrow = (ImageView)itemView.findViewById(R.id.iv_arrow);
            childGrideView = (CustomGridView)itemView.findViewById(R.id.cgv_child);
            llMore = (LinearLayout)itemView.findViewById(R.id.ll_more);

        }
    }

    class MyGrideAdapter extends BaseAdapter{

        public boolean showMore = false;
        private List<CategoryEntity.ChildEntity> mChildList;

        public MyGrideAdapter(CategoryEntity entity){
            showMore = entity.showMore;
            this.mChildList = entity.getChild();
        }

        @Override
        public int getCount() {
            return mChildList == null ? 0 : (showMore ? mChildList.size() : (mChildList.size() > 3 ? 3 : mChildList.size()));
        }

        @Override
        public Object getItem(int position) {
            return mChildList.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            if(convertView==null){
                convertView= new TextView(parent.getContext());
                ((TextView)convertView).setHeight(getPixByDp(40, parent.getContext()));
                ((TextView)convertView).setPadding(getPixByDp(16,parent.getContext()),0,0,0);
                ((TextView)convertView).setGravity(Gravity.CENTER_VERTICAL);
                ((TextView)convertView).setTextSize(16f);
                ((TextView)convertView).setSingleLine();
                ((TextView)convertView).setEllipsize(TextUtils.TruncateAt.END);
                Drawable drawable =parent.getContext().getResources().getDrawable(R.drawable.divider_line);
                drawable.setBounds(0,0,getPixByDp(1,parent.getContext()),getPixByDp(20,parent.getContext()));
                if(position%3 == 2){
                    ((TextView) convertView).setCompoundDrawables(null,null, null,null);
                }else{
                    ((TextView) convertView).setCompoundDrawables(null,null, drawable,null);
                }
            }
            ((TextView) convertView).setText(mChildList.get(position).getName());
            return convertView;
        }

        public void setShowMore(boolean showMore) {
            this.showMore = showMore;
            this.notifyDataSetChanged();
        }
    }

    /**
     * 根据dp获取pix值
     * @param dp
     * @param context
     * @return
     */
    public static int getPixByDp(int dp,Context context){
        return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, context.getResources().getDisplayMetrics());
    }

CustomGrideView代码

public class CustomGridView extends GridView {

    public CustomGridView(Context context) {
        super(context);
    }

    public CustomGridView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public CustomGridView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

     /**  
     * 设置不滚动  
     */  
    public void onMeasure(int widthMeasureSpec, int heightMeasureSpec)   
    {   
        int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2,
                MeasureSpec.AT_MOST);
        super.onMeasure(widthMeasureSpec, expandSpec);   
  
    }   

}

CategoryEntity代码

/**
     * child : [{"icon":"","id":"2541","name":"立体车库","url":"http://m.3158.17kb.com/qy/liticheku/"},{"icon":"","id":"2555","name":"洗车","url":"http://m.3158.17kb.com/qy/xiche/"},{"icon":"","id":"2569","name":"汽车配件","url":"http://m.3158.17kb.com/qy/qichepeijian/"},{"icon":"","id":"2583","name":"汽修产品","url":"http://m.3158.17kb.com/qy/qixiuchanpin/"},{"icon":"","id":"2597","name":"汽车外饰及配件","url":"http://m.3158.17kb.com/qy/qichewaishipeijian/"},{"icon":"","id":"2611","name":"汽车改装","url":"http://m.3158.17kb.com/qy/qichegaizhuang/"},{"icon":"","id":"2625","name":"影音导航","url":"http://m.3158.17kb.com/qy/yingyindaohang/"},{"icon":"","id":"2639","name":"汽车内饰用品","url":"http://m.3158.17kb.com/qy/qicheneishiyongpin/"},{"icon":"","id":"2653","name":"座垫脚垫","url":"http://m.3158.17kb.com/qy/zuodianjiaodian/"},{"icon":"","id":"2667","name":"汽车防爆膜","url":"http://m.3158.17kb.com/qy/qichefangbaomo/"},{"icon":"","id":"2681","name":"车载电器","url":"http://m.3158.17kb.com/qy/chezaidianqi/"},{"icon":"","id":"2695","name":"汽车美容养护","url":"http://m.3158.17kb.com/qy/qichemeirongyanghu/"},{"icon":"","id":"2709","name":"儿童安全座椅","url":"http://m.3158.17kb.com/qy/dongtonganquanzuoyi/"},{"icon":"","id":"2723","name":"便捷式GPS导航","url":"http://m.3158.17kb.com/qy/cheyonggpsdaohang/"},{"icon":"","id":"2737","name":"行车记录仪","url":"http://m.3158.17kb.com/qy/xingchejiluyi/"},{"icon":"","id":"2751","name":"车载摄像头","url":"http://m.3158.17kb.com/qy/chezaishexiangtou/"},{"icon":"","id":"2765","name":"智能后视镜导航","url":"http://m.3158.17kb.com/qy/houshijingdaohang/"},{"icon":"","id":"2779","name":"汽车改装件","url":"http://m.3158.17kb.com/qy/qichegaizhuangjian/"},{"icon":"","id":"2806","name":"汽保工具套装","url":"http://m.3158.17kb.com/qy/qibaogongjutaozhuang/"},{"icon":"","id":"2819","name":"维修工具","url":"http://m.3158.17kb.com/qy/weixiugongju/"},{"icon":"","id":"2831","name":"车用轮胎","url":"http://m.3158.17kb.com/qy/cheyongluntai/"},{"icon":"","id":"2843","name":"自驾装备","url":"http://m.3158.17kb.com/qy/zijiazhuangbei/"},{"icon":"","id":"2854","name":"车用汽油添加剂","url":"http://m.3158.17kb.com/qy/cheyongqiyoutianjiaji/"},{"icon":"","id":"2863","name":"汽车清洁工具","url":"http://m.3158.17kb.com/qy/qicheqingjiegongju/"},{"icon":"","id":"2871","name":"其他","url":"http://m.3158.17kb.com/qy/qitaqiyefuwu/"}]
     * id : 2527
     * name : 汽车服务
     */

    private String id;
    private String name;
    public boolean showMore;
    /**
     * id : 2541
     * name : 立体车库
     */

    private List<ChildEntity> child;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

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

    public List<ChildEntity> getChild() {
        return child;
    }

    public void setChild(List<ChildEntity> child) {
        this.child = child;
    }

    public static class ChildEntity implements Serializable {
        private String id;
        private String name;

        public String getId() {
            return id;
        }

        public void setId(String id) {
            this.id = id;
        }

        public String getName() {
            return name;
        }

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

divider_line代码

<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">

    <size android:width="1dp" android:height="15dp"/>
    <solid android:color="#f5f5f5"/>

</shape>

所用到的图片以及边距大小可以自由替换。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 204,445评论 6 478
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 85,889评论 2 381
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 151,047评论 0 337
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,760评论 1 276
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,745评论 5 367
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,638评论 1 281
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 38,011评论 3 398
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,669评论 0 258
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,923评论 1 299
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,655评论 2 321
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,740评论 1 330
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,406评论 4 320
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,995评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,961评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,197评论 1 260
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 45,023评论 2 350
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,483评论 2 342

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 171,431评论 25 707
  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 12,016评论 4 62
  • 转载:我是天佑@所有汁友大家好:接触汁法五月份大概就一年啦。感慨万千!我本是一个乳癌患者、化了八个疗、又做了一年的...
    五盛缘老爹排毒养生阅读 1,050评论 0 6
  • “没有想过,会是这个结果,在你离开之后,没有留下什么,然后感到失落,痛也不能开口,在知道结果的最后还能说什么。明白...
    波多黎各阅读 225评论 0 0
  • 左午是谁阅读 232评论 0 4