First off, you have to forgive me, because my English is very bad.
I'll try to explain the issue:
I have an ActionBarActivity
with two pages (ViewPager
).
Every page has a Fragment
.
My problem is: how to use the elements of each Fragment
from this ActionBarActivity
?
My code is this:
ActionBarActivity.java
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.manage_devices);
mActionbar = getSupportActionBar();
mActionbar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
mPager = (ViewPager) findViewById(R.id.pager);
FragmentManager fm = getSupportFragmentManager();
MyFragmentPagerAdapter fragmentPagerAdapter = new MyFragmentPagerAdapter(fm);
mPager.setAdapter(fragmentPagerAdapter);
mActionbar.setDisplayShowTitleEnabled(true);
ActionBar.TabListener tabListener = new ActionBar.TabListener() {
@Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {}
@Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
mPager.setCurrentItem(tab.getPosition());}
@Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {}
};
mActionbar.addTab(mActionbar.newTab().setText("SENSORS").setTabListener(tabListener));
mActionbar.addTab(mActionbar.newTab().setText("SIGNALS").setTabListener(tabListener));
/** Defining a listener for pageChange */
ViewPager.SimpleOnPageChangeListener pageChangeListener = new ViewPager.SimpleOnPageChangeListener(){
**@Override
public void onPageSelected(int position) {
super.onPageSelected(position);
if(position == 0 & !hecho){
currView = mPager.getChildAt(mPager.getCurrentItem());
DevicesListView = (ListView) currView.findViewById(R.id.DevicesListView);
DevicesListView.setAdapter(DevicesAdapter);
DevicesListView.setOnItemClickListener(DeviceOnItemClickListener);
hecho = true;}
else if(!hecho){
mGraph = (GraphView)findViewById(R.id.graph);
}
mActionbar.setSelectedNavigationItem(position);
}**
};
/** Setting the pageChange listener to the viewPager */
mPager.setOnPageChangeListener(pageChangeListener);
}
To get a Listview
, I access it in OnPageSelected
.
But this is bad for me, because I need to change a page for getting the Listview
and build it.
ManageFragment.java
public class ManageFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Log.d("","Entra");
return inflater.inflate(R.layout.manage_fragment, null);
}
}
My Listview
is in the manage_fragment
layout.
This can help: Illustration
Thank you very much!!!
If your fragment already created you can just use findViewById() in your activity and just find the view you want.
So you should inform your activity that the fragment view already created through an interface for example:
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
try {
mListener = (OnViewCreatedListener) activity;
} catch (ClassCastException e) {
throw new ClassCastException(activity.toString() + " must implement OnArticleSelectedListener");
}
}
And then call the interface after Fragment's onCreateView.