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 PointNet++的onnx模型只能使用一次
  • ¥20 西南科技大学数字信号处理
  • ¥15 有两个非常“自以为是”烦人的问题急期待大家解决!
  • ¥30 STM32 INMP441无法读取数据
  • ¥15 R语言绘制密度图,一个密度曲线内fill不同颜色如何实现
  • ¥100 求汇川机器人IRCB300控制器和示教器同版本升级固件文件升级包
  • ¥15 用visualstudio2022创建vue项目后无法启动
  • ¥15 x趋于0时tanx-sinx极限可以拆开算吗
  • ¥500 把面具戴到人脸上,请大家贡献智慧,别用大模型回答,大模型的答案没啥用
  • ¥15 任意一个散点图自己下载其js脚本文件并做成独立的案例页面,不要作在线的,要离线状态。