Fragment的嵌套,实现Android常见主界面

1.Fragment的产生和介绍
Android运行在各种各样的设备中,有小屏幕的手机,超大屏的平板甚至电视。针对屏幕尺寸的差距,很多情况下,都是先针对手机开发一套App,然后拷贝一份,修改布局以适应平板神马超级大屏的。难道无法做到一个App可以同时适应手机和平板么,当然了,必须有啊。Fragment的出现就是为了解决这样的问题。你可以把Fragment当成Activity的一个界面的一个组成部分,甚至Activity的界面可以完全有不同的Fragment组成,更帅气的是Fragment拥有自己的生命周期和接收、处理用户的事件,这样就不必在Activity写一堆控件的事件处理的代码了。更为重要的是,你可以动态的添加、替换和移除某个Fragment。
每个fragment拥有自己的生命周期,但是fragment要依赖于activity存在,生命周期受到包括它的activity的生命周期控制。
如果你对Fragment还不熟悉,请参考该博客http://blog.csdn.net/lmj623565791/article/details/37970961,话不多说,先上一张运行效果图Fragment的嵌套,实现Android常见主界面_第1张图片
该demo的功能是实现下方放置几个Fragment可以点击,但不可滑动,上方写了四个Fragment嵌套在下方的第一个Fragment里面,可以点击切换,也可以滑动切换。下面直接上代码
项目的入口Activity

public class Tab2 extends FragmentActivity implements OnClickListener{
    private LinearLayout weixinTab;
    private LinearLayout friendTab;
    private LinearLayout findTab;
    private LinearLayout setTab;

    private ImageButton wxImg;
    private ImageButton friImg;
    private ImageButton findImg;
    private ImageButton setImg;

    private Fragment f1;
    private Fragment f2;
    private Fragment f3;
    private Fragment f4;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.second_activity);
        initView();
        initEvent();
        setSelect(0);
    }
    //初始化布局
    private void initView() {
        weixinTab = (LinearLayout) findViewById(R.id.tabWeixin);
        friendTab = (LinearLayout) findViewById(R.id.tab_friend);
        findTab = (LinearLayout) findViewById(R.id.tabFing);
        setTab = (LinearLayout) findViewById(R.id.tabSetting);

        wxImg = (ImageButton) findViewById(R.id.wxImg);
        friImg = (ImageButton) findViewById(R.id.friImg);
        findImg = (ImageButton) findViewById(R.id.findImg);
        setImg = (ImageButton) findViewById(R.id.setImg);

    }
    //初始化监听事件
    private void initEvent() {
        weixinTab.setOnClickListener(this);
        friendTab.setOnClickListener(this);
        findTab.setOnClickListener(this);
        setTab.setOnClickListener(this);
        }

    @Override
    public void onClick(View v) {
        resetImg();
        switch (v.getId()) {
        case R.id.tabWeixin:
            setSelect(0);
            break;
        case R.id.tab_friend:
            setSelect(1);
            break;
        case R.id.tabFing:
            setSelect(2);
            break;
        case R.id.tabSetting:
            setSelect(3);
            break;

        default:
            break;
        }
    }
    //设置点击的Fragment的状态,没选择的隐藏,选中的显示
    private void setSelect(int i){
        FragmentManager fm = getSupportFragmentManager();
        FragmentTransaction transaction = fm.beginTransaction();
        hideFragment(transaction);
        switch (i) {
        case 0:
            if(f1 == null){
                f1 = new FragmentTop1();
                transaction.add(R.id.context, f1);
            }else{
                transaction.show(f1);
            }
            wxImg.setImageResource(R.drawable.tab_weixin_pressed);
            break;
        case 1:
            if(f2 == null){
                f2 = new Fragment2();
                transaction.add(R.id.context, f2);
            }else{
                transaction.show(f2);
            }
            friImg.setImageResource(R.drawable.tab_address_pressed);
            break;
        case 2:
            if(f3 == null){
                f3 = new Fragment3();
                transaction.add(R.id.context, f3);
            }else{
                transaction.show(f3);
            }
            findImg.setImageResource(R.drawable.tab_find_frd_pressed);
            break;
        case 3:
            if(f4 == null){
                f4 = new Fragment4();
                transaction.add(R.id.context, f4);
            }else{
                transaction.show(f4);
            }
            setImg.setImageResource(R.drawable.tab_settings_pressed);
            break;

        default:
            break;
        }
        transaction.commit();
    }
    //隐藏Fragment
    private void hideFragment(FragmentTransaction transaction) {
        if(f1 != null){
            transaction.hide(f1);
        }
        if(f2 != null){
            transaction.hide(f2);
        }
        if(f3 != null){
            transaction.hide(f3);
        }
        if(f4 != null){
            transaction.hide(f4);
        }
    }
    //设置要选择的Fragment状态是图片的状态
    private void resetImg() {
        wxImg.setImageResource(R.drawable.tab_weixin_normal);
        friImg.setImageResource(R.drawable.tab_address_normal);
        findImg.setImageResource(R.drawable.tab_find_frd_normal);
        setImg.setImageResource(R.drawable.tab_settings_normal);
    }

底部第一个Fragment的实现,运用了FragmentPagerAdapter和ViewPager的

public class FragmentTop1 extends Fragment implements OnClickListener{

    private TextView tv1;
    private TextView tv2;
    private TextView tv3;
    private TextView tv4;

    private ViewPager viewPager;
    private FragmentPagerAdapter mAdapter;
    private List mViews = new ArrayList();
    private View v;
    private View v1;
    private View v2;
    private View v3;
    private View v4;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
         v = inflater.inflate(R.layout.topall, container, false);
         initLayout();

         mAdapter = new FragmentPagerAdapter(getChildFragmentManager()) {

            @Override
            public int getCount() {
                // TODO Auto-generated method stub
                return mViews.size();
            }

            @Override
            public Fragment getItem(int arg0) {
                // TODO Auto-generated method stub
                return (Fragment) mViews.get(arg0);
            }
        };
        viewPager.setAdapter(mAdapter);
        viewPager.setOnPageChangeListener(new OnPageChangeListener() {
            private int currentIndex;
            @Override
            public void onPageSelected(int arg0) {
                resetColor();
                switch(arg0){
                case 0:
                    tv1.setTextColor(Color.rgb(87, 153, 8));
                    break;
                case 1:
                    tv2.setTextColor(Color.rgb(87, 153, 8));
                    break;
                case 2:
                    tv3.setTextColor(Color.rgb(87, 153, 8));
                    break;
                case 3:
                    tv4.setTextColor(Color.rgb(87, 153, 8));
                    break;
                default:
                    break;
                }
                currentIndex = arg0;
            }

            @Override
            public void onPageScrolled(int arg0, float arg1, int arg2) {
            }

            @Override
            public void onPageScrollStateChanged(int arg0) {
            }
        });
        return v;
    }

    private void initLayout() {

        tv1 = (TextView) v.findViewById(R.id.tv1);
        tv2 = (TextView) v.findViewById(R.id.tv2);
        tv3 = (TextView) v.findViewById(R.id.tv3);
        tv4 = (TextView) v.findViewById(R.id.tv4);
        viewPager = (ViewPager) v.findViewById(R.id.viewPager);

        tv1.setOnClickListener(this);
        tv2.setOnClickListener(this);
        tv3.setOnClickListener(this);
        tv4.setOnClickListener(this);

        Fragment1 f1 = new Fragment1();
        Fragment2 f2 = new Fragment2();
        Fragment3 f3 = new Fragment3();
        Fragment4 f4 = new Fragment4();

        mViews.add(f1);
        mViews.add(f2);
        mViews.add(f3);
        mViews.add(f4);
    }

    @Override
    public void onClick(View v) {
        switch(v.getId()){
        case R.id.tv1:
            resetColor();
            tv1.setTextColor(Color.rgb(87, 153, 8));
            viewPager.setCurrentItem(0);
            break;
        case R.id.tv2:
            resetColor();
            tv2.setTextColor(Color.rgb(87, 153, 8));
            viewPager.setCurrentItem(1);
            break;
        case R.id.tv3:
            resetColor();
            tv3.setTextColor(Color.rgb(87, 153, 8));
            viewPager.setCurrentItem(2);
            break;
        case R.id.tv4:
            resetColor();
            tv4.setTextColor(Color.rgb(87, 153, 8));
            viewPager.setCurrentItem(3);
            break;
        default:
            break;
        }
    }
    public void resetColor(){
        tv1.setTextColor(Color.rgb(56,56,56));
        tv2.setTextColor(Color.rgb(56,56,56));
        tv3.setTextColor(Color.rgb(56,56,56));
        tv4.setTextColor(Color.rgb(56,56,56));
    }
    public class MyPagerAdapter extends PagerAdapter { 
        public List mListViews; 

        public MyPagerAdapter(List mListViews) { 
            this.mListViews = mListViews; 
        } 


        @Override 
        public void finishUpdate(View arg0) { 
        } 

        @Override 
        public int getCount() { 
            return mListViews.size(); 
        } 



        @Override 
        public boolean isViewFromObject(View arg0, Object arg1) { 
            return arg0 == (arg1); 
        } 

        @Override 
        public void restoreState(Parcelable arg0, ClassLoader arg1) { 
        } 

        @Override 
        public Parcelable saveState() { 
            return null; 
        } 

        @Override 
        public void startUpdate(View arg0) { 
        } 
    }

剩下的Fragment都是重复的,故粘贴一个出来

public class Fragment1 extends Fragment{
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.tab_01, container, false);
        return v;
    }
}

Tab01的的布局文件

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    <FrameLayout 
        android:id="@+id/context"
        android:layout_weight="1"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        >FrameLayout>
    <include layout="@layout/bottom"/>
LinearLayout>

好了,Fragment的嵌套完美结束,快为你的项目集成该功能吧,谢谢!!!
如果有什么不懂的随时私聊我或者点这里下载源码https://github.com/1234567leihui/Test.git

你可能感兴趣的:(Fragment的嵌套,实现Android常见主界面)