AJW
AJW

Reputation: 1649

Android: how to fix CardViews' views scrolling in RecyclerView?

I load a RecyclerView with CardViews that gets populated with some SQLite data and a checkbox. The initial UI screen looks good with the CardViews:

enter image description here

I then begin scrolling downwards and a number of the CardViews disappear and the UI screen only shows one CardView. If I scroll further then another single CardView shows for the entire screen rather than many CardViews that I expected:

enter image description here

What am I missing here?

DataModel.java file

public class DataModel implements Serializable {

private static final long serialVersionUID = 1L;
private String todo;
private String note1;
private boolean isSelected;

public DataModel() {
}

public DataModel(String todo, String note1) {
    this.todo = todo;
    this.note1 = note1;
    isSelected = false;
}

public DataModel(String todo, String note1, boolean isSelected) {
    this.todo = todo;
    this.note1 = note1;
    this.isSelected = isSelected;
}

public String getTodo() {
    return todo;
}

public void setTodo(String todo) {
    this.todo = todo;
}

public String getNote1() {
    return note1;
}

public void setNote1(String note1) {
    this.note1 = note1;
}

public boolean isSelected() {
    return isSelected;
}

public void setSelected(boolean isSelected) {
    this.isSelected = isSelected;
}
}

ListAdapter.java file

public class ListAdapter extends RecyclerView.Adapter<ListAdapter.ListViewHolder> {

List<DataModel> dbList = new ArrayList<>();
Context context;
LayoutInflater mLayoutInflater;

public ListAdapter(Context context, List<DataModel> dbList) {
    this.context =  context;
    this.dbList = dbList;
    mLayoutInflater = LayoutInflater.from(context);
}

@Override
public ListViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

    View itemView = mLayoutInflater.inflate(R.layout.singlecard_layout, parent, false);
    return new ListViewHolder(itemView);
}

public class ListViewHolder extends RecyclerView.ViewHolder {

    TextView cardBlankText2;
    TextView cardBlankText3;
    CheckBox chkSelected;

    public ListViewHolder(View itemView) {
        super(itemView);

        cardBlankText2 = (TextView)itemView.findViewById(R.id.cardBlankText2);
        cardBlankText3 = (TextView)itemView.findViewById(R.id.cardBlankText3);
        chkSelected = (CheckBox) itemView.findViewById(R.id.chkSelected);
    }

}

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

@Override
public void onBindViewHolder(final ListViewHolder holder, final int position) {

    holder.cardBlankText2.setText(dbList.get(position).getTodo());
    holder.cardBlankText3.setText(dbList.get(position).getNote1());
    holder.chkSelected.setChecked(dbList.get(position).isSelected());
    holder.chkSelected.setTag(dbList.get(position));

    holder.chkSelected.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            CheckBox cb = (CheckBox) v;
            DataModel rowItem = (DataModel) cb.getTag();
            // rowItem.setSelected(cb.isChecked());
            dbList.get(position).setSelected(cb.isChecked());
        }
    });

}
}    

Upvotes: 2

Views: 1439

Answers (1)

Kosh
Kosh

Reputation: 6334

without code we can't help you, but let me assume, you are using RecyclerView new support Library which supports wrap_content and in your case, your layout list item height is set to match_parent try replacing your cardview parent layout height to wrap_content

Upvotes: 4

Related Questions