仿微信 发起群聊页面的搜索

作者 : 开心源码 本文共2938个字,预计阅读时间需要8分钟 发布时间: 2022-05-12 共227人阅读

第一次写文章,多多包涵。别的不多说,直接看效果

1550912724834.gif

仿照的

1550912289585.gif

顶部的搜索+选中时显示的头像,通过自己设置ViewGroup,RecycleView+EditText。

通过onMeasure测量,当EditText的长度小于指定的最小长度时,对RecycleView 的长度固定

 @Override    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {        super.onMeasure(widthMeasureSpec, heightMeasureSpec);        int count = getChildCount();        for (int i = 0; i < count; i++) {            View view = getChildAt(i);            if (view instanceof ViewGroup) {                childMeasure((ViewGroup) view);            }        }    }    private void childMeasure(ViewGroup viewGroup) {        int childCount = viewGroup.getChildCount();        for (int j = 0; j < childCount; j++) {            View child = viewGroup.getChildAt(j);            if (child == scRecycler) {                //假如子view 是RecycleView                int rootWidth = getMeasuredWidth(); //根View的长度                int width = scRecycler.getMeasuredWidth(); //当前RecycleView的长度                if (rootWidth - width < searchMinWidth) { //假如小于EditText指定的最小宽度                    setMeasureChildSpec(rootWidth - searchMinWidth, scRecycler.getMeasuredHeight(), scRecycler);                    setMeasureChildSpec(searchMinWidth, scEditQuery.getMeasuredHeight(), scEditQuery);                }                break;            } else {                if (child instanceof ViewGroup) {                    childMeasure((ViewGroup) child);                    break;                }            }        }    }    private void setMeasureChildSpec(int width, int height, View view) {        int measuredWidthSpec = MeasureSpec.makeMeasureSpec(width, MeasureSpec.EXACTLY);        int measuredHeightSpec = MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY);        view.measure(measuredWidthSpec, measuredHeightSpec);    }

搜索icon是通过监听 RecycleView.AdapterDataObserver的变化实现显示和隐藏

  private final RecyclerView.AdapterDataObserver adapterDataObserver = new RecyclerView.AdapterDataObserver() {        private void update() {            if (scRecycler != null && scRecycler.getAdapter() != null) {                boolean isShow = true;                if (scRecycler.getAdapter().getItemCount() == 0) {                    isShow = true;                    scRecycler.setVisibility(GONE);                } else {                    isShow = false;                    scRecycler.setVisibility(VISIBLE);                    scRecycler.scrollToPosition(scRecycler.getAdapter().getItemCount() - 1);                }                updateSearchIcon(isShow);            }        }        @Override        public void onChanged() {            super.onChanged();            update();        }        @Override        public void onItemRangeChanged(int positionStart, int itemCount) {            super.onItemRangeChanged(positionStart, itemCount);            update();        }        @Override        public void onItemRangeInserted(int positionStart, int itemCount) {            super.onItemRangeInserted(positionStart, itemCount);            update();        }        @Override        public void onItemRangeMoved(int fromPosition, int toPosition, int itemCount) {            super.onItemRangeMoved(fromPosition, toPosition, itemCount);            update();        }        @Override        public void onItemRangeRemoved(int positionStart, int itemCount) {            super.onItemRangeRemoved(positionStart, itemCount);            update();        }    };

第一次点击返回键头像颜色变浅,是通过Drawable tint着色实现的

 ImageView image = holder.findViewById(R.id.img_head);                Drawable drawable = image.getDrawable();                Drawable tintDrawable = DrawableCompat.wrap(drawable.getConstantState() == null ?                        drawable : drawable.getConstantState().newDrawable()).mutate();                DrawableCompat.setTintMode(tintDrawable, PorterDuff.Mode.SCREEN);                DrawableCompat.setTint(tintDrawable, Color.parseColor("#D9D9D9"));                image.setImageDrawable(tintDrawable);

需要注意 Drawable mutate,因为Android为了优化系统性能,同一个Drawable实例只会存一份在内存中,假如对Drawable进行修改,会导致全局的同一Drawable进行修改
不使用mutate 共享模式

image.png
使用mutate 不共享模式
image.png

项目地址

说明
1. 本站所有资源来源于用户上传和网络,如有侵权请邮件联系站长!
2. 分享目的仅供大家学习和交流,您必须在下载后24小时内删除!
3. 不得使用于非法商业用途,不得违反国家法律。否则后果自负!
4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解!
5. 如有链接无法下载、失效或广告,请联系管理员处理!
6. 本站资源售价只是摆设,本站源码仅提供给会员学习使用!
7. 如遇到加密压缩包,请使用360解压,如遇到无法解压的请联系管理员
开心源码网 » 仿微信 发起群聊页面的搜索

发表回复