RecyclerView - 我无法添加超过1行

时间:2017-10-12 11:55:49

标签: java android android-recyclerview

我在我的RecyclerView中添加超过1行(item)时出现问题,当我点击按钮时,该项目会覆盖前一行,但如果我创建了List<>在onCreate上硬编码的数据,它可以在RecyclerView中添加超过1行。请遵循以下代码:

  

ListChecklistAdapter

public class ListChecklistAdapter extends RecyclerView.Adapter<ListChecklistAdapter.ListChecklistViewHolder> {
    private List<Checklist> mChecklist;
    private Context mCtx;

    public ListChecklistAdapter(Context ctx, List<Checklist> checklists) {
        mCtx = ctx;
        mChecklist = checklists;
    }

    @Override
    public ListChecklistViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mCtx).inflate(R.layout.item_checklist, parent, false);
        return new ListChecklistViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ListChecklistViewHolder holder, int position) {
        holder.cbItem.setText(mChecklist.get(position).getDescricao());
        holder.cbItem.setChecked(mChecklist.get(position).isCheckado());
    }

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

    public class ListChecklistViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.item_checkbox)
        CheckBox cbItem;

        public ListChecklistViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }

    public void refreshData(List<Checklist> item) {
        this.mChecklist.clear();
        this.mChecklist.addAll(item);
        notifyDataSetChanged();
    }
}
  

item_checklist

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <CheckBox
        android:id="@+id/item_checkbox"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="5dp"
        android:paddingLeft="6dp"
        android:text=" "
        tools:text="Exemplo de item de lista" />

</RelativeLayout>
  

MainActivity

public class MainActivity extends AppCompatActivity {
    @BindView(R.id.rcv_lista)
    RecyclerView rvLista;
    int valor;
    private List<Checklist> lista;
    private ListChecklistAdapter listChecklistAdapter;
    @BindView(R.id.btn_add_item)
    Button btnAdd;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);

    }

    private int cont() {
        return valor++;
    }

    public void novoItem(View view) {
        lista = new ArrayList<>();
        lista.add(new Checklist("Item " + (cont() + 1), true));
        loadRecycler(lista);
    }

    private void loadRecycler(List<Checklist> lista) {
        if (listChecklistAdapter == null) {
            Log.i("LOG", "IF");

            listChecklistAdapter = new ListChecklistAdapter(this, lista);

            rvLista.setAdapter(listChecklistAdapter);
            rvLista.setLayoutManager(new LinearLayoutManager(getApplicationContext()));
            rvLista.addItemDecoration(new DividerItemDecoration(this, 1));
            return;
        } else {
            Log.i("LOG", "ELSE");
            listChecklistAdapter.refreshData(lista);
            listChecklistAdapter.onAttachedToRecyclerView(rvLista);
        }
    }
}
  

activity_main

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="16dp"
    tools:context="teste.com.br.recyclercomcheckbox.MainActivity">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/rcv_lista"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <Button
        android:id="@+id/btn_add_item"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="right"
        android:layout_marginTop="5dp"
        android:background="@android:color/transparent"
        android:onClick="novoItem"
        android:paddingLeft="20dp"
        android:paddingRight="20dp"
        android:text="+ novo item"
        android:textColor="#009688" />

</LinearLayout>

2 个答案:

答案 0 :(得分:0)

只需从this.mChecklist.clear();方法中删除此行refreshData()

public void refreshData(List<Checklist> item) {
    this.mChecklist.addAll(item);
    notifyDataSetChanged();
}

答案 1 :(得分:0)

@Nilesh Rathod解决了这个问题!我刚刚从for x in range(2, n): if n % x == 0: print("{} is equal to {} * {}".format(n, x, n // x)) return False else: print(n, " is a prime number") return True 删除了this.mChecklist.clear(); 感谢大家的帮助!