Android中Fragment点击切换与添加ViewPager滑动切换

我想大家对这些都不陌生了吧,我还是大概说一下吧,哈哈哈哈。。。。。。
首先介绍一下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);
    }

}
红色注销的是切换的代码

别的不再多说,效果也就这样
这里写图片描述
好了,一切都结束了。。。。。。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

我是阿亮啊

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值