0

I am trying to change/slide fragment on click of a button in ViewPager. My code works in one project but, not in current.

xml of activity to load fragment:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/content_swipe"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="firebasetest.imranrana.android.firebasetest.SwipeActivity"
    tools:showIn="@layout/activity_swipe"
    android:orientation="vertical"
    >

    <android.support.v4.view.ViewPager
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/viewPagerId"
        >
    </android.support.v4.view.ViewPager>

</LinearLayout>

activity:

public class  SwipeActivity extends AppCompatActivity{

    ViewPager viewPager;
    Toolbar toolbar;
    TabLayout tabLayout;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_swipe);
        toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        getSupportActionBar().hide();
        viewPager=(ViewPager)findViewById(R.id.viewPagerId);
        setupViewPager(viewPager);

//        tabLayout=(TabLayout)findViewById(R.id.tabLayoutId);
//        tabLayout.setupWithViewPager(viewPager);

    }

    private void setupViewPager(ViewPager viewPager) {
        ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());

        adapter.addFragment(new WebViewFragment()/*, "Main"*/);
        adapter.addFragment(new MenuFragment()/*, "TWO"*/);

        viewPager.setAdapter(adapter);
    }

    class ViewPagerAdapter extends FragmentStatePagerAdapter {
        private final List<Fragment> mFragmentList = new ArrayList<>();
//        private final List<String> mFragmentTitleList = new ArrayList<>();

        public ViewPagerAdapter(FragmentManager manager) {
            super(manager);
        }

        @Override
        public Fragment getItem(int position) {
            return mFragmentList.get(position);
        }

        @Override
        public int getCount() {
            return mFragmentList.size();
        }

        public void addFragment(Fragment fragment/*, String title*/) {
            mFragmentList.add(fragment);
//            mFragmentTitleList.add(title);
        }

        /*@Override
        public CharSequence getPageTitle(int position) {
            return mFragmentTitleList.get(position);
        }*/
    }
}

fragment of viewpager contains changing button:

public class    MenuFragment extends Fragment {

    Button funPageButton;
    Button profilePageButton;
    Fragment fragment;
    FragmentTransaction fragmentTransaction;



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


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

        funPageButton=(Button)view.findViewById(R.id.funPageButtonId);
        profilePageButton=(Button)view.findViewById(R.id.profileButtonId);


        funPageButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                Toast.makeText(getActivity(),"test",Toast.LENGTH_LONG).show();
                fragment=new TestFragment();
                fragmentTransaction=getActivity().getSupportFragmentManager().beginTransaction(); ;
                fragmentTransaction.replace(R.id.content_swipe,fragment).commit();


            }
        });

        profilePageButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                Toast.makeText(getActivity(),"test 1",Toast.LENGTH_LONG).show();

                fragment=new TestFragment1();
                fragmentTransaction=getFragmentManager().beginTransaction();
                fragmentTransaction.add(R.id.content_swipe,fragment).commit();


            }
        });



        return view;
    }

}

What am i doing wrong?

AAA
  • 485
  • 6
  • 23
  • What do you mean by not working.. specify – Sheychan Dec 15 '16 at 06:29
  • not loading new destination fragment – AAA Dec 15 '16 at 06:29
  • can you try to replace getActivity().getSupportFragmentManager() into just getChildFragmentManager() – Sheychan Dec 15 '16 at 06:30
  • error of no view found – AAA Dec 15 '16 at 06:35
  • You are calling `getActivity().getSupportFragmentManager()` and `replace()`, which would replace the *current* fragment. That's probably not what you want. Also replacing Fragments is not how you change pages in a ViewPager – OneCricketeer Dec 15 '16 at 06:36
  • What's R.id.content_swipe? it should be a FrameLayout – Sheychan Dec 15 '16 at 06:37
  • For setting a ViewPager page, http://stackoverflow.com/questions/7424562/how-to-change-viewpagers-page#7446356 – OneCricketeer Dec 15 '16 at 06:38
  • @cricket_007 content layout is activity layout of activity, i am doing same thing in other app and that working fine.but this its not working :( – AAA Dec 15 '16 at 06:42
  • Then you are doing something different and we can't see it without a [mcve] – OneCricketeer Dec 15 '16 at 06:45
  • please tell me which portion of code should i show you. @cricket_007 – AAA Dec 15 '16 at 06:48
  • Whatever works in the other project, I guess. I just think that replacing any Fragment from within a currently shown Fragment is wrong. I don't know about the ViewPager either, because `R.id.content_swipe` is a `LinearLayout`, and you are completely replacing the ViewPager with a `WebViewFragment`. – OneCricketeer Dec 15 '16 at 06:50
  • i am editing my ques please wait – AAA Dec 15 '16 at 06:53
  • @cricket_007 please check my ques with related code – AAA Dec 15 '16 at 06:59
  • My comment still stands. `replace(R.id.content_swipe,fragment)` is going to replace the entire LinearLayout. The ViewPager should be gone when you click the button – OneCricketeer Dec 15 '16 at 07:01
  • then how should correct my code please say – AAA Dec 15 '16 at 07:02

0 Answers0