У меня есть BusActivity
, в которой я уже знаю значение переменной position
. У нее есть дочерний фрагмент BusStopFragment
, который я создаю в методе SetupViewPager()
. Вопрос состоит в том, как мне передать значение этой переменной position
в метод setInitialData()
этого фрагмента?
Код BusActivity
:
public class BusActivity extends AppCompatActivity {
private Toolbar toolbar;
private TabLayout tabLayout;
private ViewPager viewPager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_bus);
Intent intent = getIntent();
int position;
int DefaultValue = 0;
position = intent.getIntExtra("EXTRA_POS", DefaultValue);
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
ActionBar actionBar = getSupportActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
viewPager = (ViewPager) findViewById(R.id.viewpager);
setupViewPager(viewPager);
tabLayout = (TabLayout) findViewById(R.id.tablayout);
tabLayout.setupWithViewPager(viewPager);
}
@Override
public boolean onOptionsItemSelected(MenuItem item){
int id = item.getItemId();
if(id == android.R.id.home){
finish();
}
return super.onOptionsItemSelected(item);
}
private void setupViewPager(ViewPager viewPager){
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
adapter.addFragment(new BusStopFragment(), "BusStop");
adapter.addFragment(new TableFragment(), "Table");
adapter.addFragment(new MapFragment(), "Map");
viewPager.setAdapter(adapter);
viewPager.setOffscreenPageLimit(adapter.getCount());
}
}
Код BusStopFragment
:
public class BusStopFragment extends Fragment {
private List<Station> stations = new ArrayList();
ListView stationsList;
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setInitialData();
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
View view = inflater.inflate(R.layout.busstopfragment, container, false);
stationsList = (ListView) view.findViewById(R.id.stationsList);
Activity activity = new Activity();
StationAdapter stationAdapter = new StationAdapter(getActivity().getApplicationContext(), R.layout.list_item, stations);
stationsList.setAdapter(stationAdapter);
return view;
}
private void setInitialData(){
stations.add(new Station("01", "10 grn", R.drawable.ic_launcher_background));
stations.add(new Station("02", "15 grn", R.drawable.ic_launcher_background));
stations.add(new Station("03", "20 grn", R.drawable.ic_launcher_background));
}
}