activity_main.xml
1 25 9
MainActivity.java
1 package com.example.lession30_fragment; 2 3 import android.os.Bundle; 4 import android.support.annotation.Nullable; 5 import android.support.v4.app.Fragment; 6 import android.support.v4.app.FragmentManager; 7 import android.support.v4.app.FragmentPagerAdapter; 8 import android.support.v4.view.ViewPager; 9 import android.support.v7.app.AppCompatActivity;10 11 import java.util.ArrayList;12 import java.util.List;13 14 /**15 * Created by 世宽 on 2016/11/30.16 */17 18 public class MainActivity extends AppCompatActivity {19 private ViewPager viewPager;20 private MyFragment adapter;21 @Override22 protected void onCreate(@Nullable Bundle savedInstanceState) {23 super.onCreate(savedInstanceState);24 setContentView(R.layout.activity_main);25 viewPager = (ViewPager) findViewById(R.id.viewPager);26 adapter = new MyFragment(getSupportFragmentManager(), getFragment());27 viewPager.setAdapter(adapter);28 }29 30 public ListgetFragment() {31 List fragments=new ArrayList<>();32 fragments.add(new Activity_Manager());33 fragments.add(new Activity_n());34 return fragments;35 }36 37 class MyFragment extends FragmentPagerAdapter{38 private List fragments;39 public MyFragment(FragmentManager fm, List fragments) {40 super(fm);41 this.fragments = fragments;42 }43 44 @Override45 public Fragment getItem(int position) {46 return fragments.get(position);47 }48 49 @Override50 public int getCount() {51 return fragments==null?0:fragments.size();52 }53 }54 }
activity_manager.xml
1 28 9 14 15 21 22 27
Activity_manager.xml
1 package com.example.lession30_fragment; 2 3 import android.support.annotation.Nullable; 4 import android.support.v4.app.Fragment; 5 import android.support.v4.app.FragmentManager; 6 import android.support.v4.app.FragmentTransaction; 7 import android.os.Bundle; 8 import android.view.LayoutInflater; 9 import android.view.View;10 import android.view.ViewGroup;11 import android.widget.Button;12 13 public class Activity_Manager extends Fragment {14 private Button btn1;15 private Button btn2;16 private FragmentManager fm;17 private FragmentTransaction ft;18 private Activity_1 a1;19 private Activity_2 a2;20 private View v;21 @Nullable22 @Override23 public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {24 if (v == null) {25 v = inflater.inflate(R.layout.activity_manager, null);26 btn1 = (Button) v.findViewById(R.id.btn1);27 btn2 = (Button) v.findViewById(R.id.btn2);28 a1 = new Activity_1();29 a2 = new Activity_2();30 btn1.setOnClickListener(clickListener);31 btn2.setOnClickListener(clickListener);32 //窗体加载的时候先把两个Fragment界面添加进来33 fm = getFragmentManager();34 ft = fm.beginTransaction();35 ft.add(R.id.fl, a1, Activity_1.class.getName());//添加第一个窗体36 ft.add(R.id.fl, a2, Activity_2.class.getName());//添加第二个窗体37 ft.show(a1);//显示第一个界面38 ft.hide(a2);//隐藏第二个界面39 ft.commit();//提交事务40 }41 return v;42 }43 44 private View.OnClickListener clickListener = new View.OnClickListener() {45 @Override46 public void onClick(View v) {47 ft = fm.beginTransaction();48 switch (v.getId()) {49 case R.id.btn1:50 //点击界面一的时候界面一显示界面二隐藏51 ft.show(a1);52 ft.hide(a2);53 break;54 case R.id.btn2:55 //点击界面二的时候界面一隐藏界面二显示56 ft.show(a2);57 ft.hide(a1);58 break;59 }60 ft.commit();61 }62 };63 }
activity_n.xml
1 25 12
Activity_n.java
1 package com.example.lession30_fragment; 2 3 import android.os.Bundle; 4 import android.support.annotation.Nullable; 5 import android.support.v4.app.Fragment; 6 import android.view.LayoutInflater; 7 import android.view.View; 8 import android.view.ViewGroup; 9 10 /**11 * Created by 世宽 on 2016/11/30.12 */13 14 public class Activity_n extends Fragment {15 private View v;16 @Nullable17 @Override18 public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {19 if(v==null){20 v=inflater.inflate(R.layout.activity_n,null);21 }22 return v;23 }24 }
activity_1.xml
1 25 12
Activity_1.java
1 package com.example.lession30_fragment; 2 3 import android.os.Bundle; 4 import android.support.annotation.Nullable; 5 import android.support.v4.app.Fragment; 6 import android.view.LayoutInflater; 7 import android.view.View; 8 import android.view.ViewGroup; 9 10 /**11 * Created by 世宽 on 2016/11/30.12 */13 14 public class Activity_1 extends Fragment {15 private View v;16 @Nullable17 @Override18 public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {19 if(v==null){20 v=inflater.inflate(R.layout.activity_1,null);21 }22 return v;23 }24 }
activity_2.xml
1 25 12
Activity_2.java
1 package com.example.lession30_fragment; 2 3 import android.os.Bundle; 4 import android.support.annotation.Nullable; 5 import android.support.v4.app.Fragment; 6 import android.view.LayoutInflater; 7 import android.view.View; 8 import android.view.ViewGroup; 9 10 /**11 * Created by 世宽 on 2016/11/30.12 */13 14 public class Activity_2 extends Fragment {15 private View v;16 @Nullable17 @Override18 public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {19 if(v==null){20 v=inflater.inflate(R.layout.activity_2,null);21 }22 return v;23 }24 }