Android viewpager中的选项卡未按顺序响应

Android viewpager中的选项卡未按顺序响应,android,android-fragments,tabs,Android,Android Fragments,Tabs,在我的android应用程序中,我使用了一个包含4个选项卡的选项卡滑动视图。第一个选项卡显示UI上的数据,并通过AsyncTask调用WS,WS返回的数据填充pojo,此pojo传递给其他选项卡(片段) 我面临的问题是,当我单击第一个选项卡时,选项卡没有按顺序执行,第一个和第二个选项卡在调用WS之前执行,由于这种奇怪的行为,我无法在第二个选项卡中获取数据 活动: public class TabSwipeActivity extends FragmentActivity implement

在我的android应用程序中,我使用了一个包含4个选项卡的选项卡滑动视图。第一个选项卡显示UI上的数据,并通过AsyncTask调用WS,WS返回的数据填充pojo,此pojo传递给其他选项卡(片段) 我面临的问题是,当我单击第一个选项卡时,选项卡没有按顺序执行,第一个和第二个选项卡在调用WS之前执行,由于这种奇怪的行为,我无法在第二个选项卡中获取数据

活动:

   public class TabSwipeActivity extends FragmentActivity implements ActionBar.TabListener, DetailsFoeFragment.ExistingFeatureDataInt

{
    ActionBar actionbar;
    ViewPager viewpager;
    FragmentPageAdapter ft;
    ExistingData existingData;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.tabswipe);
        ArrayList<String> detailList =this.getIntent().getStringArrayListExtra("FeatureData");      

        viewpager = (ViewPager) findViewById(R.id.pager1);
        ft = new FragmentPageAdapter(getSupportFragmentManager());

        if(detailList!=null)
            ft.setDetailList(detailList);

    //  ft.setExistingData(getExistingData());
        actionbar = getActionBar();
        viewpager.setAdapter(ft);

        actionbar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
        actionbar.setDisplayOptions(0, ActionBar.DISPLAY_SHOW_TITLE);

        actionbar.addTab(actionbar.newTab().setText("Details").setTabListener(this),0,true);
        actionbar.addTab(actionbar.newTab().setText("Action Taken").setTabListener(this),1,false);
        actionbar.addTab(actionbar.newTab().setText("Before Photos").setTabListener(this),2,false);
        actionbar.addTab(actionbar.newTab().setText("After Photos").setTabListener(this),3,false);

        viewpager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {

            public void onPageScrollStateChanged(int arg0) {
                // TODO Auto-generated method stub

            }

            public void onPageScrolled(int arg0, float arg1, int arg2) {
                // TODO Auto-generated method stub

            }

            public void onPageSelected(int arg0) {
                actionbar.setSelectedNavigationItem(arg0);

            }

        });
    }

    public void onTabSelected(Tab tab, FragmentTransaction ft) {
        viewpager.setCurrentItem(tab.getPosition());

    }

    public void onTabUnselected(Tab tab, FragmentTransaction ft) {
        // TODO Auto-generated method stub

    }

    public void onTabReselected(Tab tab, FragmentTransaction ft) {
        // TODO Auto-generated method stub

    }


////
Adapter:
/////



public class FragmentPageAdapter extends FragmentPagerAdapter {


    Bundle bundle= new Bundle();
    ExistingData existingData = new ExistingData();;

    public FragmentPageAdapter(FragmentManager fm) {
        super(fm);
        // TODO Auto-generated constructor stub
    }
    Fragment fragment;
    @Override
    public Fragment getItem(int arg0) {
        // TODO Auto-generated method stub
        switch (arg0) {
        case 0:

            fragment = new DetailsFoeFragment();
            bundle.putStringArrayList("details",detailList);         
            fragment.setArguments(this.bundle);
            return fragment;
        case 1:
              fragment= new ActionFoeFragment();        
              bundle.putParcelableArrayList("actions",(ArrayList)existingData.getActionList());
            //  bundle.putParcelableArrayList("actions",(ArrayList)existingData.getActionList());
             // bundle.putString("featureId",detailList.get(2));         
              fragment.setArguments(this.bundle);
              return fragment;

        case 2:
            fragment = new PhotoBeforeFoeFragment();
        //  bundle.putStringArrayList("beforeImage",null);
            bundle.putStringArrayList("beforeImage",(ArrayList<String>)existingData.getBeforeImagePath());       
            fragment.setArguments(this.bundle);
            return fragment;
        case 3:
            fragment = new PhotoAfterFoeFragment();
            bundle.putStringArrayList("afterImage",(ArrayList<String>)existingData.getAfterImagePath());         
            fragment.setArguments(this.bundle);
            return fragment;    

        default:
            break;
        }
        return null;
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return 4;
    }

    ArrayList<String> detailList;
    public void setDetailList(ArrayList<String> detailList) {
        this.detailList = detailList;
    }


    public void setExistingData(ExistingData existingData) {
        this.existingData = existingData;
    }
    @Override
    public int getItemPosition(Object object) {
            return POSITION_NONE;
    } 

}
公共类选项卡SwipeActivity扩展了FragmentActivity实现了ActionBar.TabListener、DetailsForAgment.ExistingFeatureDataInt
{
ActionBar ActionBar;
查看页面查看页面;
零碎的碎片;
现有数据现有数据;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.tabswipe);
ArrayList detailList=this.getIntent().getStringArrayListExtra(“FeatureData”);
viewpager=(viewpager)findViewById(R.id.pager1);
ft=新的FragmentPageAdapter(getSupportFragmentManager());
if(detailList!=null)
ft.setDetailList(详细列表);
//ft.setExistingData(getExistingData());
actionbar=getActionBar();
viewpager.setAdapter(英尺);
actionbar.setNavigationMode(actionbar.NAVIGATION\u MODE\u选项卡);
actionbar.setDisplayOptions(0,actionbar.DISPLAY\u SHOW\u TITLE);
actionbar.addTab(actionbar.newTab().setText(“详细信息”).setTabListener(this),0,true);
actionbar.addTab(actionbar.newTab().setText(“采取的操作”).setTabListener(this),1,false);
actionbar.addTab(actionbar.newTab().setText(“照片之前”).setTabListener(this),2,false);
actionbar.addTab(actionbar.newTab().setText(“照片之后”).setTabListener(this),3,false);
viewpager.setOnPageChangeListener(新的viewpager.OnPageChangeListener(){
公共无效onPageScrollStateChanged(int arg0){
//TODO自动生成的方法存根
}
已滚动页面上的公共void(int arg0、float arg1、int arg2){
//TODO自动生成的方法存根
}
已选择页面上的公共无效(int arg0){
actionbar.setSelectedNavigationItem(arg0);
}
});
}
已选择的公共事务(选项卡,碎片事务ft){
setCurrentItem(tab.getPosition());
}
已选择公共空页(选项卡,碎片事务ft){
//TODO自动生成的方法存根
}
已重新选择公共无效页签(页签,碎片事务){
//TODO自动生成的方法存根
}
////
适配器:
/////
公共类FragmentPageAdapter扩展了FragmentPagerAdapter{
Bundle=新Bundle();
ExistingData ExistingData=新的ExistingData();;
公共碎片页面适配器(碎片管理器fm){
超级(fm);
//TODO自动生成的构造函数存根
}
片段;
@凌驾
公共片段getItem(int arg0){
//TODO自动生成的方法存根
开关(arg0){
案例0:
fragment=新的detailsfefragment();
bundle.putStringArrayList(“详细信息”,详细列表);
fragment.setArguments(this.bundle);
返回片段;
案例1:
fragment=新ActionFoeFragment();
bundle.putParcelableArrayList(“操作”,(ArrayList)existingData.getActionList());
//bundle.putParcelableArrayList(“操作”,(ArrayList)existingData.getActionList());
//bundle.putString(“featureId”,detailList.get(2));
fragment.setArguments(this.bundle);
返回片段;
案例2:
fragment=新的photobeforefeefragment();
//bundle.putStringArrayList(“beforeImage”,null);
bundle.putStringArrayList(“beforeImage”,(ArrayList)existingData.getBeforeImagePath());
fragment.setArguments(this.bundle);
返回片段;
案例3:
片段=新片段();
bundle.putStringArrayList(“afterImage”,(ArrayList)existingData.getAfterImagePath());
fragment.setArguments(this.bundle);
返回片段;
违约:
打破
}
返回null;
}
@凌驾
public int getCount(){
//TODO自动生成的方法存根
返回4;
}
ArrayList详细列表;
公共void setDetailList(ArrayList detailList){
this.detailList=detailList;
}
public void setExistingData(ExistingData ExistingData){
this.existingData=existingData;
}
@凌驾
public int getItemPosition(对象){
返回位置\无;
} 
}
请提供投入 谢谢

一个解决方案:

  • 在活动中,使用Asynctask运行WS,然后调用需要数据更新视图的片段

     class yourActivity {
    
    
         @Override 
         public void onCreate(Bundle savedInstanceState) {
    
            ... ...
    
            //set up your tabs with fragments
                ... ...
    
            //WS: Your asynctask requesting data from server 
            (new AsyncTask<... ...>() {
    
              ... ...
    
              @Override
              protected void onPostExecute(dataSet) {
                  //here you have got your data set from server then inform tabs to update
                  YourFragment frag = (YourFragment)getSupportFragmentManager().findFragmentById(R.id.your_fragment);
    
                  frag.updateView(dataSet);
              } 
    
           }).execute();
    
    
       }
    
    
    }
    

希望此解决方案适用于您。

滑动视图页面在显示之前执行,但在第一个选项卡中调用WS之前,它会执行2个选项卡:(请建议更好的解决方案您的第二个选项卡基于第一个选项卡的执行结果,这不是很好的逻辑。这些选项卡应该是并行的。您能建议任何解决方案吗?从WS响应中,我必须填充4个选项卡。我不想在每个选项卡中调用WS确定我给出了一个解决方案作为回答
 class YourFragment extebds Fragment{

      ... ...

     public void updateView(dataSet)
     {
        //update this fragment view with dataSet
        //and stop your waiting dialog
     }
 }