muedsa / WarframeApp

MUedsa's Warframe App
3 stars 0 forks source link

RecyclerView Data need always update (RecyclerView需要不停更新的问题) #1

Open muedsa opened 7 years ago

muedsa commented 7 years ago

在RecyclerView的item中的有一个关于时间记录的textView和progressBar 需要子线程不停刷新(while true),但是发现在刷新过程中主线程触发动画,必如ViewPage的滑动或者tabLayout切换等动画会非常卡。由于不知道怎么解决(开发菜鸡), 只能监听这些控件在动画时添加一个锁:

MainActivity.java

        mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                threadLock = 0;
            }

            @Override
            public void onPageSelected(int position) {
                threadLock = 1;
            }

            @Override
            public void onPageScrollStateChanged(int state) {
                switch (state){
                    case ViewPager.SCROLL_STATE_DRAGGING:
                        threadLock = 0;
                        break;
                    case ViewPager.SCROLL_STATE_IDLE:
                        threadLock = 1;
                }
            }
        });
--------

*fragment.java

        recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
                ((MainActivity)getActivity()).threadLock = 0;
                if(newState == RecyclerView.SCROLL_STATE_IDLE){
                    ((MainActivity)getActivity()).threadLock = 1;
                }
            }

            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
            }
        });

在子线程刷新时判断一下:

        this.timerHandler = new Handler(){
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                if(AlertAdapter.this.alertFragment.getActivity() != null && ((MainActivity)AlertAdapter.this.alertFragment.getActivity()).threadLock == 1){
                    AlertAdapter.this.notifyDataSetChanged();
                }
            }
        };