1

I have two fragments and a MainActivity class. Everything works fine, but I want to swipe fragments with onClick button. For example if I click the button on fragment A, it will swipe to fragment B. Where do I need to add the code? In MainActivity or in Fragments?

A:

package com.example.mytesttabandsweeper;



import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;



public class Fragment_A extends Fragment implements OnClickListener {
    TextView wynik;
    Button przycisk;
    EditText procent,cena;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        View v =  inflater.inflate(R.layout.fragment_a,  container, false);

        return v;
    }

}

B:

package com.example.mytesttabandsweeper;

import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;



public class Fragment_B extends Fragment implements OnClickListener {


    TextView slowo;
    EditText podane_slowo;
    private ImageView iv;
    private Bitmap bitmap;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        // TODO Auto-generated method stub\

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

        return v;
    }

}

MainActivity:

package com.example.mytesttabandsweeper;



import android.os.Bundle;
import android.app.Activity;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends FragmentActivity {

    ViewPager viewPager=null;
    Button test;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        viewPager = (ViewPager) findViewById(R.id.pager);
        FragmentManager fragmentManager=getSupportFragmentManager();
        viewPager.setAdapter(new MyAdapter(fragmentManager));



    }




}

class MyAdapter extends FragmentStatePagerAdapter{

    public MyAdapter(FragmentManager fm) {
        super(fm);
        // TODO Auto-generated constructor stub
    }


    @Override
    public Fragment getItem(int i) {
        Fragment fragment = null;
        if(i==0)
        {
            fragment = new Fragment_A();
        }

        if(i==1)
        {
            fragment = new Fragment_B();
        }


        return fragment;
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return 2;
    }

    @Override
    public CharSequence getPageTitle(int position) {
        // TODO Auto-generated method stub
        if(position==0)
        {
            return "A";
        }
        if(position==1)
        {
            return "B";
        }

        return null;
    }
}

Everything works fine, but I want to swipe fragments with a onClick button. For example if I click the button on fragment A, it will swipe to fragment B. Where do I need to add the code? In MainActivity or in Framgments?

Gavin S. Yancey
  • 1,216
  • 1
  • 13
  • 34
mzfx
  • 73
  • 1
  • 4
  • 11

1 Answers1

7

On Button click you can call

viewPager.setCurrentItem(position);

or if you want a smooth scroll you can use

viewPager.setCurrentItem(position,true);
Apoorv
  • 13,470
  • 4
  • 27
  • 33
  • Should I use it in MainActivity? – mzfx Jun 13 '14 at 09:01
  • Yes and if the `Button` is in `Fragment` you will need to pass the click event to the `Activity` – Apoorv Jun 13 '14 at 11:01
  • Just one more question. Can I swipe my fragments only with on button click? I don't want to swipe them by finger. – mzfx Jun 22 '14 at 18:13
  • Do you have your `Fragments` in a `ViewPager`? – Apoorv Jun 23 '14 at 05:03
  • You will need to make your own `ViewPager` and disable scrolling. Look at this answer [Disable swiping in ViewPager](http://stackoverflow.com/questions/9650265/how-do-disable-paging-by-swiping-with-finger-in-viewpager-but-still-be-able-to-s) – Apoorv Jun 23 '14 at 07:27
  • @Apoorv Can u please tell me how can we swipe back(or go previous) using button click. – Rajat kumar Nov 26 '14 at 06:44
  • 1
    The same way as given in the answer just replace `position` with `currentPosition - 1` where `currentPosition` is the current position of the view pager. – Apoorv Nov 26 '14 at 09:01