我在 fragment 中有RecyclerView,我希望当用户按下“添加新行”下面的按钮时,它将重定向到新 fragment ,直到这里我可以实现它,但现在我希望填写表单的详细信息将显示为recyclerview中的新行
如上图所示,用户可以在 RecyclerView 中动态添加行,并且每行右上角都有一个十字,可以从列表中删除行。
所以,我想做的是
1) 如何在用户按下保存事件后立即将行添加到带有表单输入的列表中,然后向用户显示新添加的行及其在表单中输入的详细信息
简而言之,我想使用表单中的用户输入动态从列表中添加行,并在用户按下行项目右上角的十字按钮时将其从列表中删除
最佳答案
我自己解决了这个问题,我做了什么将数据从第二个 fragment 返回到第一个 fragment
首先,我创建接口(interface)
以将结果从fragment_2(具有表单)发送到fragment_1(具有recyclerview)
这是我的表单代码
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view=inflater.inflate(R.layout.ppl_details_form,container,false);
PPL_Title_Spinner= (Spinner) view.findViewById(R.id.PPL_Spinner);
Title= (EditText) view.findViewById(R.id.ppl_title);
Address= (AutoCompleteTextView) view.findViewById(R.id.ppl_address);
Year= (EditText) view.findViewById(R.id.ppl_passing_year);
Description= (EditText) view.findViewById(R.id.ppl_description);
btn1= (Button) view.findViewById(R.id.ppl_map_button);
Save= (Button) view.findViewById(R.id.ppl_save);
Save_PPL.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
title=Title.getText().toString();
address= Address.getText().toString();
year=Year.getText().toString();
description=Description.getText().toString();
//PPL Wrapper
PPL_list_wrapper list=new PPL_list_wrapper(title,year,address);
Add_PPL=(PPL_transfer_data_to_list)getActivity();
Add_PPL.addLocation(list);
fragment_1 fragment1=new fragment_1();
FragmentTransaction fragmentTransaction=getFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.Navigation_Main_Layout,fragment1);
fragmentTransaction.commit();
}
});
@Override
public void onAttach(Context context) {
super.onAttach(context);
try{
Add_PPL= (PPL_transfer_data_to_list) context;
}
catch (ClassCastException e){
throw new ClassCastException(getActivity().toString()+" Must Implement Interface");
}
}
然后我将接口(interface)实现到父 Activity 中,然后从 Activity 中调用fragment_1方法,如下所示
@Override
public void addEvent(PPL_list_wrapper PPL_DATA) {
fragment_1 recycler=new fragment_1();
recycler.PPL_eve(PPL_DATA);
}
然后我将数据获取到fragment_1中,并且必须在 fragment 静态
中创建数组列表,因为没有静态列表也会丢失其引用和数据,并且它将停止将数据显示到recyclerview
public void PPL_eve(final PPL_list_wrapper ppl_list_wrapper){
PPL_wrapper=ppl_list_wrapper;
event_details.add(ppl_list_wrapper);//Static ArrayList
adapter=new ppl_Recycler_Adapter(getActivity(),event_details);
adapter.notifyDataSetChanged();
}
我必须确保我的列表是静态的
static List<PPL_list_wrapper> event_details=new ArrayList<PPL_list_wrapper>();
关于java - 从编辑文本内容创建 RecyclerView 列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39280994/