qq_38490134
2019-01-29 14:26
采纳率: 100%
浏览 4.0k

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去滚动。

    点赞 2 打赏 评论
  • Dopedada 2019-01-30 11:38
    点赞 打赏 评论

相关推荐