I am working on Android application. In my app I have to use 5 fragment and swipe from fragment 1 to 5. SO I used one FragmentActivity ,FragmentPagerAdapter and 5 fragments. Now I can swipe through all the fragments and can identify the current fragment by using

ViewPager.OnPageChangeListener()

I have to add buttons in all the fragments and button click must open the next fragment like swipe. How could I do this? Here I am posting some of my code snippets.

fragmentactivity.java

public class HomeScreen extends FragmentActivity 
{
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.themed_circles);

    mAdapter = new InsuranceFragmentAdapter(getSupportFragmentManager(),
            HomeScreen.this);
    mPager = (ViewPager) findViewById(R.id.pager);
    mPager.setAdapter(mAdapter);
    mIndicator = (CirclePageIndicator) findViewById(R.id.indicator);
    mIndicator.setViewPager(mPager);
    // mIndicator.setCurrentItem(2);

    mIndicator
            .setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
                private int pos;

                @Override
                public void onPageSelected(int position) {

                }

                @Override
                public void onPageScrolled(int position,
                        float positionOffset, int positionOffsetPixels) {


                }

                @Override
                public void onPageScrollStateChanged(int state) {
                    System.out.println("test scroll state" + state);

                }
            });
}

  }

The following is my FragmentPagerAdapter for swiping fragments.

FragmentPagerAdapter.java


public class InsuranceFragmentAdapter extends FragmentPagerAdapter {
Context contx;


public InsuranceFragmentAdapter(FragmentManager fm, Context ctx) {

    super(fm);
    this.contx = ctx;


}

@Override
public Fragment getItem(int position) {
    Fragment fragment = null;
    switch (position) {
    case 0:
        fragment = Fragment.instantiate(contx, com.lic.fragments.FragmentBasicPersonal.class.getName());
        break;

    case 1:
        fragment = Fragment.instantiate(contx, com.lic.fragments.FragmentationNomineeDetails.class.getName());
        break;

    }

    return fragment;
}

@Override
public int getCount() {

    return 2;
}



public void setCount(int count) {

}

}

and one of my fragment is the following. fragment_one.java

public class FragmentFemale extends Fragment {

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

   }

   @Override
   public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View layout = inflater.inflate(R.layout.fragment_female_husband, container, false);
            next= (Button) layout.findViewById(R.id.save);
            next.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
                   //need to change to next fragment
          }
            }
       return layout;
   }

   @Override
   public void onSaveInstanceState(Bundle outState) {
       super.onSaveInstanceState(outState);
     //  outState.putString(KEY_CONTENT, mContent);
   }
}

Please help to fix my issue.

有帮助吗?

解决方案 2

I would insert that next button into fragment activity instead of inserting the same button inside every fragment. But of course it depends on your design. If you want to have only one next button, you can use sherlock bar and keep your buttons up there on the screen.

If your design requires to have buttons inside of the Fragments, then I would go with using interfaces. You can listen click events of fragments from your fragment activity. And set Your Fragment Pager Adapters Current item according to Fragments position.

But anyway, check this tutorial. I believe it will help.

Vogella Fragments Tutorial

其他提示

Well, it's pretty straightforward really.

Every fragment allows you to get an instance of your parent activity. Once you have that you can assume that the ViewPager is instantiated in there. Just fetch that and increment the position of the ViewPager like so:

next.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {

        // Change to next fragment onClick

        ViewPager vp = (ViewPager) getActivity().findViewById(R.id.pager); // Fetch ViewPager instance
        vp.setCurrentItem(vp.getCurrentItem()+1); // Increment ViewPager's position
    }
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top