zoukankan      html  css  js  c++  java
  • Android 蹲坑的疑难杂症集锦一

     各位看官老爷子你们好,我就是那个挖坑不埋,还喜欢开新矿的小喵同志。

     问大家一个问题,在Github上找项目的时候,看到中文简介说明你们是不是觉得这个项目很low不屑一顾?

     最近朋友无意中说,在Github上看到中文的项目点都不想点进去,太low了。想想好像很多大神的项目都是纯英语的,so,作为只有四级程度的小同志,我就是那个项目里弥漫着中文的家伙(ノQ益Q)ノ彡┻━┻,你们怎么看?我挺喜欢中文的。
     
    例牌Github:https://github.com/CarGuo :欢迎各种姿势的star,fuck,watch。

     这期要聊的是那么躺在坑里的需求,想想这一路走来,本人还能正常的行走于人世,真是好感动呢,感谢产品和QA的不杀之恩。

    • 1、TextView的由于Span导致省略号显示不正常问题。

    • 2、TextView容易被背景同化看不清?增加阴影吧。

    • 3、启动白屏一会或者点击logo卡顿很久没反应的暴力解决。

    • 4、打开自定义图库,选择图片容易OOM,页面崩溃回到程序crash。

    • 5、ViewPager嵌套RecyclerView还有轮播图的手势冲突,参考:linkagescroll

    • 6、透明主题会导致手机背景在切换Tab的时候穿透到桌面,一般MainActivity不要用这个。

    • 7、ViewPager的实时数据刷新。

    • 8、CardView 慎用,因为在某些低版本的机器上会有白带,需要的话可以用shape实现圆角。

    • 9、图片压缩安利。

    • 10、RecyclerView的回到顶部的优化。

    1、之前分享过一篇文章《文本编辑和显示(emoji表情,@某人、链接高亮点击)》,这里用到的Spanable替代String用于TextView的显示,一般情况下是没什么问题的,但是如果你需要在列表中或者详情中做省略号处理,你就有可能发现,省略号居然出现在表情的前面,把一大段文字给省略了;又或者直接最后面显示了一部分的表情。原因Google可阅,这里就说说我农村式的做法吧:

     是的,自己截断,根据你需要的行数,对应的在显示的时候把数据截断,然后自己加上···(这里如有更好的记得评论分享啊!)

    textView.setText(spannable);
    textView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
        @Override
        public void onGlobalLayout() {
            textView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
            if (textView.getLineCount() > lineCount) {
                int endOfLastLine = textView.getLayout().getLineEnd(lineCount - 1);
                if (endOfLastLine > 4) {
                    CharSequence newVal = textView.getText().subSequence(0, endOfLastLine - 4);
                    textView.setText(newVal);
                    textView.append("...");
                }
            }
        }
    });

    2、TextView经常会因为背景图片的原因而导致出现看不清文字的情况,这里无数次考虑之后,决定某些特殊地方可以用轻微的阴影解决。

    android:shadowColor="#AA000000"
    android:shadowDy="1"
    android:shadowRadius="1"

    3、启动页问题,其实最好是做冷启动优化,但是作为一个时间紧(tou)张(lan)的程序猿,我是在WelcomeActivity的主题里设置了主题,暴力解决,虽然这不是长久之计:

    
    

    4、打开自定义图库OOM主要要解决的还是内存释放的问题,有时候内存一下子来的太猛还是不行的,而且逻辑有问题的崩溃导致APP直接回到解放前的确得不偿失,所以呢:

     直接把选择Activityyoga新的process进程打开,这样闪退了也不影响APP啊,数据该怎么返回还是怎么返回。什么?首次启动的时候回卡白屏几秒,下面的主题暴力帮助你。什么?打开卡顿一会才能看到页面。我试了微信也会,所以如果有优化的控件记得评论分享下,这样我可以愉悦的去找产品说(zhuang)教(B)。

    
    ···
    

     
    5、更新ViewPager,这里简单上代码,感受下,我就静静的不说话.....((/- -)/:

     resolveViewLogic();//比如更新list数据
     fragmentPagerAdapter.setUpdateFlag(true);//让其更新
     fragmentPagerAdapter.notifyDataSetChanged();
     fragmentPagerAdapter.setUpdateFlag(false);//关了
    
    ···
    public class RefreshFragmentPagerAdapter extends FragmentPagerAdapter {
    
        private List fragments;
        private FragmentManager fragmentManager;
        private boolean updateFlag;
    
    
        public boolean isUpdateFlag() {
            return updateFlag;
        }
    
        public void setUpdateFlag(boolean updateFlag) {
            this.updateFlag = updateFlag;
        }
    
        public void setFragments( List fragments) {
            this.fragments = fragments;
        }
    
    
        public RefreshFragmentPagerAdapter(FragmentManager fragmentManager, List fragments) {
            super(fragmentManager);
            this.fragments = fragments;
            this.fragmentManager = fragmentManager;
        }
    
        @Override
        public Fragment getItem(int position) {
            return fragments.get(position);
        }
    
        @Override
        public int getCount() {
            return fragments.size();
        }
    
        @Override
        public CharSequence getPageTitle(int position) {
            return "";
        }
    
        //让其更新
        @Override
        public int getItemPosition(Object object) {
            return POSITION_NONE;
        }
    
        //核心方法
        @Override
        public Object instantiateItem(ViewGroup container, int position) {
    
            if (updateFlag) {//根据需求添加更新标示
                //得到缓存的fragment
                Fragment fragment = (Fragment) super.instantiateItem(container, position);
                //得到tag,这点很重要
                String fragmentTag = fragment.getTag(); //这里的tag是系统自己生产的,我们直接取就可以
                //如果这个fragment需要更新
                FragmentTransaction ft = fragmentManager.beginTransaction();
                //移除旧的fragment
                ft.remove(fragment);
                //换成新的fragment
                fragment = fragments.get(position);
                //添加新fragment时必须用前面获得的tag,这点很重要
                if (!fragment.isAdded()) {
                    ft.add(container.getId(), fragment, fragmentTag);
                    ft.attach(fragment);
                    ft.commit();
                }
                return fragment;
            } else {
                return super.instantiateItem(container, position);
            }
        }
    }

    9、图片压缩安利:Luban,压缩效果真的不错,和微信一拼,就是那种长的离谱的图片压缩后效果不佳,其他都很不错,反正我是直接把里面的thirdCompress拿出来套到自己里面。

    private File thirdCompress(@NonNull File file)

    10、RecyclerView的回到顶部的优化。

     也许你会问,不就是一键回去么,为什么还要优化?
     
     因为爱情,不会轻易悲伤,产品经理都是年轻的模样,看看回到顶部的效果,所以就有了这个优化:

     记得先停止滚动,然后回到第十个,然后在智能的滚到顶部,不信你试试,感觉舒服好多哟。

    protected void goToTop() {
        mRecyclerView.stopScroll();
        mLayoutManager.setSmoothScrollbarEnabled(true);
        if (firstVisibilityPosition > 10) {
            mLayoutManager.scrollToPositionWithOffset(10, 0);
        }
        mRecyclerView.smoothScrollToPosition(0);
        firstVisibilityPosition = 0;
    }
  • 相关阅读:
    CodeForces 659F Polycarp and Hay
    CodeForces 713C Sonya and Problem Wihtout a Legend
    CodeForces 712D Memory and Scores
    CodeForces 689E Mike and Geometry Problem
    CodeForces 675D Tree Construction
    CodeForces 671A Recycling Bottles
    CodeForces 667C Reberland Linguistics
    CodeForces 672D Robin Hood
    CodeForces 675E Trains and Statistic
    CodeForces 676D Theseus and labyrinth
  • 原文地址:https://www.cnblogs.com/wangfeng520/p/6096406.html
Copyright © 2011-2022 走看看