Fragment

来源:互联网 发布:金山毒霸软件恢复会员 编辑:程序博客网 时间:2024/06/03 21:00
1.Fragment基本使用 3.0  碎片 片段
2.Fragment之间的跳转(返回栈)
3.Fragment的生命周期(文档)
4.带侧边栏的Fragment(新建带侧边栏的模板的Activity)
5.Tabbed Activity(新建左右滑动的模板的Activity)
6.ViewPager+Fragment


layout-sw600dp
1.320,针对以下屏幕配置的设备:
    240x320ldpi(QVGA手持设备)
    320x480mdpi(手持设备)
    480x800hdpi(高分辨率手持设备)
2.480,针对480x800mdpi的屏幕(平板或手持设备)
3.600,针对600x1024mdip的屏幕(7英寸平板)

4.720,针对720x1280mdip的屏幕(10英寸平板)

微信效果图


<?xml version="1.0" encoding="utf-8"?><LinearLayout    xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:app="http://schemas.android.com/apk/res-auto"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical"    tools:context="com.zking.administrator.myapplication5.MainActivity">    <android.support.v4.view.ViewPager        android:layout_width="match_parent"        android:layout_height="0dp"        android:id="@+id/vp_main_viewpager"        android:layout_weight="1"        ></android.support.v4.view.ViewPager>    <RadioGroup        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:orientation="horizontal"        android:id="@+id/rg_main_radiogroup"        android:gravity="center">        <RadioButton            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:button="@null"            android:text="微信"            android:drawableTop="@drawable/weixin_selctor"            android:id="@+id/rb_main_weixin"/>        <RadioButton            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:button="@null"            android:text="通讯录"            android:drawableTop="@drawable/contacts_selctor"            android:id="@+id/rb_main_contacts"/>        <RadioButton            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:button="@null"            android:text="发现"            android:drawableTop="@drawable/find_selctor"            android:id="@+id/rb_main_find"/>        <RadioButton            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:button="@null"            android:text="我"            android:drawableTop="@drawable/me_selctor"            android:id="@+id/rb_main_me"/>    </RadioGroup></LinearLayout>

写四个fragment碎片的xml<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:orientation="vertical" android:layout_width="match_parent"    android:layout_height="match_parent">    <TextView        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="微信"        android:gravity="center"        android:textSize="30sp"        android:background="#66ff0000"/></LinearLayout>

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:orientation="vertical" android:layout_width="match_parent"    android:layout_height="match_parent">    <TextView        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="通讯录"        android:gravity="center"        android:textSize="30sp"        android:background="#66ff0000"/></LinearLayout>

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:orientation="vertical" android:layout_width="match_parent"    android:layout_height="match_parent">    <TextView        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="发现"        android:gravity="center"        android:textSize="30sp"        android:background="#66ff0000"/></LinearLayout>

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:orientation="vertical" android:layout_width="match_parent"    android:layout_height="match_parent">    <TextView        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="我"        android:gravity="center"        android:textSize="30sp"        android:background="#66ff0000"/></LinearLayout>

package com.zking.administrator.myapplication5;import android.support.annotation.IdRes;import android.support.v4.app.Fragment;import android.support.v4.app.FragmentManager;import android.support.v4.app.FragmentPagerAdapter;import android.support.v4.view.ViewPager;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.widget.RadioGroup;import java.util.ArrayList;import java.util.List;public class MainActivity extends AppCompatActivity {    private ViewPager vp_main_viewpager;    private RadioGroup rg_main_radiogroup;    List<Fragment> fragments=new ArrayList<>();    int radio[]={R.id.rb_main_weixin,R.id.rb_main_contacts,R.id.rb_main_find,R.id.rb_main_me};    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        vp_main_viewpager = (ViewPager) findViewById(R.id.vp_main_viewpager);        rg_main_radiogroup = (RadioGroup) findViewById(R.id.rg_main_radiogroup);        WeiXinFragment weiXinFragment=new WeiXinFragment();        ContactsFragment contactsFragment=new ContactsFragment();        FindFragment findFragment=new FindFragment();        MeFragment meFragment=new MeFragment();        fragments.add(weiXinFragment);        fragments.add(contactsFragment);        fragments.add(findFragment);        fragments.add(meFragment);        vp_main_viewpager.setAdapter(new MyAdapter(getSupportFragmentManager()));        rg_main_radiogroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {            @Override            public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) {                for (int i = 0; i < radio.length; i++) {                    if(checkedId==radio[i]){                        vp_main_viewpager.setCurrentItem(i);                    }                }            }        });        vp_main_viewpager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {            @Override            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {            }            @Override            public void onPageSelected(int position) {            }            @Override            public void onPageScrollStateChanged(int state) {                if(state==2){                    rg_main_radiogroup.check(radio[vp_main_viewpager.getCurrentItem()]);                }            }        });    }    class MyAdapter extends FragmentPagerAdapter{        public MyAdapter(FragmentManager fm) {            super(fm);        }        @Override        public Fragment getItem(int position) {            return fragments.get(position);        }        @Override        public int getCount() {            return fragments.size();        }    }}
写四个Fragment片段,命名要规范

package com.zking.administrator.myapplication5;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;/** * Created by Administrator on 2017/6/15. */public class WeiXinFragment extends Fragment{    @Nullable    @Override    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {        return inflater.inflate(R.layout.weixin_fragment,null);    }} 
package com.zking.administrator.myapplication5;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;/** * Created by Administrator on 2017/6/15. */public class ContactsFragment extends Fragment{    @Nullable    @Override    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {        return inflater.inflate(R.layout.contacts_fragment,null);    }}

package com.zking.administrator.myapplication5;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;/** * Created by Administrator on 2017/6/15. */public class FindFragment extends Fragment{    @Nullable    @Override    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {        return inflater.inflate(R.layout.find_fragment,null);    }}
package com.zking.administrator.myapplication5;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;/** * Created by Administrator on 2017/6/15. */public class MeFragment extends Fragment{    @Nullable    @Override    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {        return inflater.inflate(R.layout.me_fragment,null);    }}