FragmentPagerAdapter根据参数值重新加载选项卡上的已处理片段

时间:2015-06-17 07:58:58

标签: android android-fragments android-viewpager fragmentpageradapter pagerslidingtabstrip

我使用InvariantCulture材料设计示例创建了我的项目。我修改了代码,现在我有5个标签。现在我创建了5个片段类并将其加载到视图寻呼机上。现在我想要的是,我想将选定的滑动菜单位置作为参数传递给我正在加载的片段。这里的问题是,我可以将参数(使用Bundle)传递给FragmentPagerAdapter,但它不会根据我传递的参数重新加载片段!事情是它甚至没有在FragmentPagerAdapter中执行页面加载部分!

如何根据我传递的参数在Taps上重新加载我的片段? 仅供参考,基于我传递的参数值,我正在进行一些处理,我想在选项卡上显示已处理的片段页面。

请帮助这是我现在面对的系列问题! 在此先感谢:)

这是我的适配器代码

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;
}}

目前我这样做!我的抽屉里有9件物品。创建了9个不同的适配器类!(对此非常悲伤!:|)

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 个答案:

答案 0 :(得分:4)

我发现了!浪费了大量时间来寻找简单的解决方案!我应该使用FragmentStatePagerAdapter而不是FragmentPagerAdapter! uufff! :/