首页 > 解决方案 > RecyclerView 加载超过 n 个项目的问题

问题描述

我已经实现了一个 RecyclerView 如下

带支架的适配器

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyHolder> {

    @NonNull
    @Override
    public MyHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.indovinelloelement,parent,false);

        MyHolder holder = new MyHolder(view);
        return holder;
    }


    @Override
    public void onBindViewHolder(@NonNull MyHolder holder, int position) {
        holder.category.setText(data.get(position).getCategory());
        holder.difficulty.setText(""+data.get(position).getDifficulty());
        holder.title.setText(data.get(position).getTitle());
        holder.score.setText(""+data.get(position).getScore());
        if(data.get(position).isLocked())
            holder.setLocked();
        else
            holder.setUnlocked();

    }


    public DataGestour.Indovinello getIndovinello(int pos){
        return data.get(pos);
    }


    @Override
    public int getItemCount() {
        return data.size();
    }


    public class MyHolder extends RecyclerView.ViewHolder {

        public TextView category,score,difficulty,title;

        private ImageView lock_state;

        public MyHolder(View itemView) {
            super(itemView);
            category = (TextView)itemView.findViewById(R.id.categoria);
            score = (TextView)itemView.findViewById(R.id.punteggio);
            difficulty = (TextView) itemView.findViewById(R.id.difficolta);
            title = (TextView)itemView.findViewById(R.id.titolo);

            lock_state = (ImageView) itemView.findViewById(R.id.lock_state);
        }

        public void setLocked(){
            lock_state.setImageResource(R.drawable.ic_lock_outline_black_24dp);
        }

        public void setUnlocked(){
            lock_state.setImageResource(R.drawable.ic_lock_open_black_24dp);
        }
    }



    ArrayList<DataGestour.Indovinello> data;
    Context context;
    public MyAdapter(Context context, ArrayList<DataGestour.Indovinello> list){
        this.context = context;
        this.data = list;
    }
}

和 RecyclerView:

rc = (RecyclerView)root_view.findViewById(R.id.lista_domande);
    rc.setHasFixedSize(true);

LinearLayoutManager ly = new LinearLayoutManager(getContext());
ly.setOrientation(LinearLayoutManager.VERTICAL);

rc.setLayoutManager(ly);

try{
    gestour = new DataGestour(getContext());
}catch (IllegalStateException e){
    Log.e("DataManager",e.getMessage());
};

adapter = new MyAdapter(getContext(),gestour.getAllDatas());

rc.setAdapter(adapter);
adapter.notifyDataSetChanged();

其中 DataGestoure 是一个类,它操作数据库中的一些数据并将它们存储在 ArrayList 中(DataGestour.getAllDatas 是在 ArrayList 下返回数据的方法)

问题仅在 ArrayList 包含超过 3 个项目时才开始,因为 RecyclerView 不显示它们,尽管适配器将所有数据保存在 ArrayList<DataGestour.Indovinello> 数据中

标签: javaandroidandroid-recyclerview

解决方案


好吧,您需要在代码中增加一点顺序,因为它的结构不正确,默认情况下 RecyclerView 具有其 Scrolleable 视图,除非在 XML 中删除此属性,我建议您在适配器中使用此代码并使用模型来保存您从 BD 获得的信息,这是保持一切井井有条的正确工作方式,而且工作起来更容易,一旦完成,您就可以完美地使用此代码,我相信它会起作用,请记住仅将信息传递给我的模型,在我的情况下称为“模型”,这样您的数据加载错误将得到解决。对于这些情况,也建议使用 List <>,因为 Arrays <> 在您操作数据时往往会出现错误,如果您按照呈现代码的方式进行操作,则会出现更多错误。

private List<Model> mDataList;

public MyAdapter(Context context){
        this.context = context;
        this.mDataList = new ArrayList<>();
    }

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyHolder> {

@NonNull
@Override
public MyHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    View view = LayoutInflater.from(parent.getContext())
            .inflate(R.layout.indovinelloelement,parent,false);

    return new MyHolder(view);
}


@Override
public void onBindViewHolder(@NonNull MyHolder holder, int position) {
    MyHolder holder = (MyHolder) MyHolder;
    hoder.bind(mDataList.getPosition(position));
}

@Override
public int getItemCount() {
    return mDataList.size();
}

 public void setData(List<Model> list) {
    mDataList.clear();
    mDataList.addAll(list);
    notifyDataSetChanged();
}


public class MyHolder extends RecyclerView.ViewHolder {

    TextView category;
    category = (TextView)itemView.findViewById(R.id.categoria);
    TextView score;
    score = (TextView)itemView.findViewById(R.id.punteggio);
    TextView difficulty;
    difficulty = (TextView) itemView.findViewById(R.id.difficolta);
    TextView title;
    title = (TextView)itemView.findViewById(R.id.titolo);
    ImageView lock_state;
    lock_state = (ImageView) itemView.findViewById(R.id.lock_state);

    public MyHolder(View itemView) {
        super(itemView);
    }

    protected void bind(Model model){
        category.setText(model.getCategory());
        score.setText(model.getScore());
        difficulty.setText(model.getDifficulty());
        title.setText(model.getTitle());

        if(model.isLocked()){
            lock_state.setImageResource(R.drawable.ic_lock_outline_black_24dp);
        } else {
            lock_state.setImageResource(R.drawable.ic_lock_open_black_24dp);
        }
    }
}
}

对于包含 RecyclerView 的类,使用如下代码。

RecyclerView mRecyclerView;
mRecyclerView = (RecyclerView)root_view.findViewById(R.id.lista_domande);

private MyAdapter mAdapter;
private List<Model> mDataList;
private DataGestour gestour;

private void setupRecyclerView(){

LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext());
linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);

mRecyclerView.setHasFixedSize(true);
mRecyclerView.setLayoutManager(linearLayoutManager);

mAdapter = new MyAdapter(getContext());
mRecyclerView.setAdapter(mAdapter);
mRecyclerView.setData(mDataList);

}

private void getDataDB(){
    mDataList.add(gestour.getAllDatas);
}

推荐阅读