简体   繁体   English

RecyclerView问题加载了n个以上的项目

[英]RecyclerView problem to load more than n items

I've implementend a RecyclerView as follow 我已经实现了一个RecyclerView如下

The Adapter with the Holder 带支架的适配器

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;
    }
}

and the RecyclerView: 和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();

where DataGestoure is a class that manipulates some data from a database and store them in a ArrayList (DataGestour.getAllDatas is the method to return the data under a ArrayList) 其中,DataGestoure是一个类,用于处理数据库中的某些数据并将其存储在ArrayList中(DataGestour.getAllDatas是在ArrayList下返回数据的方法)

The problem start only if the ArrayList contains more then 3 items becouse the RecyclerView doen't show them despite the fact that the adapter holds all the data in ArrayList< DataGestour.Indovinello > data 仅当ArrayList包含3个以上的项目时,问题才开始出现,因为尽管适配器保存了ArrayList <DataGestour.Indovinello> data中的所有数据,但RecyclerView并未显示它们

Well, you need a little more order in your code, since it is not properly structured, by default the RecyclerView has its Scrolleable view unless in the XML this attribute is removed, well I recommend that you use this code in your adapter and use a model to save the information you get from your BD, that is the correct way to work to keep everything in order and it is easier to work it, once this is done you can use this code perfectly that I am sure will work, remember to only pass information to my Model that in my case calls it "Model" and with this the error of your data load will be solved. 好吧,由于代码结构不正确,因此您需要在代码中多一点顺序,默认情况下,RecyclerView具有其Scrolleable视图,除非在XML中删除了此属性,那么我建议您在适配器中使用此代码并使用模型来保存您从BD获得的信息,这是使所有事物井井有条的正确方法,并且操作起来更容易,一旦完成,您就可以完美地使用此代码,我相信它将起作用,请记住仅将信息传递给我的模型(在我的情况下称为“模型”),这样就可以解决您的数据加载错误。 It is also advisable to use a List <> for these cases since the Arrays <> tend to have errors when you manipulate the data and more if you do it the way you present the code. 在这些情况下,也建议使用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);
        }
    }
}
}

And for your class that contains the RecyclerView use the code as follows. 对于包含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);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM