片段按钮单击不适用于Marshmallow版本的android

时间:2016-02-17 04:54:07

标签: android android-6.0-marshmallow

片段按钮单击在Marshmallow版本的android中不起作用。我有三个片段a1,a2和a3所有片段都有一个按钮,在a1按钮中单击转到a2和a2按钮单击转到a3它在较低版本中工作正常然后23但在23 api a2按钮单击不起作用。

片段a1

public class A1Fragment extends RootFragment {

public A1Fragment() {
    // Required empty public constructor
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View rootView = inflater.inflate(R.layout.fragment_a1, container, false);

    rootView.findViewById(R.id.next_button).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            enterNextFragment();
        }
    });

    return rootView;
}

private void enterNextFragment() {
    A2Fragment a2Fragment = new A2Fragment();
    FragmentTransaction transaction = getChildFragmentManager().beginTransaction();

    // Store the Fragment in stack
    transaction.addToBackStack(null);
    transaction.replace(R.id.fragment_mainLayout, a2Fragment).commit();
}
}

Fragement a2

public class A2Fragment extends RootFragment implements View.OnClickListener{

Button button;

public A2Fragment() {
    // Required empty public constructor
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View rootView = inflater.inflate(R.layout.fragment_a2, container, false);

    button = (Button)rootView.findViewById(R.id.next_button);
    button.setOnClickListener(this);

    return rootView;
}


private void enterNextFragment() {
    A3Fragment a2Fragment = new A3Fragment();
    FragmentTransaction transaction = getChildFragmentManager().beginTransaction();

    // Store the Fragment in stack
    transaction.addToBackStack(null);
    transaction.replace(R.id.fragment_mainLayout, a2Fragment).commit();
}

@Override
public void onClick(View v) {

    switch (v.getId()){

        case R.id.next_button:

            enterNextFragment();
            break;
    }
}
}

片段a3

public class A3Fragment extends RootFragment {

protected ViewPager pager;

public A3Fragment() {
    // Required empty public constructor
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View rootView = inflater.inflate(R.layout.fragment_a3, container, false);
    Button button = (Button) rootView.findViewById(R.id.button);
    pager = (ViewPager) getActivity().findViewById(R.id.vp_pages);

   /* getFragmentManager().popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);
    getFragmentManager().beginTransaction().remove(new A3Fragment());*/

    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            pager.setCurrentItem(2);
            enterNextFragment();
        }
    });
    return rootView;

}


private void enterNextFragment() {

    C1Fragment a2Fragment = new C1Fragment();
    FragmentTransaction transaction = getChildFragmentManager().beginTransaction();

    // Store the Fragment in stack
    /*transaction.addToBackStack(null);*/
    transaction.replace(R.id.fragment_mainLayout, a2Fragment).commit();
}

}

0 个答案:

没有答案