Android:如何修复CardViews'在RecyclerView中滚动的视图?

时间:2016-04-20 02:42:02

标签: android android-recyclerview android-adapter

我使用CardViews加载一个RecyclerView,其中填充了一些SQLite数据和一个复选框。使用CardViews初始UI屏幕看起来很好:

enter image description here

然后我开始向下滚动,一些CardView消失,UI屏幕只显示一个CardView。如果我进一步滚动,那么另一个CardView会显示整个屏幕,而不是我期望的许多CardView:

enter image description 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());
        }
    });

}
}    

1 个答案:

答案 0 :(得分:3)

没有代码,我们无法为您提供帮助,但让我假设,您使用RecyclerView新支持Library支持wrap_content,在您的情况下,您的布局列表项高度设置为match_parent尝试将cardviewlayout height替换为wrap_content