【问题标题】:Adding a new item to a RecyclerView when button is clicked?单击按钮时向 RecyclerView 添加新项目?
【发布时间】:2016-01-11 17:04:05
【问题描述】:

我刚开始使用 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);
        }
    });
}

}

【问题讨论】:

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


    【解决方案1】:

    我通常用 RecyclerViews 做的是添加一个方法来设置数据。 在你的例子中:

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

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

    【讨论】:

      【解决方案2】:

      您可以在适配器中添加一个方法,以在 arrayList 中添加 Plates 并通知更改。 比如:

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

      【讨论】:

      • 这是我困惑的地方。我在哪里调用 addPlates?在应该将项目添加到列表中的按钮的 onClickListener 中?
      • 您必须在要添加项目的事件中调用 adapter.addPlates。如果要添加单击按钮的项目,请在 onclicklistener 方法中调用它
      【解决方案3】:

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

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

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2019-11-20
        • 1970-01-01
        • 1970-01-01
        • 2020-06-17
        • 1970-01-01
        • 2020-07-29
        • 1970-01-01
        相关资源
        最近更新 更多