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?