qq_38490134 2019-01-29 14:26 采纳率: 100%
浏览 4685
已采纳

RecyclerView初始化时加载了全部item,导致打开页面卡顿问题

一个选择城市的页面,打开页面时就初始化了全部item,之后怎么滑动也不会再调用onBindViewHolder方法。下面是适配器代码,求解谢谢!

public class SelectCityRvAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private Context mContext;
    private final int TYPE_LETTER = 0;
    private final int TYPE_CHINESE = 1;
    private final int TYPE_HEADER = 2;
    private SelectCityContract.OnSelectCityListener mListener;
    private String mLetters[] = {"#", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q",
            "R", "S", "T", "U", "V", "W", "X", "Y", "Z"};
    private int mLetterPos[] = new int[27];

    SelectCityRvAdapter(Context context){
        mContext = context;
        mListener = (SelectCityContract.OnSelectCityListener) context;
        getAllLetterPos();
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = null;
        if(viewType == TYPE_LETTER){
            view = LayoutInflater.from(mContext).inflate(R.layout.item_select_city_letter,parent,
                    false);
            return new ViewHolderLetter(view);
        }else if(viewType == TYPE_CHINESE){
            view = LayoutInflater.from(mContext).inflate(R.layout.item_select_city_chinese,parent,
                    false);
            return new ViewHolderCity(view);
        }
        return null;
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, final int position) {
        if(holder instanceof ViewHolderLetter){
            ((ViewHolderLetter)holder).textView.setText(City.citys[position]);
            for(int i=0;i<mLetterPos.length;i++){
                if(mLetterPos[i] == position){
                    mListener.onScrollLetter(i);
                }
            }
        }else if(holder instanceof ViewHolderCity){
            ((ViewHolderCity)holder).textView.setText(City.citys[position]);
            ((ViewHolderCity)holder).textView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    mListener.onSelect(City.citys[position]);
                }
            });
        }
        KLog.i("aaa","第"+position+"项");
    }

    @Override
    public int getItemCount() {
        return City.citys.length;
    }

    @Override
    public int getItemViewType(int position) {
        Pattern pattern = Pattern.compile("^[A-Z]");
        if (pattern.matcher(City.citys[position]).matches()){
            return TYPE_LETTER;
        }else{
            return TYPE_CHINESE;
        }
    }

    public int[] getLetterPos(){
        return mLetterPos;
    }

    private void getAllLetterPos(){
        ThreadPoolUtils.getInstance().execute(new Runnable() {
            @Override
            public void run() {
                for(int n=0;n<mLetters.length;n++){
                    boolean exits = false;
                    for(int i=0;i<City.citys.length;i++){
                        if(City.citys[i].equals(mLetters[n])){
                            mLetterPos[n] = i;
                            exits = true;
                            break;
                        }
                    }
                    if(!exits){
                        if(n == 0){
                            mLetterPos[0] = 0;
                        }else{
                            mLetterPos[n] = mLetterPos[n - 1];
                        }
                    }
                }
            }
        });

    }

    public class ViewHolderCity extends RecyclerView.ViewHolder{
        TextView textView;

        public ViewHolderCity(View itemView) {
            super(itemView);
            textView = itemView.findViewById(R.id.textView);
        }
    }

    public class ViewHolderLetter extends RecyclerView.ViewHolder{
        TextView textView;

        public ViewHolderLetter(View itemView) {
            super(itemView);
            textView = itemView.findViewById(R.id.textView);
        }
    }
}![图片说明](https://img-ask.csdn.net/upload/201901/29/1548743089_581308.png)

  • 写回答

2条回答 默认 最新

  • timorwu 2019-01-31 18:20
    关注

    是不是RecyclerView的上级布局是ScrollView?ScrollView+RecyclerView需要先算出RecyclerView的高度的,不然显示不全。RecyclerView的Item复用是RecyclerView内部自己算法,在ScrollView中并不会调用那个算法,就是全部加载了然后交由ScrollView去滚动。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 求Houdini使用行家,付费。价格面议。
  • ¥15 AttributeError: 'EasyDict' object has no attribute 'BACKUP_DB_INFO'
  • ¥15 前端高拍仪调用问题报错
  • ¥15 想用octave解决这个数学问题
  • ¥15 Centos新建的临时ip无法上网,如何解决?
  • ¥15 海康威视如何实现客户端软件对设备语音请求的处理。
  • ¥15 支付宝h5参数如何实现跳转
  • ¥15 MATLAB代码补全插值
  • ¥15 Typegoose 中如何使用 arrayFilters 筛选并更新深度嵌套的子文档数组信息
  • ¥15 CF1927D 求Hack