fragment和Activity同時操做UI引發的延遲、卡頓

最近項目中遇到一個問題,app首頁的Activity中由若干個Fragment頁面組成,其中一個fragment頁面是由一個GridView和ListView組成的列表,若是列表中數據量過大的時候,在請求數據的時候點擊頁面上的其餘按鈕會無響應,直到該fragment上的數據刷新完頁面纔會有反應,通過測試是因爲fragment請求完數據刷新UI界面與Activity中的底部按鈕都是共用的Activity UI主線程,因此只能同時響應一個,解決方案就是對列表數據進行分頁處理,並用RecyclerView實現列表。app

  • 分頁處理就是請求完接口獲得數據,做爲總數據,刷新、加載時經過page再獲取每頁須要獲取的數據,刷新頁面,進行一些相關的操做便可,在此就不作贅述了。ide

  • 使用RecyclerView實現的好處是能夠利用其局部刷新的特色,減小由於notifyDataSetChanged()帶來的沒必要要的開銷,此處用一個RecyclerView實現了GridView和ListView,用viewType屬性實現兩種佈局的效果。佈局

設置GridLayoutManager的列數爲兩列,而後經過itemViewType值判斷item佔用的列數。測試

final GridLayoutManager manager = new GridLayoutManager(getActivity(), 2);
        //設置佔用的列數
        manager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
            @Override
            public int getSpanSize(int position) {
                int type = rvXiangmu.getAdapter().getItemViewType(position);
                Log.i("pp", position + ":" + type);
                //如果TYPE_ONE,佔用兩列,不然佔用一列,分母爲grid設置的列數
                if (type == TaskTeamEntity2.TYPE_ONE) {
                    return manager.getSpanCount();
                } else {
                    return 1;
                }
            }
        });
        rvXiangmu.setLayoutManager(manager);

給item繪製間距spa

rvXiangmu.addItemDecoration(new RecyclerView.ItemDecoration() {
            @Override
            public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
                super.getItemOffsets(outRect, view, parent, state);
                //給佈局裏的子view添加邊距
                GridLayoutManager.LayoutParams layoutParams = (GridLayoutManager.LayoutParams) view.getLayoutParams();
                int position = parent.getChildPosition(view);
                //spanSize表示item佔用幾列
                int spanSize = layoutParams.getSpanSize();
                //若是是GridView的話繪製
                //表示item佔用的列數不是manager設置的總列數,表示一行多列的狀況
                if (spanSize!=manager.getSpanCount()) {
                    int spanIndex = layoutParams.getSpanIndex();
                    outRect.bottom = ScreenUtil.dip2px(getActivity(), 4);
                    if (spanIndex == 0) {
                        outRect.left = ScreenUtil.dip2px(getActivity(), 8);
                        outRect.right = ScreenUtil.dip2px(getActivity(), 2);
                    } else {
                        outRect.right = ScreenUtil.dip2px(getActivity(), 8);
                        outRect.left = ScreenUtil.dip2px(getActivity(), 2);
                    }
                    if (position == 0 || position == 1) {
                        outRect.top = ScreenUtil.dip2px(getActivity(), 4);
                    }
                }
            }
        });

adapter中部分代碼.net

//建立新View,被LayoutManager所調用
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
        //根據不一樣的viewType,建立並返回相應的ViewHolder
        switch (viewType) {
            case TaskTeamEntity2.TYPE_TWO:
                return new TypeTwoHolder(mLayoutInflater.inflate(R.layout.item_item1, viewGroup, false));
            case TaskTeamEntity2.TYPE_ONE:
                return new TypeOneHolder(mLayoutInflater.inflate(R.layout.item_item2, viewGroup, false));
        }
        return null;
    }
public class TypeOneHolder extends TypeAbstarctViewHolder {
        private TextView itemNameTv;
        private TextView itemTimeTv;

        public TypeOneHolder(View view) {
            super(view);
            itemNameTv = (TextView) view.findViewById(R.id.tv_item_name);
            itemTimeTv = (TextView) view.findViewById(R.id.tv_item_time);
        }

        @Override
        public void bindHolder(TaskTeamEntity2 item, int position) {
            itemNameTv.setText(item.t_teamName);
            itemTimeTv.setText(item.t_enddate);
        }
    }
//自定義的ViewHolder,持有每一個Item的的全部界面元素
    public class TypeTwoHolder extends TypeAbstarctViewHolder {
        private TextView itemNameTv;
        private TextView itemTimeTv;

        public TypeTwoHolder(View view) {
            super(view);
            itemNameTv = (TextView) view.findViewById(R.id.tv_item_name);
            itemTimeTv = (TextView) view.findViewById(R.id.tv_item_time);
        }

        @Override
        public void bindHolder(TaskTeamEntity2 item, int position) {
            itemNameTv.setText(item.t_teamName);
            itemTimeTv.setText(item.t_enddate);
        }
    }
public abstract class TypeAbstarctViewHolder extends RecyclerView.ViewHolder {
        public TypeAbstarctViewHolder(View itemView) {
            super(itemView);
        }

        public abstract void bindHolder(TaskTeamEntity2 item, int position);
    }

優雅的使用RecyclerView(在一個recyclerView裏顯示有不一樣子佈局的界面,就是混合不一樣佈局)線程

一個簡單的RecyclerView多佈局實現code

RecycleView 實現複雜首頁佈局三種方式blog

相關文章
相關標籤/搜索