Android中Fragment点击切换与添加ViewPager滑动切换
2017-06-13 20:24
621 查看
我想大家对这些都不陌生了吧,我还是大概说一下吧,哈哈哈哈。。。。。。
首先介绍一下Fragment是碎片,
为了让界面可以在平板上更好地展示,Android在3.0版本引入了Fragment(碎片)功能,它非常类似于Activity,可以像Activity一样包含布局。Fragment通常是嵌套在Activity中使用的,现在想象这种场景:有两个Fragment,Fragment 1包含了一个ListView,每行显示一本书的标题。Fragment 2包含了TextView和ImageView,来显示书的详细内容和图片。
而使用Fragment 我们可以把屏幕划分成几块,然后进行分组,进行一个模块化的管理!从而可以更加方便的在 运行过程中动态地更新Activity的用户界面!
另外Fragment并不能单独使用,他需要嵌套在Activity 中使用,尽管他拥有自己的生命周期,但是还是会受到宿主Activity的生命周期的影响,比如Activity 被destory销毁了,他也会跟着销毁!
别的不再多说,直接贴代码。。。。。。
别的不再多说,效果也就这样
好了,一切都结束了。。。。。。
首先介绍一下Fragment是碎片,
为了让界面可以在平板上更好地展示,Android在3.0版本引入了Fragment(碎片)功能,它非常类似于Activity,可以像Activity一样包含布局。Fragment通常是嵌套在Activity中使用的,现在想象这种场景:有两个Fragment,Fragment 1包含了一个ListView,每行显示一本书的标题。Fragment 2包含了TextView和ImageView,来显示书的详细内容和图片。
而使用Fragment 我们可以把屏幕划分成几块,然后进行分组,进行一个模块化的管理!从而可以更加方便的在 运行过程中动态地更新Activity的用户界面!
另外Fragment并不能单独使用,他需要嵌套在Activity 中使用,尽管他拥有自己的生命周期,但是还是会受到宿主Activity的生命周期的影响,比如Activity 被destory销毁了,他也会跟着销毁!
别的不再多说,直接贴代码。。。。。。
这里写代码片 xml布局代码 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity" > <android.support.v4.view.ViewPager android:id="@+id/pager" android:layout_width="match_parent" android:layout_height="match_parent"/> <RelativeLayout android:id="@+id/mcontainer" android:layout_width="match_parent" android:layout_height="match_parent" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:orientation="horizontal" > <Button android:id="@+id/mBtn1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="A" /> <Button android:id="@+id/mBtn2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="B" /> </LinearLayout> </RelativeLayout>
这里写代码片 fragmenta代码 <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <TextView android:id="@+id/mTv1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:text="碎片一" android:textSize="20sp" /> </RelativeLayout>
这里写代码片 fragmentb代码 <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <TextView android:id="@+id/mTv2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:textSize="20sp" android:text="碎片二" /> </RelativeLayout>
这里写代码片 Fragment_a代码 package com.example.cn.bgs.fragmentviewpagerdemoa; import android.R.color; import android.os.Bundle; import android.support.v4.app.Fragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.TextView; public class Fragment_a extends Fragment{ @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View v=View.inflate(getActivity(),R.layout.fragmenta, null); return v; } }
这里写代码片 Fragment_b代码 package com.example.cn.bgs.fragmentviewpagerdemoa; import android.R.color; import android.os.Bundle; import android.support.v4.app.Fragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.TextView; public class Fragment_b extends Fragment{ @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // TODO Auto-generated method stub View v=View.inflate(getActivity(),R.layout.fragmentb, null); return v; } }
这里写代码片 MainActivity代码 package com.example.cn.bgs.fragmentviewpagerdemoa; import java.util.ArrayList; import java.util.List; import android.os.Bundle; import android.app.Activity; import android.content.Context; import android.graphics.Color; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentActivity; import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentPagerAdapter; import android.support.v4.app.FragmentTransaction; import android.support.v4.view.ViewPager; import android.support.v4.view.ViewPager.OnPageChangeListener; import android.view.Menu; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; public class MainActivity extends FragmentActivity implements OnClickListener, OnPageChangeListener { private Button mBtn1, mBtn2; private Fragment_a fa; private Fragment_b fb; private FragmentManager manager; private ViewPager pager; private List<Fragment> list=new ArrayList<Fragment>(); private MyAdapter adapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // manager = getSupportFragmentManager(); // FragmentTransaction transaction = manager.beginTransaction(); // fa = new Fragment_a(); // transaction.add(R.id.mcontainer, fa); // transaction.commit(); initView(); } private void initView() { mBtn1 = (Button) findViewById(R.id.mBtn1); mBtn2 = (Button) findViewById(R.id.mBtn2); list.add(new Fragment_a()); list.add(new Fragment_b()); pager = (ViewPager) findViewById(R.id.pager); mBtn1.setOnClickListener(this); mBtn2.setOnClickListener(this); pager.setOnPageChangeListener(this); adapter = new MyAdapter(getSupportFragmentManager(), list); pager.setAdapter(adapter); } // @Override // public void onClick(View v) { // hideAll(); // FragmentTransaction transaction = manager.beginTransaction(); // switch (v.getId()) { // // case R.id.mBtn1: // if (fa == null) { // fa = new Fragment_a(); // transaction.add(R.id.mcontainer, fa); // } else { // transaction.show(fa); // } // // break; // case R.id.mBtn2: // if (fb == null) { // fb = new Fragment_b(); // transaction.add(R.id.mcontainer, fb); // } else { // transaction.show(fb); // } // break; // } // transaction.commit(); // // } // // public void hideAll() { // FragmentTransaction transaction = manager.beginTransaction(); // if (fa != null) { // transaction.hide(fa); // } // if (fb != null) { // transaction.hide(fb); // } // transaction.commit(); // } @Override public void onClick(View v) { switch (v.getId()) { case R.id.mBtn1: mBtn1.setTextColor(Color.GREEN); pager.setCurrentItem(0, false); break; case R.id.mBtn2: mBtn2.setTextColor(Color.GREEN); pager.setCurrentItem(1, false); break; } } class MyAdapter extends FragmentPagerAdapter { private List<Fragment> list; public MyAdapter(FragmentManager fm, List<Fragment> list) { super(fm); this.list = list; } @Override public Fragment getItem(int arg0) { // TODO Auto-generated method stub return list.get(arg0); } @Override public int getCount() { // TODO Auto-generated method stub return list.size(); } } @Override public void onPageScrollStateChanged(int arg0) { // TODO Auto-generated method stub } @Override public void onPageScrolled(int arg0, float arg1, int arg2) { // TODO Auto-generated method stub } @Override public void onPageSelected(int arg0) { clear(); switch (arg0) { case 0: mBtn1.setTextColor(Color.GREEN); pager.setCurrentItem(0, false); break; case 1: mBtn2.setTextColor(Color.GREEN); pager.setCurrentItem(1, false); break; } } private void clear() { mBtn1.setTextColor(Color.GRAY); mBtn2.setTextColor(Color.GRAY); } } 红色注销的是切换的代码
别的不再多说,效果也就这样
好了,一切都结束了。。。。。。
相关文章推荐
- Android简单的Fragment+Viewpager切换(可滑动和点击切换)
- android app主题UI界面框架搭建 tablayout+Viewpager+fragment 导航栏滑动和点击切换界面
- Android 安卓 fragment+viewpager 仿qq界面 实现点击菜单切换界面+滑动切换viewpager切换界面
- Android ViewPager、Fragment、组合控件结合使用(滑动切换,点击导航切换)
- Android之Fragment+ViewPager实现点击+滑动界面切换学习笔记
- Android之ViewPager+Fragment实现页面点击切换和手势滑动
- Android ViewPager加Fragment实现滑动或者点击页面切换
- Android 通过ViewPager实现点击和滑动切换Fragment标签页
- Android 使用ViewPager和RadioGroup配合Fragment实现标签页,可点击按钮切换、滑动切换。
- Android之ViewPager+Fragment实现页面点击切换和手势滑动
- 基于android的网络音乐播放器-添加viewpager和fragment实现滑动切换多个界面(二)
- Android之ViewPager+Fragment实现页面点击切换和手势滑动
- viewpager滑动fragment点击textview切换页面线条滑动效果
- Android基础入门教程——5.2.4 Fragment实例精讲——底部导航栏+ViewPager滑动切换页面
- Android 使用TabLayout、ViewPager和Fragment实现顶部菜单可滑动切换
- 滑动或点击按钮切换页面 ViewPager+RadioGroup+Fragment
- Android 利用ViewPager实现底部圆点导航左右滑动效果以及Fragment页面切换
- Android例子—Fragment实例讲解—底部导航栏+ViewPager滑动切换页面
- Android ViewPager+Fragment滑动选项卡,tab点击选项卡
- Android TabLayout、ViewPager实现顶部和底部Tab导航 点击滑动切换Tab页面