首页 > 代码库 > Android的Fragement基础

Android的Fragement基础

在Android3.0的时候,为了适配平板等大屏幕,谷歌引入了碎片(Fragement)的概念。

Fragement可以有自己的布局,同时又可以相当于控件一样显示在Activity里。可以动态添加,也可以静态添加。

Fragement的生命周期和Activity很相似,在Activity的基础上多了五个方法:

onAttach -> onCreate -> oncreateView -> onActivityCreated -> onStart -> onResume -> onPause -> 0nStop -> onDestoryView -> onDestory -> onDetach

onAttach:Fragment和Activity建立关联的时候调用.

onCreateView:为Fragment加载布局时调用。

onActivityCreated:当Activity中的onCreate方法执行完后调用。

onDestoryView:Fragment中的布局被移除时调用。

onDetach:Fragment和Activity解除关联的时候调用。

碎片基础:

http://blog.csdn.net/guolin_blog/article/details/8881711

利用碎片同时兼容手机和平板:

http://blog.csdn.net/guolin_blog/article/details/8744943

随着碎片的广泛应用,Fragement不再只限于解决平板适配的问题了。

Fragement + ViewPager + RadioGroup实现仿微信底部滑动菜单栏:

1> 定义主界面

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:android="http://schemas.android.com/apk/res/android">
    <android.support.v4.view.ViewPager
        android:id="@+id/main_ViewPager"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1.0"
        />
    <RadioGroup
        android:id="@+id/main_tab_RadioGroup"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:orientation="horizontal"
        >
        <RadioButton
            android:id="@+id/radio_message"
            android:button="@null"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:drawableTop="@drawable/icon_message"
            android:text="Message"
            android:checked="true"
            />
        <RadioButton
            android:id="@+id/radio_contacts"
            android:button="@null"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:drawableTop="@drawable/icon_contacts"
            android:text="Contacts"
            />
        <RadioButton
            android:id="@+id/radio_setting"
            android:button="@null"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:drawableTop="@drawable/icon_setting"
            android:text="Settings"
            />
    </RadioGroup>
</LinearLayout>

2> 图标的点击变色通过xml控制,放于drawable下:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_checked="true" android:drawable="@drawable/message_selected" />
    <item android:state_checked="false" android:drawable="@drawable/message_unselected" />
</selector>

3> MainActivity类

public class MainActivity extends AppCompatActivity {
    public ViewPager mViewPager;
    private RadioGroup mRadioGroup;
    private RadioButton radioButton_message;
    private RadioButton radioButton_contacts;
    private RadioButton radioButton_settings;
    private ArrayList<Fragment> fragmentArrayList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //
        initButton();
        initPager();
    }

    public void initButton(){
        mRadioGroup = (RadioGroup) findViewById(R.id.main_tab_RadioGroup);
        radioButton_message = (RadioButton) findViewById(R.id.radio_message);
        radioButton_contacts = (RadioButton) findViewById(R.id.radio_contacts);
        radioButton_settings = (RadioButton) findViewById(R.id.radio_setting);
        //监听button的点击,显示对应的fragement
        mRadioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                int current = 0;
                switch (checkedId){
                    case R.id.radio_message:
                        current = 0;
                        break;
                    case R.id.radio_contacts:
                        current = 1;
                        break;
                    case R.id.radio_setting:
                        current = 2;
                        break;
                }
                if (mViewPager.getCurrentItem() != current){
                    mViewPager.setCurrentItem(current);
                }
            }
        });
    }
    public void initPager(){
        mViewPager = (ViewPager) findViewById(R.id.main_ViewPager);
        fragmentArrayList = new ArrayList<Fragment>();

        MessageFragement messageFragement = new MessageFragement();
        ContactsFragement contactsFragement = new ContactsFragement();
        SettingsFragement settingsFragement = new SettingsFragement();

        fragmentArrayList.add(messageFragement);
        fragmentArrayList.add(contactsFragement);
        fragmentArrayList.add(settingsFragement);
        
        mViewPager.setAdapter(new MyAdapter(getSupportFragmentManager(),fragmentArrayList));
        mViewPager.setCurrentItem(0);
        //监听fragement滑动,显示对应button
        mViewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                switch (position){
                    case 0:
                        mRadioGroup.check(R.id.radio_message);
                        break;
                    case 1:
                        mRadioGroup.check(R.id.radio_contacts);
                        break;
                    case 2:
                        mRadioGroup.check(R.id.radio_setting);
                        break;
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    public class MyAdapter extends FragmentPagerAdapter
    {
        ArrayList<Fragment> list ;
        public MyAdapter(FragmentManager fm , ArrayList<Fragment> list)
        {
            super(fm);
            this.list = list ;
        }
        @Override
        public android.support.v4.app.Fragment getItem(int arg0) {
            return list.get(arg0);
        }
        @Override
        public int getCount() {
            return list.size();
        }
    }
}

 

Android的Fragement基础