从其他片段添加新的 RecyclerView 项
Posted
技术标签:
【中文标题】从其他片段添加新的 RecyclerView 项【英文标题】:add new item of RecyclerView from other fragment 【发布时间】:2020-07-07 08:38:43 【问题描述】:我有 buyfragment
在 RecyclerView 上添加了项目列表。我想从另一个片段(sellfragment
)通过按钮调用(startselling
)添加新项目..所以我只需要lstDressAdd()
方法或onClick(startselling
)按钮的正确代码
这是(buyfragment
):
public class buyFragment extends Fragment
public List<Dresses> lstDress;
RecyclerView myrv;
RecyclerViewAdapter myAdapter;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
@ Nullable Bundle savedInstanceState)
final View view = inflater.inflate(R.layout.fragment_buy, container, false);
lstDress = new ArrayList<>();
//DRESSES DATA
lstDress.add(new Dresses("flora V-neck Drawstring A-line dress", "Bersheka", "new", 150.0, R.drawable.dress22));
lstDress.add(new Dresses("Button front flounce waist floral dress", "Pull&Bear", "new", 200.0, R.drawable.dress2));
lstDress.add(new Dresses("Shirred waist split hem Flower print dress", "Stradivarius", "new", 199.0, R.drawable.dress1));
lstDress.add(new Dresses("flora print square neck split hem midi dress", "MANGO", "new", 175.0, R.drawable.e));
lstDress.add(new Dresses("confetti heart surplice neck knot sleeve dress", "ZARA", "new", 168.0, R.drawable.dress22));
lstDress.add(new Dresses("flora V-neck Drawstring A-line dress", "ZARA", "new", 100.0, R.drawable.dress2));
lstDress.add(new Dresses("Shirred waist split hem Flower print dress", "Stradivuse", "new", 199.0, R.drawable.dress1));
myrv = (RecyclerView) view.findViewById(R.id.recyclerviwe_id);
myAdapter = (new RecyclerViewAdapter(getContext(), lstDress));
myrv.setLayoutManager(new GridLayoutManager(getActivity(), 2));
myrv.setAdapter(myAdapter);
return view;
public void lstDressAdd(String dress_title, String brand, String use, Double price)
lstDress.add(new Dresses(dress_title, brand, use, price, R.drawable.dress1));
这就是(sellfragment):
startselling=(Button)view.findViewById(R.id.button3) ;
startselling.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View v)
String text = brands.getSelectedItem().toString();
String Use;
if(newradio.getText().toString()=="New dress")
Use="New";
else
Use="Used";
String d=dress_title.getText().toString();
Double p=Double.parseDouble(price.getText().toString());
if(d==null &&p==null&&Use==null&&text==null)
Toast.makeText(getContext(), "you need to complete the dress Info", Toast.LENGTH_SHORT).show();
else
buyFragment n=new buyFragment();
n.lstDressAdd(dress_title.getText().toString(),text,Use,Double.parseDouble(price.getText().toString()));
Toast.makeText(getContext(), "Done", Toast.LENGTH_SHORT).show();
);
请帮帮我,这是我的大学项目,两天后的截止日期
【问题讨论】:
【参考方案1】:基本上你需要在列表中添加一个新项目,然后通知你的 recyclerView。
n.lstDressAdd(dress_title.getText().toString(),text,Use,Double.parseDouble(price.getText().toString()));
n.myAdapter.notifyItemInserted()
【讨论】:
notifyItemInserted(int position),,我怎么知道哪个位置【参考方案2】:试试这个代码。
public void lstDressAdd(String dress_title, String brand, String use, Double price)
lstDress.add(new Dresses(dress_title, brand, use, price, R.drawable.dress1));
myAdapter.notifyDataSetChanged();
myAdapter = (new RecyclerViewAdapter(getContext(), lstDress));
[编辑]
【讨论】:
adapter.lstDress 不可用以上是关于从其他片段添加新的 RecyclerView 项的主要内容,如果未能解决你的问题,请参考以下文章
从其他活动返回到同一片段时,如何保存和恢复片段中 RecyclerView 的滚动位置?
如何将数据从回收器适配器发送到片段 |如何从 recyclerview 适配器调用片段函数