将fragment1切换为fragment2

时间:2014-05-13 01:04:11

标签: android android-fragments

大家好我有一个带按钮的片段。我想在点击按钮时打开fragment2。 什么是最好的解决方案? 你能举个例子吗? 在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" />
在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) 
{

}
片段2中的

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" />

提前谢谢!

1 个答案:

答案 0 :(得分:1)

首先,您必须使用以下代码从第一个片段上的按钮获取视图:

btnChFrag = info.findViewById(R.id.button1);

(信息是片段布局的视图)

然后你需要在那个按钮上设置onclicklistener

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();
}

在MainActivity视图中使用容器更改R.id.container

所以你在fragment1上的代码将是:

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;
}

----- ------编辑

抱歉,我完全忘了你想用按钮,我的坏

这是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;
}

尝试过,它有效:)