博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
ViewPager+Fragment实现左右切换,还有点击事件
阅读量:5143 次
发布时间:2019-06-13

本文共 6485 字,大约阅读时间需要 21 分钟。

 

activity_main.xml

1 
2
5
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 List
getFragment() {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 
2
8 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 
2
5
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 
2
5
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 
2
5
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 }

 

转载于:https://www.cnblogs.com/fengzikuange/p/6120004.html

你可能感兴趣的文章
C++有关 const & 内敛 & 友元&静态成员那些事
查看>>
函数积累
查看>>
python搜索引擎(转)
查看>>
关于height,line-height导致的样式混乱的问题
查看>>
《SEO实战密码》读后一点感受
查看>>
bzoj 4815 [Cqoi2017]小Q的表格——反演+分块
查看>>
Swift 入门之简单语法(六)
查看>>
shim和polyfill有什么区别
查看>>
Failed to load the JNI shared library “E:/2000/Java/JDK6/bin/..jre/bin/client/jvm.dll
查看>>
Zabbix3.4服务器的搭建--CentOS7
查看>>
〖Python〗-- IO多路复用
查看>>
栈(括号匹配)
查看>>
夜太美---酒不醉--人自醉
查看>>
Java学习 · 初识 面向对象深入一
查看>>
源代码如何管理
查看>>
vue怎么将一个组件引入另一个组件?
查看>>
多线程学习笔记三之ReentrantLock与AQS实现分析
查看>>
【转】进程与线程的一个简单解释
查看>>
getopt,getoptlong学习
查看>>
数据的传递 变量与参数的使用
查看>>