关闭

android--fragment的使用

标签: androidfragment fragmentPag
72人阅读 评论(0) 收藏 举报
分类:


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

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:26854次
    • 积分:926
    • 等级:
    • 排名:千里之外
    • 原创:67篇
    • 转载:5篇
    • 译文:0篇
    • 评论:3条
    最新评论