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

  • 时间:2019-02-24 21:40 作者:路人乙丙丁_7cbc 来源:路人乙丙丁_7cbc 阅读:757
  • 扫一扫,手机访问
摘要:第一次写文章,多多包涵。别的不多说,直接看效果1550912724834.gif仿照的1550912289585.gif顶部的搜索+选中时显示的头像,通过自己设置ViewGroup,RecycleView+EditText。通过onMeasure测量,当EditText的长度小于指定的最小长度时,对R

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


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

项目地址

  • 全部评论(0)
手机二维码手机访问领取大礼包
返回顶部