[英]Adding a new item to a RecyclerView when button is clicked?
我剛剛開始使用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);
}
});
}
}
我通常對RecyclerViews進行的操作是添加一種設置數據的方法。 在您的示例中:
public void setPlates(List<Plates> plates) {
mPlates = plates;
notifyDataSetChanged();
}`
如果要驗證數據是否已更改,也可以添加吸氣劑。
首先,不需要createPlateList
方法。 您應該在適配器中添加如下所示的方法:
public void addItem(Plates plate)
{
mPlates.add(plate);
}
由於適配器與該列表一起使用,因此添加或刪除項目所需要做的就是從列表中添加/刪除項目。 畢竟,您需要在適配器中調用notifyDataSetChanged()
,以便它知道列表中的數據已更改。
您可以在適配器中添加方法,以便在arrayList中添加Plates並通知更改。 就像是:
public void addPlates(Plates plate) {
if (mPlates == null) mPlates = new ArrayList();
mPlates.add(plate);
//notifyDataSetChanged();
notifyItemInserted(mPlates.size()-1)
}`
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.