更改选项卡上片段中的按钮数量

时间:2014-05-23 11:50:42

标签: android android-layout android-fragments android-studio

我已经以编程方式创建了10个标签,并且想要在用户点击标签时更改放置在片段中的按钮数量。

我的'MainActivity'代码:

public class MainActivity extends Activity {
int numberOfButtons=0;
@Override
  protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        FragmentA fragmentA = new FragmentA();
        FragmentManager manager = getFragmentManager();
        FragmentTransaction transaction = manager.beginTransaction();
        transaction.add(R.id.mainLayout,fragmentA,"fragA");
        transaction.commit();
        ActionBar  actionBar = getActionBar();
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
        for (int i=0;i<10;i++)
        {
            ActionBar.Tab tab = actionBar.newTab().setText("Tab"+i).setTabListener(new ActionBar.TabListener() {
                @Override
                public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) {
                    numberOfButtons = 10;
                    sendNumber(numberOfButtons);

                }

                @Override
                public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction ft) {

                }

                @Override
                public void onTabReselected(ActionBar.Tab tab, FragmentTransaction ft) {

                }
            });

            actionBar.addTab(tab);
     }
}

这是我的片段代码:

public class FragmentA extends Fragment implements View.OnClickListener{

Button btn;
View myView;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    int numberOfButtons= getArguments().getInt("someInt",0);
    LinearLayout view =  new LinearLayout(getActivity());
    // Inflate the layout for this fragment
    view.setOrientation(LinearLayout.VERTICAL);
    view.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
    for (int i = 0;i<numberOfButtons;i++)
    {
        btn = new Button(getActivity());
        view.addView(new Button(getActivity()));
    }
    myView = view;
    return myView;
}


@Override
public void onAttach(Activity activity) {
    super.onAttach(activity);
}

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

}
@Override
public void onClick(View v) {

}

}

我在MainActivity中调用numbeOfButtons方法,并希望在单击选项卡时将数量的按钮发送到Fragment。但是,在调用行fA.numberOfBtns(numberOfButtons);之后,应用程序会抛出错误。错误是reflect.InvocationTargetException。

单击选项卡时,如何更改Fragment中的按钮数量?

1 个答案:

答案 0 :(得分:1)

您可能希望在newInstance初始化期间使用Fragment技术传递参数。以下是一个取自here的小例子:

public static MyFragment newInstance(int someInt) {
    MyFragment myFragment = new MyFragment();

    Bundle args = new Bundle();
    args.putInt("someInt", someInt);
    myFragment.setArguments(args);

    return myFragment;
}

稍后您可以访问此参数:

getArguments().getInt("someInt", 0);