单击按钮时向RecyclerView添加新项目?

时间:2016-01-11 17:04:06

标签: android model-view-controller model adapter android-recyclerview

我刚刚开始使用RecyclerViews,但我完全不了解如何添加或删除项目。下面我将附加我的适配器代码它是一个测试代码,布局中的一切工作正常。我觉得我也写了太多不必要的代码,所以任何提示或批评都会受到赞赏。

public class PlatesAdapter extends
        RecyclerView.Adapter<PlatesAdapter.ViewHolder> {
    //Declaring a List<> of Plates
    private List<Plates> mPlates;
    int amountOfPlates;

    public static class ViewHolder extends RecyclerView.ViewHolder {
        //Declaring Buttons and textViews
        public TextView plateWeightTextView, amountOfPlatesTextView;
        public Button addButton, subButton, addLayoutButton;


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

            //initializing Buttons and TextViews
            plateWeightTextView = (TextView) itemView.findViewById(R.id.plate_weight_value_textView);
            amountOfPlatesTextView = (TextView) itemView.findViewById(R.id.amount_of_plates_textView);
            addButton = (Button) itemView.findViewById(R.id.add_button);
            subButton = (Button) itemView.findViewById(R.id.subtract_button);
            addLayoutButton = (Button) itemView.findViewById(R.id.button);

        }
    }

    //Constructor
    public PlatesAdapter(List<Plates> plates) {
        mPlates = plates;
    }

    @Override
    public PlatesAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        Context context = parent.getContext();
        LayoutInflater inflater = LayoutInflater.from(context);

        View PlatesView = inflater.inflate(R.layout.plate_item_layout, parent, false);

        ViewHolder viewHolder = new ViewHolder(PlatesView);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(PlatesAdapter.ViewHolder holder, int position) {

        final TextView textView2 = holder.amountOfPlatesTextView;

        //BUTTONS add 1 or subtract 1 from amountOfPlates;
        Button button = holder.addButton;
        Button button2 = holder.subButton;

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                amountOfPlates++;
                textView2.setText(Integer.toString(amountOfPlates));
            }
        });

        button2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                amountOfPlates--;
                textView2.setText(Integer.toString(amountOfPlates));
            }
        });
    }

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

这是我的模型层,我觉得这是完全错误的,但我不是100%确定是否。

  public class Plates {
    private int mPlateWeight;
    private int mAmountOfPlates;

    public Plates() {
        //mPlateWeight = plateWeight;
        //mAmountOfPlates = amountOfPlates;
    }

    public int getmPlateWeight() {
        return mPlateWeight;
    }

    public int getmAmountOfPlates() {
        return mAmountOfPlates;
    }

    public static List<Plates> createPlateList() {
        List<Plates> plates = new ArrayList<>();

        plates.add(new Plates());

        return plates;
    }
}

这就是我所喜欢的地方。它是我调用addPlates或addItem方法,我传递给它的是什么?以下是我的主要活动。我只是不知道在哪里添加这些addItems或addPlates方法是模型层还是适配器?

public class MainActivity extends AppCompatActivity {

private RecyclerView.LayoutManager mLayoutManager;

Button mButton;

private List<Plates> mData = new ArrayList<>();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    //Button to add layout to recyclerView
    mButton = (Button) findViewById(R.id.button);
    //Adapter LayoutManager
    mLayoutManager = new LinearLayoutManager(this);

    mButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            RecyclerView recyclerView = (RecyclerView) findViewById(R.id.weights_recycler_view);
            PlatesAdapter adapter = new PlatesAdapter(mData);
            recyclerView.setAdapter(adapter);
            recyclerView.setLayoutManager(mLayoutManager);
        }
    });
}

}

3 个答案:

答案 0 :(得分:1)

我通常使用RecyclerViews添加一种方法来设置数据。 在您的示例中:

public void setPlates(List<Plates> plates) {
  mPlates = plates;
  notifyDataSetChanged();
}`

如果要验证数据是否已更改,也可以添加getter。

答案 1 :(得分:0)

首先,不需要createPlateList方法。 您应该在适配器中添加如下所示的方法:

public void addItem(Plates plate)
{
    mPlates.add(plate);
}

由于适配器使用此列表,因此添加或删除项目所需的全部操作是添加/删除列表中的项目。毕竟,您需要在适配器中调用notifyDataSetChanged(),以便它知道列表中的数据已更改。

答案 2 :(得分:0)

您可以在适配器中添加方法,以在arrayList中添加Plates并通知更改。 类似的东西:

public void addPlates(Plates plate) {
  if (mPlates == null) mPlates = new ArrayList();
  mPlates.add(plate);
  //notifyDataSetChanged();
  notifyItemInserted(mPlates.size()-1)
}`