android--fragment的使用


1通过FragmentAdapter适配器实现

1  主框架布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    android:orientation="vertical">

    <FrameLayout
        android:id="@+id/flMain"
        android:layout_width="match_parent"
        android:layout_height="@dimen/weight_setting"
        android:layout_weight="1"
        android:background="#00ff00">

    </FrameLayout>

    <RadioGroup
        android:id="@+id/rdgMain"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <RadioButton
            android:id="@+id/rbtFirst"
            android:layout_width="@dimen/weight_setting"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:button="@null"
            android:gravity="center"
            android:text="首页" />

        <RadioButton
            android:id="@+id/rbtShop"
            android:layout_width="@dimen/weight_setting"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:button="@null"
            android:gravity="center"
            android:text="商家" />

        <RadioButton
            android:id="@+id/rbtMy"
            android:layout_width="@dimen/weight_setting"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:button="@null"
            android:gravity="center"
            android:text="我的" />

        <RadioButton
            android:id="@+id/rbtMore"
            android:layout_width="@dimen/weight_setting"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:button="@null"
            android:gravity="center"
            android:text="更多" />

    </RadioGroup>


</LinearLayout>
2 四个碎片的布局

2.1

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="首页"/>


</LinearLayout>
2.2

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="商家"/>


</LinearLayout>

2.3

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="我的"/>


</LinearLayout>

2.4

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="更多"/>


</LinearLayout>

3 java文件

3.1主框架

package com.example.joshua.meituan.activity;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.FrameLayout;
import android.widget.RadioGroup;

import com.example.joshua.meituan.R;
import com.example.joshua.meituan.adapter.MyFragmentAdapter;
import com.example.joshua.meituan.fragment.FirstFragment;
import com.example.joshua.meituan.fragment.MoreFragment;
import com.example.joshua.meituan.fragment.MyFragment;
import com.example.joshua.meituan.fragment.ShopFragment;
import com.example.joshua.meituan.utils.LogUtils;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends FragmentActivity implements RadioGroup.OnCheckedChangeListener {


    FrameLayout flMain;
    RadioGroup rdgMain;


    //碎片管理
    FragmentManager fragmentManager;
    MyFragmentAdapter myFragmentAdapter;
    int fragmentIndex = 0;
    List<Fragment> fragmentDatas;
    FirstFragment firstFragment;
    ShopFragment shopFragment;
    MyFragment myFragment;
    MoreFragment moreFragment;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        init();
        initOnClickListener();

    }

    private void initOnClickListener() {
        rdgMain.setOnCheckedChangeListener(this);
    }

    private void init() {
        flMain= (FrameLayout) findViewById(R.id.flMain);
        rdgMain= (RadioGroup) findViewById(R.id.rdgMain);
        fragmentDatas = new ArrayList<>();
        firstFragment = new FirstFragment();
        shopFragment = new ShopFragment();
        myFragment = new MyFragment();
        moreFragment = new MoreFragment();
        fragmentDatas.add(firstFragment);
        fragmentDatas.add(shopFragment);
        fragmentDatas.add(myFragment);
        fragmentDatas.add(moreFragment);


        fragmentManager = getSupportFragmentManager();
        myFragmentAdapter = new MyFragmentAdapter(fragmentManager, fragmentDatas);
    }

    @Override
    public void onCheckedChanged(RadioGroup radioGroup, int i) {

        switch (i) {
            case R.id.rbtFirst:
                fragmentIndex = 0;
                LogUtils.getLogInfo(this, fragmentIndex + "");
                break;

            case R.id.rbtShop:
                fragmentIndex = 1;
                LogUtils.getLogInfo(this, fragmentIndex + "");
                break;

            case R.id.rbtMy:
                fragmentIndex = 2;
                LogUtils.getLogInfo(this, fragmentIndex + "");
                break;

            case R.id.rbtMore:
                fragmentIndex = 3;
                LogUtils.getLogInfo(this, fragmentIndex + "");
                break;
        }
       Fragment fragment = (Fragment) myFragmentAdapter.instantiateItem(flMain,fragmentIndex);
        myFragmentAdapter.setPrimaryItem(flMain,fragmentIndex,fragment);
        myFragmentAdapter.finishUpdate(flMain);


    }
}

3.2碎片1

package com.example.joshua.meituan.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.example.joshua.meituan.R;
import com.example.joshua.meituan.utils.LogUtils;

/**
 * Created by joshua on 2016/6/1.
 */
public class FirstFragment extends Fragment {


    View view;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        view = inflater.inflate(R.layout.fragment_first, container, false);


        return view;
    }


    @Override
    public void onDestroy() {
        super.onDestroy();
        LogUtils.getLogError(getActivity(),"FirstFragment");
    }

    @Override
    public void setMenuVisibility(boolean menuVisible) {
        super.setMenuVisibility(menuVisible);
        if (this.getView() != null)
            this.getView().setVisibility(menuVisible ? View.VISIBLE : View.GONE);
    }


}

3.3碎片2

package com.example.joshua.meituan.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.example.joshua.meituan.R;
import com.example.joshua.meituan.utils.LogUtils;

/**
 * Created by joshua on 2016/6/1.
 */
public class ShopFragment extends Fragment {


    View view;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        view = inflater.inflate(R.layout.fragment_shop, container, false);


        return view;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        LogUtils.getLogError(getActivity(),"ShopFragment");
    }

    @Override
    public void setMenuVisibility(boolean menuVisible) {
        super.setMenuVisibility(menuVisible);
        if (this.getView() != null)
            this.getView().setVisibility(menuVisible ? View.VISIBLE : View.GONE);
    }
}

3.4碎片3

package com.example.joshua.meituan.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.example.joshua.meituan.R;
import com.example.joshua.meituan.utils.LogUtils;

/**
 * Created by joshua on 2016/6/1.
 */
public class MyFragment extends Fragment {


    View view;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        view = inflater.inflate(R.layout.fragment_my, container, false);


        return view;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        LogUtils.getLogError(getActivity(),"MyFragment");
    }

    @Override
    public void setMenuVisibility(boolean menuVisible) {
        super.setMenuVisibility(menuVisible);
        if (this.getView() != null)
            this.getView().setVisibility(menuVisible ? View.VISIBLE : View.GONE);
    }
}

3.5碎片4

package com.example.joshua.meituan.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.example.joshua.meituan.R;
import com.example.joshua.meituan.utils.LogUtils;

/**
 * Created by joshua on 2016/6/1.
 */
public class ShopFragment extends Fragment {


    View view;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        view = inflater.inflate(R.layout.fragment_shop, container, false);


        return view;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        LogUtils.getLogError(getActivity(),"ShopFragment");
    }

    @Override
    public void setMenuVisibility(boolean menuVisible) {
        super.setMenuVisibility(menuVisible);
        if (this.getView() != null)
            this.getView().setVisibility(menuVisible ? View.VISIBLE : View.GONE);
    }
}


4适配器

package com.example.joshua.meituan.adapter;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;

import java.util.List;

/**
 * Created by joshua on 2016/6/2.
 */
public class MyFragmentAdapter extends FragmentPagerAdapter {

    List<Fragment> datas;

    public MyFragmentAdapter(FragmentManager fm, List<Fragment> datas) {
        super(fm);
        this.datas=datas;
    }


    @Override
    public Fragment getItem(int position) {
        return datas.get(position);
    }

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


}








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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值