Reputation: 95
Hi guys I have a fragment with a button. I want open fragment2 when button is clicked. what is the best solution? Can you make an example for this? in XML fragment1:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/background"
android:orientation="vertical" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="116dp"
android:background="#0000"
android:drawableTop="@drawable/autobus"
android:onClick="OPENFRAGMENT"
android:text="ooo" />
in fragment1:
public class Fragment1 extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View info = inflater.inflate(R.layout.info, container, false);
return info;
}
public void OPENFRAGMENT(View view)
{
}
in fragment2:
public class Fragment2 extends Fragment{
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View frag = inflater.inflate(R.layout.frag2, container, false);
return frag;
}
}
MainActivity:
public class MainActivity extends SherlockFragmentActivity {
ViewPager Tab;
TabPagerAdapter TabAdapter;
ActionBar actionBar;
public boolean StatusConnection = false;
public int thread = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TabAdapter = new TabPagerAdapter(getSupportFragmentManager());
Tab = (ViewPager)findViewById(R.id.pager);
Tab.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
@Override
public void onPageSelected(int position) {
actionBar.setSelectedNavigationItem(
position);
Tab.setCurrentItem(position);
}
});
Tab.setAdapter(TabAdapter);
actionBar = getSupportActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
ActionBar.TabListener tabListener = new ActionBar.TabListener(){
@Override
public void onTabReselected(ActionBar.Tab arg0,FragmentTransaction arg1) {
// TODO Auto-generated method stub
}
@Override
public void onTabSelected(ActionBar.Tab arg0,FragmentTransaction arg1) {
// TODO Auto-generated method stub
Tab.setCurrentItem(arg0.getPosition());
}
@Override
public void onTabUnselected(ActionBar.Tab arg0,FragmentTransaction arg1) {
// TODO Auto-generated method stub
}
};
actionBar.addTab(actionBar.newTab().setText("A").setTabListener(tabListener));
actionBar.addTab(actionBar.newTab().setText("B").setTabListener(tabListener));
actionBar.addTab(actionBar.newTab().setText("C").setTabListener(tabListener));
@Override
public Fragment getItem(int i) {
switch (i) {
case 0:
return new Info();
case 1:
return new B();
case 2:
return new C();
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.view.ViewPager
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent" />
Thank you in advance!
Upvotes: 1
Views: 260
Reputation: 332
first you must get view from the button on your 1st fragment, using this code:
btnChFrag = info.findViewById(R.id.button1);
(info is the view of your fragment layout)
then you need to set onclicklistener on that button
btnChFrag.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Fragment2 newFragment = new Fragment2();
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.replace(R.id.container, newFragment);
ft.commit();
}
change R.id.container with your container on your MainActivity view
so your code on fragment1 will be:
public class Fragment1 extends Fragment {
private Button btnChFrag
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View info = inflater.inflate(R.layout.info, container, false);
btnChFrag = info.findViewById(R.id.button1);
//set button onclicklistener here
return info;
}
-----Edit------
sorry I totally forgot you want to do it with button, my bad
this is the code of fragment1
public class Fragment1 extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View info = inflater.inflate(R.layout.info, container, false);
Button btn1 = (Button)rootView.findViewById(R.id.button1);
btn1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
((MainActivity)Fragment1.this.getActivity()).Tab.setCurrentItem(2);
}
});
return info;
}
tried it, and it works :)
Upvotes: 1