我在ViewPager中有一个片段,它有一个RecyclrView和Button, 按钮要打开新片段以填充数据以显示在RecyclerView中,我需要一种方法来获取这些数据并在RecyclerView中显示iy,
这是显示recyclerview + button
的片段public class ContractPlanFragment extends Fragment implements PlanRecyclerView.MyClickListener {
public static final String MyPREFERENCES = "MyPrefs" ;
private RecyclerView planRecyclerView;
private RecyclerView.Adapter mAdapter;
private List<Integer> framePhotoList ;
private Button addPlan ;
View rootView;
List<Plan> planList ;
private static final String ARG_KEY = "key";
private String mKey;
private ContractInfoPage mPage;
private PageFragmentCallbacks mCallbacks;
private String plan_detials;
private String palnDate;
private String startTime;
private String endTime;
public static ContractPlanFragment create(String key) {
Bundle args = new Bundle();
args.putString(ARG_KEY, key);
ContractPlanFragment fragment = new ContractPlanFragment();
fragment.setArguments(args);
return fragment;
}
public ContractPlanFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
rootView = inflater.inflate(R.layout.fragment_contract_plan, container, false);
addPlan = (Button) rootView.findViewById(R.id.addNewPlan);
planRecyclerView = (RecyclerView) rootView.findViewById(R.id.plan_view);
planRecyclerView.setItemViewCacheSize(20);
planRecyclerView.setDrawingCacheEnabled(true);
planRecyclerView.setDrawingCacheQuality(View.DRAWING_CACHE_QUALITY_HIGH);
planRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
SharedPreferences prefs = getActivity().getSharedPreferences(MyPREFERENCES , MODE_PRIVATE);
plan_detials = prefs.getString("plan_detials", null);
palnDate = prefs.getString("Date", null);
startTime = prefs.getString("startTime", null);
endTime = prefs.getString("endTime", null);
Log.i("the valu" , palnDate + plan_detials);
Plan p = new Plan();
p.setPlanDetails( plan_detials);
p.setStartTime(palnDate);
p.setEndTime(startTime);
p.setPlanDate(endTime);
planList = new ArrayList<>();
planList.add(p);
planList.add(new Plan("تجربة ويا رب تزبط" ,"15/10/2018" ,"08:50" ,"13:20"));
mAdapter = new PlanRecyclerView(planList , getContext());
planRecyclerView.setAdapter(mAdapter);
addPlan.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Fragment addPlanFragment = new AddPlanFragment();
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.replace(R.id.create_contract_layout, addPlanFragment ); // give your fragment container id in first parameter
transaction.addToBackStack(null); // if written, this transaction will be added to backstack
transaction.commit();
}
});
return rootView;
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
if (!(activity instanceof PageFragmentCallbacks)) {
throw new ClassCastException("Activity must implement PageFragmentCallbacks");
}
mCallbacks = (PageFragmentCallbacks) activity;
}
@Override
public void onDetach() {
super.onDetach();
mCallbacks = null;
}
@Override
public void onItemClick(int position, View v) {
}
@Override
public void onResume() {
super.onResume();
}
}
这里是我填充数据的片段:
public class AddPlanFragment extends Fragment {
private TextView tvDisplayTime;
private TimePicker timePicker1;
private DatePicker plan_date;
View bottomSheetView;
static EditText addDateText;
static EditText startTime ;
private EditText plan_detials_text ;
Button savePlanBtn ,cancelBtn;
static EditText endTime;
SharedPreferences sharedpreferences;
public static final String MyPREFERENCES = "MyPrefs" ;
public AddPlanFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View rootView = inflater.inflate(R.layout.fragment_add_plan, container, false);
sharedpreferences = getActivity().getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);
addDateText = (EditText) rootView.findViewById(R.id.addDateText) ;
startTime = (EditText) rootView.findViewById(R.id.startTime);
endTime = (EditText) rootView.findViewById(R.id.endTime);
plan_detials_text = (EditText) rootView.findViewById(R.id.plan_detials_text);
savePlanBtn = (Button) rootView.findViewById(R.id.savePlanBtn);
cancelBtn = (Button) rootView.findViewById(R.id.cancelBtn);
addDateText.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
DialogFragment newFragment = new SelectDateFragment();
newFragment.show(getFragmentManager(), "DatePicker");
}
});
startTime.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
DialogFragment newFragment = new TimePickerFragment_start();
newFragment.show(getFragmentManager(), "DatePicker");
}
});
endTime.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
DialogFragment newFragment = new TimePickerFragment_end();
newFragment.show(getFragmentManager(), "DatePicker");
}
});
cancelBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
getActivity().onBackPressed();
}
});
savePlanBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
SharedPreferences.Editor editor = sharedpreferences.edit();
editor.putString("plan_detials", plan_detials_text.getText().toString());
editor.putString("Date", addDateText.getText().toString());
editor.putString("startTime", startTime.getText().toString());
editor.putString("endTime",endTime.getText().toString());
editor.commit();
Intent go = new Intent(getActivity(),CreateContractActivity.class);
// you pass the position you want the viewpager to show in the extra,
// please don't forget to define and initialize the position variable
// properly
go.putExtra("viewpager_position", 6);
startActivity(go);
// getActivity().onBackPressed();
}
});
return rootView;
}
public static class SelectDateFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener {
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
final Calendar calendar = Calendar.getInstance();
int yy = calendar.get(Calendar.YEAR);
int mm = calendar.get(Calendar.MONTH);
int dd = calendar.get(Calendar.DAY_OF_MONTH);
return new DatePickerDialog(getActivity(), this, yy, mm, dd);
}
public void onDateSet(DatePicker view, int yy, int mm, int dd) {
populateSetDate(yy, mm+1, dd);
}
public void populateSetDate(int year, int month, int day) {
addDateText.setText(month+"/"+day+"/"+year);
}
}
public static class TimePickerFragment_start extends DialogFragment implements TimePickerDialog.OnTimeSetListener{
@Override
public Dialog onCreateDialog(Bundle savedInstanceState){
// Get a Calendar instance
final Calendar calendar = Calendar.getInstance();
// Get the current hour and minute
int hour = calendar.get(Calendar.HOUR_OF_DAY);
int minute = calendar.get(Calendar.MINUTE);
TimePickerDialog tpd = new TimePickerDialog(getActivity(), android.R.style.Theme_Holo_Light_Dialog_NoActionBar,this,hour,minute,false);
// Return the TimePickerDialog
return tpd;
}
public void onTimeSet(TimePicker view, int hourOfDay, int minute){
// Do something with the returned time
startTime.setText(hourOfDay + ":" + minute);
}
}
public static class TimePickerFragment_end extends DialogFragment implements TimePickerDialog.OnTimeSetListener{
@Override
public Dialog onCreateDialog(Bundle savedInstanceState){
// Get a Calendar instance
final Calendar calendar = Calendar.getInstance();
// Get the current hour and minute
int hour = calendar.get(Calendar.HOUR_OF_DAY);
int minute = calendar.get(Calendar.MINUTE);
TimePickerDialog tpd = new TimePickerDialog(getActivity(), android.R.style.Theme_Holo_Light_Dialog_NoActionBar,this,hour,minute,false);
// Return the TimePickerDialog
return tpd;
}
public void onTimeSet(TimePicker view, int hourOfDay, int minute){
// Do something with the returned time
endTime.setText(hourOfDay + ":" + minute);
}
}
}
我希望有一种方法可以在保存数据后返回相同的片段,请提供任何帮助
答案 0 :(得分:1)
要在SavePlanBtn.setOnClickListener
中保存片段中的数据,请使用以下代码:
Bundle args = new Bundle();
args.putString("first","Here is your first String");
args.putString("second" , "Here is your second String");
Fragment newFragment = new yourFragment();
newFragment.setArguments(args);
getFragmentManager().popBackStack(); // this to back to the first fragment
然后在onCreateView
方法中接收片段中的数据,请使用:
if (getArguments() != null) {
String one = getArguments().getString("first");
String two = getArguments().getString("second");
}
不要忘记编辑此代码:transaction.addToBackStack(null);
致:
transaction.addToBackStack("string");
回到第一个片段。