ViewPager+Fragment实现左右切换,还有点击事件

Posted 疯子宽哥

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ViewPager+Fragment实现左右切换,还有点击事件相关的知识,希望对你有一定的参考价值。

 

activity_main.xml

1 <?xml version="1.0" encoding="utf-8"?>
2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3     android:orientation="vertical" android:layout_width="match_parent"
4     android:layout_height="match_parent">
5     <android.support.v4.view.ViewPager
6         android:id="@+id/viewPager"
7         android:layout_width="match_parent"
8         android:layout_height="match_parent"/>
9 </LinearLayout>

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     @Override
22     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<Fragment> getFragment() {
31         List<Fragment> 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<Fragment> fragments;
39         public MyFragment(FragmentManager fm, List<Fragment> fragments) {
40             super(fm);
41             this.fragments = fragments;
42         }
43 
44         @Override
45         public Fragment getItem(int position) {
46             return fragments.get(position);
47         }
48 
49         @Override
50         public int getCount() {
51             return fragments==null?0:fragments.size();
52         }
53     }
54 }

activity_manager.xml

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     xmlns:tools="http://schemas.android.com/tools"
 4     android:id="@+id/activity_main"
 5     android:layout_width="match_parent"
 6     android:layout_height="match_parent"
 7     tools:context="com.example.lession30_fragment.Activity_Manager">
 8 
 9     <Button
10         android:id="@+id/btn1"
11         android:layout_width="wrap_content"
12         android:layout_height="wrap_content"
13         android:text="第一个界面" />
14 
15     <Button
16         android:id="@+id/btn2"
17         android:layout_width="wrap_content"
18         android:layout_height="wrap_content"
19         android:layout_alignParentRight="true"
20         android:text="第二个界面" />
21 
22     <FrameLayout
23         android:id="@+id/fl"
24         android:layout_width="match_parent"
25         android:layout_height="match_parent"
26         android:layout_below="@id/btn1"/>
27 </RelativeLayout>

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     @Nullable
22     @Override
23     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         @Override
46         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 <?xml version="1.0" encoding="utf-8"?>
 2 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     android:orientation="vertical" android:layout_width="match_parent"
 4     android:layout_height="match_parent">
 5     <TextView
 6         android:layout_width="wrap_content"
 7         android:layout_height="wrap_content"
 8         android:text="第N个界面"
 9         android:layout_centerHorizontal="true"
10         android:layout_centerVertical="true"
11         android:textSize="30sp"/>
12 </RelativeLayout>

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     @Nullable
17     @Override
18     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 <?xml version="1.0" encoding="utf-8"?>
 2 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     android:orientation="vertical" android:layout_width="match_parent"
 4     android:layout_height="match_parent">
 5     <TextView
 6         android:layout_width="wrap_content"
 7         android:layout_height="wrap_content"
 8         android:text="我是第一个界面"
 9         android:textSize="30sp"
10         android:layout_centerHorizontal="true"
11         android:layout_centerVertical="true"/>
12 </RelativeLayout>

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     @Nullable
17     @Override
18     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 <?xml version="1.0" encoding="utf-8"?>
 2 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     android:orientation="vertical" android:layout_width="match_parent"
 4     android:layout_height="match_parent">
 5     <TextView
 6         android:layout_width="wrap_content"
 7         android:layout_height="wrap_content"
 8         android:text="我是第二个界面"
 9         android:textSize="30sp"
10         android:layout_centerHorizontal="true"
11         android:layout_centerVertical="true"/>
12 </RelativeLayout>

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     @Nullable
17     @Override
18     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 }

 

以上是关于ViewPager+Fragment实现左右切换,还有点击事件的主要内容,如果未能解决你的问题,请参考以下文章

ViewPager+Fragment 懒加载

Android之实现ViewPager+Fragment左右滑动

关于tablayout+viewpager+fragment配合使用的一点记录

android ViewPager+fragment切换动画实现

android ViewPager+fragment切换动画实现

Android- fragment结合ViewPager实现左右滑动