2014-05-13 165 views
1

嗨,大家好我有一個帶有按鈕的片段。我想在點擊按鈕時打開fragment2。 什麼是最佳解決方案? 你能舉個例子嗎? 在XML片段1:切換片段1到片段2

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

在片段1:

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) 
{ 

} 

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

預先感謝您!

回答

1

首先,你必須讓你的第一個片段從按鈕視圖,使用此代碼:

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

(信息是你的片段佈局視圖)

,那麼你需要設置onclicklistener上您MainActivity視圖

按鈕

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

變化R.id.container與容器

所以您的片段1的代碼將是:

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

-----編輯------

對不起,我完全忘了要與按鈕做到這一點,我的壞

這是片段1

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

嘗試過的代碼,它的工作原理:)

+0

嗨,我已經添加MainActivity在第一篇文章,但我得到一個容器錯誤 - \t ft.replace(R.id.container,newFragment); 在您的MainActivity視圖中將容器更改爲R.id.container 。什麼是!? – roxdragon

+0

@roxdragon如果你使用了一個片段,你將會有一個默認運行的Activity(調用你的fragment1的Activity),它將始終在xml中有一個容器,它將被你想要更改的片段替換。或者你可以在你的問題上添加你的活動代碼和xml,也許我可以進一步幫助你 – TqT

+0

是的,我已經在1st post activity_main.xml中添加了。請幫助... – roxdragon