2014-01-23 56 views
0

我有一個活動與ViewPager內兩個可滑動的片段。 我有一個FragmentPagerAdapter的實現,它處理ViewPager中的片段。 問題是,我啓動了片段A中的Intent(zxing條形碼掃描器),它調用父活動的onActivityResult()(而不是片段,我嘗試了很多東西,但它不起作用!)。片段的查看成員null配置更改,內ViewPager

無論如何,通過Activity的onActivityResult()我從ViewPager獲取片段,調用其中的一個方法,它使用掃描器結果更新搜索視圖。該片段是牽強,但我想在破片A到更新搜索查看是空

這裏是我的班,一些地方爲了簡單去除(配置變化,由故意觸發後):

public class TabsAdapter extends FragmentPagerAdapter { 
    private final Context mContext; 
    private final ActionBar mActionBar; 
    private final ViewPager mViewPager; 
    private final ArrayList<TabInfo> mTabs = new ArrayList<TabInfo>(); 
    private final String TAG = "21st Polling:"; 

    static final class TabInfo{ 
     private final Class<?> clss; 
     private final Bundle args; 

     TabInfo(Class<?> _class, Bundle _args){ 
      clss = _class; 
      args = _args; 
     } 
    } 

    public TabsAdapter(SherlockFragmentActivity fa, ViewPager pager) { 
     super(fa.getSupportFragmentManager()); 
     mContext = fa; 
     mActionBar = fa.getSupportActionBar(); 
     mViewPager = pager; 
     mViewPager.setAdapter(this); 
     mViewPager.setOffscreenPageLimit(3); 
     mViewPager.setOnPageChangeListener(this); 
    } 

    public void addTab(ActionBar.Tab tab, Class<?> clss, Bundle args){ 
     TabInfo info = new TabInfo(clss, args); 
     tab.setTag(info); 
     tab.setTabListener(this); 
     mTabs.add(info); 
     mActionBar.addTab(tab); 
     notifyDataSetChanged(); 
    } 

    @Override 
    public void onPageSelected(int position) { 
     mActionBar.setSelectedNavigationItem(position); 

    } 

    @Override 
    public void onTabSelected(Tab tab, FragmentTransaction ft) { 
     mViewPager.setCurrentItem(tab.getPosition()); 
     Log.v(TAG, "clicked"); 
     Object tag = tab.getTag(); 
     for (int i = 0; i<mTabs.size(); i++){ 
      if (mTabs.get(i) == tag){ 
       mViewPager.setCurrentItem(i); 
      } 
     } 

    } 
} 

Acivity這主機片段:

public class OrderActivity extends SherlockFragmentActivity implements 
     OrderData { 

    private ViewPager mViewPager; 
    private TabsAdapter mTabsAdapter; 
    private Tab cartTab; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS); 

     mViewPager = new ViewPager(this); 
     mViewPager.setId(R.id.pager); 
     setContentView(mViewPager); 

    final ActionBar bar = getSupportActionBar(); 
    bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS); 
    mTabsAdapter = new TabsAdapter(this, mViewPager); 
    mTabsAdapter.addTab(bar.newTab().setText("A"), 
      A.class, null); 
    mTabsAdapter.addTab(bar.newTab().setText("B(0)"), 
      B.class, null); 

    } 


    public void onActivityResult(int requestCode, int resultCode, Intent intent) { 
     IntentResult scanResult = IntentIntegrator.parseActivityResult(
       requestCode, resultCode, intent); 
     //calling method productScaned() in fragment A here 
     if (scanResult != null) { 
      ((A)mTabsAdapter.getItem(0)).productScaned(intent.getStringExtra("SCAN_RESULT")); 
     } 
    } 

} 

片段A:

public class A extends SherlockFragment { 

    private SearchView svMProducts; 

    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
      Bundle savedInstanceState) { 
     View view = inflater.inflate(R.layout.fragment_a, container, 
       false); 
     svMProducts = (SearchView) view.findViewById(R.id.svMProductsInvoice); 
     svMProducts.setOnQueryTextListener(queryListener); 
     svMProducts.requestFocus(); 

     btnScanBarcode = (ImageView)view.findViewById(R.id.btnScanBarcode); 
     btnScanBarcode.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View v) { 
           //tried with IntentIntegrator also, didn't work 
       IntentIntegratorSupportV4 integrator = new IntentIntegratorSupportV4(A.this);       
       integrator.initiateScan(); 
      } 
     }); 
     return view; 
    } 

    public void productScaned(String upc){ 
       //this method gets called but svMProducts view is null! 
     svMProducts.setQuery(upc, true); 
    } 
} 

回答

0

找到它。

startActivityForResult(intent, code);應該從片段調用以獲得Fragment中的結果。上面的代碼通過IntentIntegratorSupportV4來做到這一點,但我也有onActivityResult()Activity這是防止在Fragment的onActivityResult()