点赞处理的一些小技巧

1,953 阅读4分钟

今天来讲一些平时用的小技巧,然后由此引申一些其他的小技巧。

点赞是我们平时业务开发最常接触的,但如何更投巧的去做这块,还需要细细的开动小脑筋,下面会有一些和平时开发的对比,慢慢品味吧

连续点赞网络请求处理

在我们处理点赞业务中,主要处理“赞”图片的翻转和结果的网络请求,按照我们常规操作的话,每一次点击,都会把结果告知服务器,操作伪代码如下:

    boolean zanFlag = false;

    public void clickZan(View view) {
        if (zanFlag) {
            imgZan.setImageResource(R.drawable.zan_normal);
        } else {
            imgZan.setImageResource(R.drawable.zan_focus);
        }
        zanFlag = !zanFlag;
        //网络请求操作
        requestNet();
    }

这么看似乎是没什么问题,业务处理也是可行的,然后我们在网络请求这个部分做个日志打印,模拟网络请求,然后来看下图:

当我们在开始部分缓慢的进行点赞时,似乎是没有任何问题的,后面我开始疯狂点击,不断的产生网络的请求。假设,如何我疯狂连续点击100次,最终的结果只是“赞了”或是“没赞”,也就是只有2个结果,为了这两个结果,我们需要做100次网络请求,实数浪费。

从产品设计思路来思考,对于连续请求的处理,我们完全可以延时等待用户停止疯狂点击,然后将最后一次的结果作为最终结果再来上传处理,避免了在疯狂点击时频繁请求。

顺着这个思路来思考编码,我们需要一个定时,即在用户点击该按钮时,延时这个定时等待用户是否还会继续点击,如果用户在这个延时内还继续点击,则取消之前的定时,重新发送一个定时,直到用户停止点击了,延时也到了,则在延时到了的回调中去处理网络请求,这样,可以省了很多无用网络请求,接下来,我们开始编码

    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            //网络请求操作
            requestNet();
        }
    };

    boolean zanFlag = false;
    private static final int DELAY_MESSAGE = 0x0429;
    private static final int delayTime = 500;
    
    public void clickZan(View view) {

        if (zanFlag) {
            imgZan.setImageResource(R.drawable.zan_normal);
        } else {
            imgZan.setImageResource(R.drawable.zan_focus);

        }
        zanFlag = !zanFlag;
        handler.removeMessages(DELAY_MESSAGE);
        handler.sendEmptyMessageDelayed(DELAY_MESSAGE,delayTime);
    }

这里我使用了handler来做延时的处理,每次点击时,先把handler的 DELAY_MESSAGE 先移除了,然后重新发送延时 DELAY_MESSAGE 消息,延时时间设置了500毫秒,如果在500毫秒内疯狂点击,在handler消息还未到达时,早已通过removeMessage将之前的消息给移除掉了,通过这样的一个小技巧,我们来看看效果:

在我频的繁操作下,也就是最后一次有效,这样避免了之前频繁的无效操作。

通过这个例子,开始延伸,想到了我们平时用到的搜索,接下来讲搜索

实时搜索网络请求

有的业务情况是在EditText中实时搜索,显示网络返回的结果,对于搜索,一般都是给EditText添加TextWatcher监听,在onTextChanged回调中实时获取当前EditText的内容,然后请求网络,这里的问题也是和上面的问题一样,因为在用户输入文本的时候,TextWatcher会实时回调,回调十分频繁,我们只想在用户确认搜索结果时,用户不在继续输入了,然后再去请求网络,这里的处理也是和上面一样,通过延时的方式来解决频繁的操作。
伪代码

 editText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
               
                handler.removeMessages(DELAY_MESSAGE);
                Message msg = handler.obtainMessage();
                msg.what = DELAY_MESSAGE;
                msg.obj = s.toString();
                handler.sendMessageDelayed(msg, delayTime);
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });


    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            //网络请求操作
            Log.e("TAG", "editText : " + msg.obj);
        }
    };

效果如下,还是十分的nice的


由于点赞部分,又让我想起了点赞动画的处理,这个地方参考了该篇博文的思路使用Glide V4 实现GIF点赞动画 , 他的思路比较巧妙,也是我觉得特别喜欢的一个地方,下面我来说下思路:

1、通过view点击的位置,获取view当前所在屏幕位置的坐标

 int arr[] = new int[2];
 view.getLocationInWindow(arr);

2、获取Decorview,创建个一样的view,add到Decorview上

        View decorView = getWindow().getDecorView();
        if (decorView instanceof FrameLayout) {
            frameLayout = (FrameLayout) decorView;
        }
        ImageView imageView = new ImageView(this);
        imageView.setImageResource(resInt);
        FrameLayout.LayoutParams layoutParams =
                new FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT, FrameLayout.LayoutParams.WRAP_CONTENT);
       
        //获取位置拿到的坐标是控件左上角的位置,采用margin的方式来定位控件在decorview中的位置
        layoutParams.leftMargin = arr[0];
        layoutParams.topMargin = arr[1];
        layoutParams.width = 100;
        layoutParams.height = 100;

        imageView.setLayoutParams(layoutParams);
        frameLayout.addView(imageView);

3、将添加到Decorview的view做动画,监听动画结束时移除该view

   set.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {}

            @Override
            public void onAnimationEnd(Animation animation) {
                ViewGroup parent = (ViewGroup) view.getParent();
                if (parent != null) {
                    parent.removeView(view);
                }
            }

            @Override
            public void onAnimationRepeat(Animation animation) {}
        });

具体代码就不贴了,大家可以看原链接,下面贴一张我做的图

动画主要就是一个透明+缩放的一个AnimationSet