2016-02-26 47 views
0

我試圖將一個對象從活動傳遞到滑動選項卡片段。我使用Bundle,Serializable和args的newInstance方法,但我的問題是如何以及應該在哪裏調用此方法。提前致謝。將Google API客戶端對象從活動傳遞到滑動選項卡片段

MainActivity

public class MainActivity extends AppCompatActivity implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener { 

GoogleApiClient apiClient; 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    apiClient = new GoogleApiClient.Builder(this) 
      .addApi(Drive.API) 
      .addScope(Drive.SCOPE_APPFOLDER) 
      .addConnectionCallbacks(this) 
      .addOnConnectionFailedListener(this) 
      .build(); 
    setContentView(R.layout.activity_main); 
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
    setSupportActionBar(toolbar); 

    TabLayout tabLayout = (TabLayout) findViewById(R.id.tab_layout); 
    tabLayout.addTab(tabLayout.newTab().setText("Tab1")); 
    tabLayout.addTab(tabLayout.newTab().setText("Tab2")); 
    tabLayout.addTab(tabLayout.newTab().setText("Tab3")); 
    tabLayout.setTabGravity(TabLayout.GRAVITY_FILL); 

    final ViewPager viewPager = (ViewPager) findViewById(R.id.pager); 
    final PageAdapter adapter = new PageAdapter 
      (getSupportFragmentManager(), tabLayout.getTabCount()); 
    viewPager.setAdapter(adapter); 
    viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout)); 
    tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() { 
     @Override 
     public void onTabSelected(TabLayout.Tab tab) { 
      viewPager.setCurrentItem(tab.getPosition()); 
     } 

     @Override 
     public void onTabUnselected(TabLayout.Tab tab) { 

     } 

     @Override 
     public void onTabReselected(TabLayout.Tab tab) { 

     } 
    }); 


} 

PageAdapter

public class PageAdapter extends FragmentStatePagerAdapter { 
int mNumOfTabs; 


public PageAdapter(FragmentManager fm, int NumOfTabs) { 
    super(fm); 
    this.mNumOfTabs = NumOfTabs; 
} 

@Override 
public Fragment getItem(int position) { 

    switch (position) { 
     case 0: 
      ValidTab tab1 = new ValidTab(); 
      return tab1; 
     case 1: 
      ExpiredTab tab2 = new ExpiredTab(); 
      return tab2; 
     case 2: 
      AllTab tab3 = new AllTab(); 
      return tab3; 
     default: 
      return null; 
    } 
} 


@Override 
public int getCount() { 
    return mNumOfTabs; 
} 

ValidTab

public class ValidTab extends Fragment { 

DatabaseHelper db; 
Button pick_buy_date, pick_exp_date, save, sync; 
TextView buy_date, exp_date; 
EditText et_name,et_shop,et_note; 
int type; 
private static final String DESCRIBABLE_KEY = "gapiclient"; 
private GoogleApiClient googleApiClient; 

public static ValidTab newInstance(GoogleApiClient gApiclient){ 
    ValidTab validTab = new ValidTab(); 
    Bundle bundle = new Bundle(); 
    bundle.putSerializable(DESCRIBABLE_KEY, (Serializable) gApiclient); 
    validTab.setArguments(bundle); 
    return validTab; 
} 

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
    googleApiClient = (GoogleApiClient) getArguments().getSerializable(DESCRIBABLE_KEY); 
    View rootView = inflater.inflate(R.layout.valid_tab, container, false); 
    return rootView; 
} 
+0

現在檢查我的答案 –

回答

1

在這裏你可以使用它像這樣

final PageAdapter adapter = new PageAdapter 
      (getSupportFragmentManager(), tabLayout.getTabCount(),apiClient); 

而且在pageradapter類

GoogleApiClient apiClient; 
public PageAdapter(FragmentManager fm, int NumOfTabs,GoogleApiClient apiClient) { 
    super(fm); 
    this.mNumOfTabs = NumOfTabs; 
    this.apiClient=apiClient; 
} 

switch (position) { 
     case 0: 

      return ValidTab.newIntance(gApiclient); 
     case 1: 

      return ExpiredTab.newIntance(gApiclient); 
     case 2: 

      return AllTab.newIntance(gApiclient); 
     default: 
      return null; 
    } 

最好的辦法就是這個

public class ApplicationClass extends Application 
{ 
    //instantiate object public static 
    public static GoogleApiClient mGoogleApiClient; 
    public Context mContext; 

    @Override 
    public void onCreate() 
    { 
     super.onCreate(); 
     mContext = getApplicationContext(); 

     mGoogleApiClient = new GoogleApiClient.Builder(mContext) 
      .addConnectionCallbacks(mContext) 
      .addOnConnectionFailedListener(mContext) 
      .addApi(Games.API).addScope(Games.SCOPE_GAMES) 
      // add other APIs and scopes here as needed 
      .build(); 
    } 

    @Override 
    public void onTerminate() 
    { 
     super.onTerminate(); 
    } 
} 

您的片段類

private GoogleApiClient googleApiClient; 
    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 

     View rootView = inflater.inflate(R.layout.valid_tab, container, false); 
googleApiClient = ApplicationTest.mGoogleApiClient; 
     return rootView; 
    } 
+0

好的,我認爲你的方式正在工作,但現在我得到了一個無法投射錯誤: undle.putSerializable(DESCRIBABLE_KEY,(Serializable)gApiclient); 有什麼想法? –

+0

你需要做正確的實現可序列化 –

+0

現在檢查答案 –

0

的正確位置應該是在你的適配器的getItem()方法 -

ValidTab tab1 = new ValidTab(); 
//add arguments to fragment here 
return tab1; 

要在您的適配器獲得的數據,無論是通過它通過構造函數,當你初始化活動中的適配器或創建適配器一個公共方法,並通過該方法通過調用傳遞它來自你的活動。

相關問題