问题
i have created my project using this material design example. i have modified the code and now i have 5 tabs now. And now i have created 5 fragment classes and loading it on the view pager. now what i want is, i want to pass the selected sliding menu position as a parameter to the fragment which i am loading. Here the problem is, i can pass the parameter(using Bundle) to the FragmentPagerAdapter , but it is not reloading the fragments based on the parameter i'm passing! the thing is it is not even executing the page loading part in FragmentPagerAdapter!
How can i reload my fragments on the Taps based on the parameter i'm passing? FYI, based on the parameter value that i'm passing, i'm doing some processing and i want to show the processed fragment page on the tabs.
please help this is the series problem i'm facing it right now! Thanks in advance :)
Here is my adapter code
public class ViewPagerAdapter1 extends FragmentPagerAdapter {
final int PAGE_COUNT =6;
private String titles[] ;
Bundle b2;
public ViewPagerAdapter1(FragmentManager fm, String[] titles2, Bundle b) {
super(fm);
titles=titles2;
this.b2=b;
}
@Override
public Fragment getItem(int position) {
switch (position) {
case 0:
return new GridTableActivity();
case 1:
Bundle selected1 = new Bundle();
selected1.putInt("selected",0);
GridTableActivity gridTableActivity = new GridTableActivity();
gridTableActivity.setArguments(selected1);
return gridTableActivity;
case 2:
Bundle selected2 = new Bundle();
selected2.putInt("selected",0);
DownTimeTable downTimeTable = new DownTimeTable();
downTimeTable.setArguments(selected2);
return downTimeTable;
case 3:
Bundle selected3 = new Bundle();
selected3.putInt("selected",0);
GraphActivity graphActivity = new GraphActivity();
graphActivity.setArguments(selected3);
return graphActivity;
case 4:
Bundle selected4 = new Bundle();
selected4.putInt("selected",0);
BarChartActivity barChartActivity = new BarChartActivity();
barChartActivity.setArguments(selected4);
return barChartActivity;
case 5:
Bundle selected5 = new Bundle();
selected5.putInt("selected",0);
PieChartActivity pieChartActivity = new PieChartActivity();
pieChartActivity.setArguments(selected5);
return pieChartActivity;
}
return null;
}
public CharSequence getPageTitle(int position) {
return titles[position];
}
@Override
public int getCount() {
return PAGE_COUNT;
}}
currently i'm doing like this! i have 9 items on the drawer & created 9 different adapter classes!(so sad sry abt this! :|)
public class SampleActivity extends ActionBarActivity {
private DrawerLayout mDrawerLayout;
private ActionBarDrawerToggle drawerToggle;
private ListView mDrawerList;
ViewPager pager1,pager2,pager3,pager4,pager5,pager6,pager7,pager8,pager9;
TextView tv;
String proj ="Connect";
private String titles0[] = new String[]{"Home",
"DELIVERY", "DOWNTIME", "Graph 1"
, "Graph 2", "Graph 3"};
//private Toolbar toolbar;
SlidingTabLayout slidingTabLayout;
DatabaseHelper db;
LinearLayout lvt;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sample);
db= new DatabaseHelper(getApplicationContext());
db.getReadableDatabase();
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerList = (ListView) findViewById(R.id.navdrawer);
tv=(TextView)findViewById(R.id.tv);
// toolbar = (Toolbar) findViewById(R.id.toolbar);
// if (toolbar != null) {
// setSupportActionBar(toolbar);
// toolbar.setNavigationIcon(R.drawable.ic_ab_drawer);
// }
pager1 = (ViewPager) findViewById(R.id.viewpager1);
pager2 = (ViewPager) findViewById(R.id.viewpager2);
pager3 = (ViewPager) findViewById(R.id.viewpager3);
pager4 = (ViewPager) findViewById(R.id.viewpager4);
pager5 = (ViewPager) findViewById(R.id.viewpager5);
pager6 = (ViewPager) findViewById(R.id.viewpager6);
pager7 = (ViewPager) findViewById(R.id.viewpager7);
pager8 = (ViewPager) findViewById(R.id.viewpager8);
pager9 = (ViewPager) findViewById(R.id.viewpager9);
slidingTabLayout = (SlidingTabLayout) findViewById(R.id.sliding_tabs);
lvt=(LinearLayout)findViewById(R.id.lvt);
final Bundle good = new Bundle();
good.putInt("kpr",0);
pager1.setVisibility(View.VISIBLE);
pager1.setAdapter(new ViewPagerAdapter1(getSupportFragmentManager(), titles0, good));
slidingTabLayout.setViewPager(pager1);
slidingTabLayout.setCustomTabColorizer(new SlidingTabLayout.TabColorizer() {
@Override
public int getIndicatorColor(int position) {
return Color.WHITE;
}
});
//drawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, toolbar, R.string.app_name, R.string.app_name);
mDrawerLayout.setDrawerListener(drawerToggle);
String[] values = new String[]{"OVERALL", "SPEED", "CONNECT", "ORBIT","SPEED INT","EXPRESS","ERROR CORRECTION","PRODEX","ORBIT AUDIT"};
final ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, android.R.id.text1, values);
mDrawerList.setAdapter(adapter);
mDrawerList.setBackgroundColor(getResources().getColor(R.color.blue));
// toolbar.setBackgroundColor(getResources().getColor(R.color.blue));
slidingTabLayout.setBackgroundColor(getResources().getColor(R.color.blue));
mDrawerList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
switch (position) {
case 0:
Toast.makeText(getApplicationContext(), "Overall Selected", Toast.LENGTH_LONG).show();
mDrawerLayout.closeDrawer(Gravity.START);
tv.setText("OVERALL");
good.putInt("kpr",0);
pager1.setVisibility(View.VISIBLE);
pager2.setVisibility(View.GONE);
pager3.setVisibility(View.GONE);
pager4.setVisibility(View.GONE);
pager5.setVisibility(View.GONE);
pager6.setVisibility(View.GONE);
pager7.setVisibility(View.GONE);
pager8.setVisibility(View.GONE);
pager9.setVisibility(View.GONE);
pager1.setAdapter(new ViewPagerAdapter1(getSupportFragmentManager(), titles0, good));
slidingTabLayout.setViewPager(pager1);
pager1.setCurrentItem(1);
break;
case 1:
Toast.makeText(getApplicationContext(), "Speed Selected", Toast.LENGTH_LONG).show();
mDrawerLayout.closeDrawer(Gravity.START);
// good.putString("Project","SPEED");
good.putInt("kpr",1);
tv.setText("SPEED");
pager1.setVisibility(View.GONE);
pager2.setVisibility(View.VISIBLE);
pager3.setVisibility(View.GONE);
pager4.setVisibility(View.GONE);
pager5.setVisibility(View.GONE);
pager6.setVisibility(View.GONE);
pager7.setVisibility(View.GONE);
pager8.setVisibility(View.GONE);
pager9.setVisibility(View.GONE);;
pager2.setAdapter(new ViewPagerAdapter2(getSupportFragmentManager(), titles0, good));
slidingTabLayout.setViewPager(pager2);
pager2.setCurrentItem(1);
break;
case 2:
Toast.makeText(getApplicationContext(), "Connect Selected", Toast.LENGTH_LONG).show();
mDrawerLayout.closeDrawer(Gravity.START);
// good.putString("Project","CONNECT");
good.putInt("kpr",2);
tv.setText("CONNECT");
pager1.setVisibility(View.GONE);
pager2.setVisibility(View.GONE);
pager3.setVisibility(View.VISIBLE);
pager4.setVisibility(View.GONE);
pager5.setVisibility(View.GONE);
pager6.setVisibility(View.GONE);
pager7.setVisibility(View.GONE);
pager8.setVisibility(View.GONE);
pager9.setVisibility(View.GONE);
pager3.setAdapter(new ViewPagerAdapter3(getSupportFragmentManager(), titles0, good));
slidingTabLayout.setViewPager(pager3);
pager3.setCurrentItem(1);
break;
case 3:
Toast.makeText(getApplicationContext(), "Orbit Selected", Toast.LENGTH_LONG).show();
mDrawerLayout.closeDrawer(Gravity.START);
tv.setText("ORBIT");
// good.putString("Project","ORBIT");
good.putInt("kpr",3);
pager1.setVisibility(View.GONE);
pager2.setVisibility(View.GONE);
pager3.setVisibility(View.GONE);
pager4.setVisibility(View.VISIBLE);
pager5.setVisibility(View.GONE);
pager6.setVisibility(View.GONE);
pager7.setVisibility(View.GONE);
pager8.setVisibility(View.GONE);
pager9.setVisibility(View.GONE);
pager4.setAdapter(new ViewPagerAdapter4(getSupportFragmentManager(), titles0, good));
slidingTabLayout.setViewPager(pager4);
pager4.setCurrentItem(1);
break;
case 4:
Toast.makeText(getApplicationContext(), "SPEED INT Selected", Toast.LENGTH_LONG).show();
mDrawerLayout.closeDrawer(Gravity.START);
tv.setText("SPEED INT");
// good.putString("Project","SPEEDINT");
good.putInt("kpr",4);
pager1.setVisibility(View.GONE);
pager2.setVisibility(View.GONE);
pager3.setVisibility(View.GONE);
pager4.setVisibility(View.GONE);
pager5.setVisibility(View.VISIBLE);
pager6.setVisibility(View.GONE);
pager7.setVisibility(View.GONE);
pager8.setVisibility(View.GONE);
pager9.setVisibility(View.GONE);
pager5.setAdapter(new ViewPagerAdapter5(getSupportFragmentManager(), titles0, good));
slidingTabLayout.setViewPager(pager5);
pager5.setCurrentItem(1);
break;
case 5:
Toast.makeText(getApplicationContext(), "EXPRESS Selected", Toast.LENGTH_LONG).show();
mDrawerLayout.closeDrawer(Gravity.START);
tv.setText("EXPRESS");
good.putInt("kpr",5);
pager1.setVisibility(View.GONE);
pager2.setVisibility(View.GONE);
pager3.setVisibility(View.GONE);
pager4.setVisibility(View.GONE);
pager5.setVisibility(View.GONE);
pager6.setVisibility(View.VISIBLE);
pager7.setVisibility(View.GONE);
pager8.setVisibility(View.GONE);
pager9.setVisibility(View.GONE);
pager6.setAdapter(new ViewPagerAdapter6(getSupportFragmentManager(), titles0, good));
slidingTabLayout.setViewPager(pager6);
pager6.setCurrentItem(1);
break;
case 6:
Toast.makeText(getApplicationContext(), "ERROR CORRECTION Selected", Toast.LENGTH_LONG).show();
mDrawerLayout.closeDrawer(Gravity.START);
tv.setText("ERROR CORRECTION");
good.putInt("kpr",6);
pager1.setVisibility(View.GONE);
pager2.setVisibility(View.GONE);
pager3.setVisibility(View.GONE);
pager4.setVisibility(View.GONE);
pager5.setVisibility(View.GONE);
pager6.setVisibility(View.GONE);
pager7.setVisibility(View.VISIBLE);
pager8.setVisibility(View.GONE);
pager9.setVisibility(View.GONE);
pager7.setAdapter(new ViewPagerAdapter7(getSupportFragmentManager(), titles0, good));
slidingTabLayout.setViewPager(pager7);
pager7.setCurrentItem(1);
break;
case 7:
Toast.makeText(getApplicationContext(), "PRODEX Selected", Toast.LENGTH_LONG).show();
mDrawerLayout.closeDrawer(Gravity.START);
tv.setText("PRODEX");
good.putInt("kpr",7);
pager1.setVisibility(View.GONE);
pager2.setVisibility(View.GONE);
pager3.setVisibility(View.GONE);
pager4.setVisibility(View.GONE);
pager5.setVisibility(View.GONE);
pager6.setVisibility(View.GONE);
pager7.setVisibility(View.GONE);
pager8.setVisibility(View.VISIBLE);
pager9.setVisibility(View.GONE);
pager8.setAdapter(new ViewPagerAdapter8(getSupportFragmentManager(), titles0, good));
slidingTabLayout.setViewPager(pager8);
pager8.setCurrentItem(1);
break;
case 8:
Toast.makeText(getApplicationContext(), "ORBIT AUDIT Selected", Toast.LENGTH_LONG).show();
mDrawerLayout.closeDrawer(Gravity.START);
tv.setText("ORBIT AUDIT");
good.putInt("kpr",8);
pager1.setVisibility(View.GONE);
pager2.setVisibility(View.GONE);
pager3.setVisibility(View.GONE);
pager4.setVisibility(View.GONE);
pager5.setVisibility(View.GONE);
pager6.setVisibility(View.GONE);
pager7.setVisibility(View.GONE);
pager8.setVisibility(View.GONE);
pager9.setVisibility(View.VISIBLE);
pager9.setAdapter(new ViewPagerAdapter9(getSupportFragmentManager(), titles0, good));
slidingTabLayout.setViewPager(pager9);
pager9.setCurrentItem(1);
break;
}
}
});
}
@Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
drawerToggle.syncState();
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
drawerToggle.onConfigurationChanged(newConfig);
}
}
回答1:
i figured out! wasted a lot of time for a simple solution! I should use FragmentStatePagerAdapter instead of FragmentPagerAdapter!!! uufff! :/
来源:https://stackoverflow.com/questions/30885610/fragmentpageradapter-reload-the-processed-fragment-on-the-tabs-based-on-the-para