将Google API客户端对象从活动传递到滑动标签片段

时间:2016-02-26 20:52:15

标签: android android-fragments android-tablayout

我正在尝试将对象从活动传递到滑动选项卡片段。我在我的片段中使用了一个带有Bundle,Serializable和args的newInstance方法但我的问题是我应该如何以及在何处调用此方法。提前谢谢。

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

2 个答案:

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

答案 1 :(得分:0)

正确的位置将在您的适配器的getItem()方法 -

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

要获取适配器中的数据,请在初始化Activity中的适配器时将其传递给构造函数,或者在适配器中创建公共方法,并通过从活动中调用该方法来传递该方法。