0

我需要帮助访问 tablistener 类实现中的一些变量,例如我想访问 int nTabSelected = tab.getPosition(),并在同一活动的公共 void 函数上使用它,这是我的编码,谢谢

private class TabListener<T extends Fragment> implements
ActionBar.TabListener {
    private Fragment mFragment;
    private final Activity mActivity;
    private final String mTag;
    private final Class<T> mClass;

    /**
     * Constructor used each time a new tab is created.
     * 
     * @param activity
     *            The host Activity, used to instantiate the fragment
     * @param tag
     *            The identifier tag for the fragment
     * @param clz
     *            The fragment's Class, used to instantiate the fragment
     */
    public TabListener(Activity activity, String tag, Class<T> clz) {
        mActivity = activity;
        mTag = tag;
        mClass = clz;
    }

    public void onTabSelected(Tab tab, FragmentTransaction ft) {

        int nTabSelected = tab.getPosition();

        // Check if the fragment is already initialized
        if (mFragment == null) {
            // If not, instantiate and add it to the activity
            mFragment = Fragment.instantiate(mActivity, mClass.getName());
            ft.add(android.R.id.content, mFragment, mTag);
        } else {
            // If it exists, simply attach it in order to show it
            ft.attach(mFragment);
        }
    }

    public void onTabUnselected(Tab tab, FragmentTransaction ft) {
        if (mFragment != null) {
            // Detach the fragment, because another one is being attached
            ft.detach(mFragment);
        }
    }

    public void onTabReselected(Tab tab, FragmentTransaction ft) {
        // User selected the already selected tab. Usually do nothing.
    }
}

这是我的功能

 public void GeneraVentas() {

    /* genera tres etiquetas para diferenciar las jugadas*/
    String tipoQN = "Nu";
    String tipoPL = "Pl";  
    String tipoTR = "Tr";

    jgds = playDisplayed;
    valorJugada = numberDisplayed;
            ....
}
4

1 回答 1

0

首先,您需要在 TabListener 中使用一个成员变量来存储该值,然后创建一个 getter 来访问它。

private class TabListener<T extends Fragment> implements ActionBar.TabListener {
    private int mSelectedTab = -1;
    ....
    public void onTabSelected(Tab tab, FragmentTransaction ft) {
        mSelectedTab = tab.getPosition();
        ....
    public int getSelectedTab(){
        return mSelectedTab;
    }
}

然后,您可以创建一个新的侦听器并从中获取选定的选项卡。

TabListener listener = new TabListener();
....
int tab = listener.getSelectedTab();
于 2013-08-05T16:52:00.070 回答