onClickListener (buttons) to switch view of ViewPager using Fragments

…衆ロ難τιáo~ 提交于 2019-12-11 04:24:17

问题


I'm developing an application, which consists of a FragmentActivity with three Fragments. I want to go from the first fragment to the third and last one with "next" buttons. For that, I'm using a viewPager and pagerAdpater (I disabled the paging/swiping on the viewpager). My "next" buttons have been implemented in their own fragments. I have one xml layout for each fragment and one for the fragmentActivity containing the viewpager.

I am trying to use an OnClickListener where I could use the setCurrentItem(index) method and go from the first to the last view.

The problem is that i don't know where I could put the OnClickListener, since I don't have the viewpager reference in my fragment.

How can I get the viewpager reference in my fragments? Do I have to put my OnClickListener method somewhere else?

Here is the code of one of my 3 Fragments:

public class InitializationStep1 extends Fragment{
private PagerAdapter mPagerAdapter;

CustomViewPager pager;

public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

    View v = (LinearLayout)inflater.inflate(R.layout.initialization_step1_layout, container, false);

    Button button = (Button) v.findViewById(R.id.goToStep2);
    button.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            CustomViewPager pager = (CustomViewPager) v.findViewById(R.id.viewpager);
            if(pager == null)
                System.out.println("It's null..."); // always getting a null value
            else
                pager.setCurrentItem(0);
        }
    });

    return v;
}
@Override
public void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    setUserVisibleHint(true);
   }
}

and this is my FragmentActivity:

public class Initialization extends FragmentActivity{
private PagerAdapter mPagerAdapter;

CustomViewPager pager;
Button b1;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    super.setContentView(R.layout.viewpager_initialization_layout);

    //initialize the pager

    List<Fragment> fragments = new Vector<Fragment>();
    fragments.add(Fragment.instantiate(this, InitializationStep1.class.getName()));
    fragments.add(Fragment.instantiate(this, InitializationStep2.class.getName()));
    fragments.add(Fragment.instantiate(this, InitializationStep3.class.getName()));
    this.mPagerAdapter = new PagerAdapter(super.getSupportFragmentManager(), fragments);

    pager = (CustomViewPager)super.findViewById(R.id.viewpager);
    pager.setPagingEnabled(false);
    pager.setAdapter(this.mPagerAdapter);
    pager.setCurrentItem(0);
    }
}

To sum all this up, I have:
- implemented 3 Fragments (1, 2 and 3)
and I would like to get:
- Each Fragment containing a button to go to the next Fragment (1 -> 2 -> 3).
- The last fragment containing a "finish" button (because there is no next Fragment to navigate to)

I assume that this is a very simple action, but I'm relatively new in Android development and couldn't figure this out. I'm maybe trying to use the wrong component to do that.

Thanks


回答1:


I just had the same problem, try this! In your Initialization FragmentActivity:

public CustomViewPager getpager(){
        return pager;
    }

In "InitializationStep1":

CustomViewPager pager;
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

    View v = (LinearLayout)inflater.inflate(R.layout.initialization_step1_layout, container, false);
    pager = ((Initialization)getActivity()).getpager();

    Button next = (Button) v.findViewById(R.id.button1);
    next.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            pager.setCurrentItem(2);
        }
    });

In this way, you can get the viewpager object that you declare in your FragmentActivity, so you can use it in each fragment to switch between fragments as you want!



来源:https://stackoverflow.com/questions/10691628/onclicklistener-buttons-to-switch-view-of-viewpager-using-fragments

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!