简体   繁体   中英

Switching to a fragment from a fragment onClick()

I'm trying to create an application that has a similar framework to that of Snapchat, where I have three screens that can be swiped through. I have three screens (made with a fragment for each) and wish to slide to a screen on button press. Just to be clear, I have a table on one screen, from which a row is clickable and has a function that it calls onClick(). I wish to slide to the desired fragment using the mentioned function.

Fragment.Java:

public class ChatFragment extends Fragment {

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.chat_fragment_layout, container, false);
        rootView.findViewById(R.id.row0).setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v){
                openfrag("1");
            }
        });
        rootView.findViewById(R.id.row1).setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v){
                openfrag("2");
            }
        });
        rootView.findViewById(R.id.row2).setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v){
                openfrag("3");
            }
        });

        return rootView;
    }
    private void openfrag(String number){
    }

I'm swiping through the fragments by using a PagerAdapter that extends FragmentPagerAdapter:

public class PagerAdapter extends FragmentPagerAdapter {

    public PagerAdapter(FragmentManager fm) {
        super(fm);

    }

    @Override
    public Fragment getItem(int arg0) {
        switch (arg0) {
            case 0:

                return new MessageFragment();
            case 1:
                return new ChatFragment();
            case 2:
                return new CameraFragment();
            default:
                break;
        }
        return null;
    }

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

I have tried the replace method with fragmentmanager, but that is not what I want, I want it to act as if I swiped to that fragment. Thanks for the help.

You can use simply this given code

        String tag = "android:switcher:" + R.id.pager + ":" + 0;
        AddStudentFragment f = (AddStudentFragment) getActivity().getSupportFragmentManager().findFragmentByTag(tag);
        String tag1 = "android:switcher:" + R.id.pager + ":" + 1;
        AssignSubjectFragment a = (AssignSubjectFragment) getActivity().getSupportFragmentManager().findFragmentByTag(tag1);

I managed to get it to work with a small addition to my MainActivity class outside the onCreate method. Whenever you need to switch to another fragment in your pager, simply call this function with the item index and true/false depending if you want it to smooth scroll or not.

public void setCurrentItem (int item, boolean smoothScroll) {
        mViewPager.setCurrentItem(item, smoothScroll);
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM