我有两个片段并在滑动标签上使用它我希望将对象从片段(1)传递到片段(2),此对象来自(Web服务)
此活动负责标签
public class ChartActivity extends AppCompatActivity {
private static final String TAG="ChartActivity";
private ViewPager mPager;
private SlidingTabLayout tabLayout;
private Toolbar toolbarChart;
private TextView txtToolbar;
private AdapterTabs adapterTabs;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_chart);
Log.e(TAG,"starting to Create Chart");
setupToolBar();
setupTabs();
}
// setup ToolBar
public void setupToolBar() {
toolbarChart = (Toolbar) findViewById(R.id.app_bar_chart); // get instance from view
setSupportActionBar(toolbarChart); // set Toolbar in supporting ActionBar
getSupportActionBar().setDisplayShowTitleEnabled(false);
txtToolbar=(TextView)toolbarChart.findViewById(R.id.toolbar_title);
txtToolbar.setText("Dashboard");
}
public void setupTabs(){
tabLayout=(SlidingTabLayout)findViewById(R.id.tabs);
mPager=(ViewPager)findViewById(R.id.viewPager);
adapterTabs= new AdapterTabs(this,getSupportFragmentManager());
mPager.setAdapter(adapterTabs);
tabLayout.setDistributeEvenly(true);
tabLayout.setCustomTabColorizer(new SlidingTabLayout.TabColorizer() {
@Override
public int getIndicatorColor(int position) {
return getResources().getColor(R.color.colorAccent);
}
});
tabLayout.setViewPager(mPager);
}
public AdapterTabs getAdapterTabs() {
return adapterTabs;
}
}
此片段(1)
public class InpatientFragment extends Fragment {
private final static String INPATIENT_STATE="inpatient";
public final static String OutPatient_STATE="outpatient";
private RequestQueue request;
private PieChart chart;
ArrayList<Patient> inpatients;
ArrayList<Patient> outPatient; //outPatient=====> show it in fragment(2)
private ProgressDialog progressDialog;
private VolleySingleton singleton;
private final static String TAG = "InpatientFragment";
public InpatientFragment() {
// Required empty public constructor
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
progressDialog = new ProgressDialog(getActivity());
progressDialog.setIndeterminate(true);
progressDialog.setMessage("Load Data...");
requsetDatachart();
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View layout = inflater.inflate(R.layout.fragment_inpatient, container, false);
chart = (PieChart) layout.findViewById(R.id.pieChart);
chart.setCenterTextSize(10);
chart.setCenterText("Inpatient");
chart.setDrawEntryLabels(true);
chart.setHoleRadius(2.5f);
return layout;
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
}
private void requsetDatachart() {
singleton = VolleySingleton.getInstance();
request = singleton.getRequestQueue();
progressDialog.show();
JsonObjectRequest objectRequest = new JsonObjectRequest(Request.Method.GET, MyApplication.APP_URL + "api/getOverview", null,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
inpatients = PatientParser.parserJsonPatient(response, "INPATIENT", 1);
Log.e(TAG, inpatients.size()+"");
outPatient=PatientParser.parserJsonPatient(response,"OUTPATIENT",0);
addDataToChart(inpatients);
progressDialog.dismiss();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.e(TAG, error.getMessage());
progressDialog.dismiss();
}
});
request.add(objectRequest);
}
public void addDataToChart(List<Patient> patients){
ArrayList<PieEntry>pieEntries=new ArrayList<>();
for (int i=0;i<patients.size();i++){
pieEntries.add(new PieEntry(Float.parseFloat(patients.get(i).getResult_val()),patients.get(i).getName_en()));
}
// add color to data set
ArrayList<Integer> colors=new ArrayList<>();
colors.add(Color.BLUE);
colors.add(Color.GREEN);
colors.add(Color.YELLOW);
// form data set slicing between containing and size of text
PieDataSet dataSet=new PieDataSet(pieEntries,"InPatient");
dataSet.setSliceSpace(3);
dataSet.setValueTextSize(13);
dataSet.setColors(colors);
/// form of chart through legend object
Legend legend=chart.getLegend();
legend.setForm(Legend.LegendForm.CIRCLE);
// set data set to pieData
PieData data=new PieData(dataSet);
chart.setData(data);
chart.invalidate();
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putParcelableArrayList(INPATIENT_STATE,inpatients);
}
}
和这个片段2我想在这个片段中显示对象 门诊==&GT;片段(1)中的列表
public class OutpatientFragment extends Fragment {
private final static String TAG = "OutpatientFragment";
private static final String ARG_PARAM1 = "param1";
ArrayList<Patient> outPatient;
private RequestQueue request;
private PieChart chart;
private ProgressDialog progressDialog;
private VolleySingleton singleton;
public OutpatientFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View layout=inflater.inflate(R.layout.fragment_outpatient, container, false);
//getArguments().getBundle(InpatientFragment.OutPatient_STATE).getParcelableArrayList();
chart = (PieChart) layout.findViewById(R.id.pieChart);
chart.setCenterTextSize(10);
chart.setCenterText("Outpatient");
chart.setDrawEntryLabels(true);
chart.setHoleRadius(2.5f);
return layout;
}
@Override
public void onResume() {
super.onResume();
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
progressDialog = new ProgressDialog(getActivity());
progressDialog.setIndeterminate(true);
progressDialog.setMessage("Load Data...");
}
}
答案 0 :(得分:0)
我认为您可以使用EventBus。例如
在接收数据时InpatientFragment
致电EventBus.getDefault().post(new MessageEvent(<Your object>))
。
在OutpatientFragment
创建接收数据的方法中,例如
@Subscribe(threadMode = ThreadMode.MAIN)
public void onMessageEvent(MessageEvent event) {/* Do something */};