基础布局加载

来源:互联网 发布:linux配置ip地址命令 编辑:程序博客网 时间:2024/06/06 12:33

清单文件:添加权限
<uses-permission android:name="android.permission.INTERNET" /><uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/><uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/><uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>

activity_main:

<?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"    tools:context="com.bwei.my1108.MainActivity"    android:orientation="vertical">   <FrameLayout       android:id="@+id/fl"       android:layout_width="match_parent"       android:layout_height="0dp"       android:layout_weight="1"       ></FrameLayout>   <RadioGroup       android:id="@+id/rg"       android:layout_width="match_parent"       android:layout_height="wrap_content"       android:orientation="horizontal">      <RadioButton          android:id="@+id/rb1"          android:layout_width="0dp"          android:layout_weight="1"          android:layout_marginLeft="50dp"          android:layout_height="wrap_content"          android:text="首页"          android:button="@null"/>      <RadioButton          android:id="@+id/rb2"          android:layout_width="0dp"          android:layout_weight="1"          android:layout_height="wrap_content"          android:text="好友"          android:button="@null"/>      <RadioButton          android:id="@+id/rb3"          android:layout_width="0dp"          android:layout_weight="1"          android:layout_height="wrap_content"          android:text="我的"          android:button="@null"/>      <RadioButton          android:id="@+id/rb4"          android:layout_width="0dp"          android:layout_weight="1"          android:layout_height="wrap_content"          android:text="设置"          android:button="@null"/>   </RadioGroup></LinearLayout>

MainActivity

package com.bwei.my1108;import android.os.Bundle;import android.support.annotation.IdRes;import android.support.v4.app.Fragment;import android.support.v4.app.FragmentManager;import android.support.v7.app.AppCompatActivity;import android.widget.RadioGroup;import java.util.ArrayList;import java.util.List;public class MainActivity extends AppCompatActivity {    //定义变量    private List<Fragment> fragmentList = new ArrayList<Fragment>();    private RadioGroup rg;    private FragmentManager supportFragmentManager;    String TAG = "wzq";    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        //初始化组件        rg = (RadioGroup) findViewById(R.id.rg);        initFragment();        supportFragmentManager = getSupportFragmentManager();        supportFragmentManager.beginTransaction().add(R.id.fl, fragmentList.get(0)).commit();        rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {            @Override            public void onCheckedChanged(RadioGroup radioGroup, @IdRes int i) {                switch (i) {                    case R.id.rb1:                        supportFragmentManager.beginTransaction().replace(R.id.fl,fragmentList.get(0)).commit();                        break;                    case R.id.rb2:                        supportFragmentManager.beginTransaction().replace(R.id.fl,fragmentList.get(1)).commit();                        break;                    case R.id.rb3:                        supportFragmentManager.beginTransaction().replace(R.id.fl,fragmentList.get(2)).commit();                        break;                    case R.id.rb4:                        supportFragmentManager.beginTransaction().replace(R.id.fl,fragmentList.get(3)).commit();                        break;                }            }        });    }    private void initFragment() {        F1 F1 = new F1();        F2 f2 = new F2();        F3 f3 = new F3();         F4 f4 =  new F4();        fragmentList.add(F1);        fragmentList.add(f2);        fragmentList.add(f3);        fragmentList.add(f4);    }}f1:
<?xml version="1.0" encoding="utf-8"?><android.support.v4.widget.DrawerLayout    android:id="@+id/drawer"    xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent">    <android.support.v4.view.ViewPager        android:id="@+id/vp"        android:layout_width="match_parent"        android:layout_height="match_parent">    </android.support.v4.view.ViewPager>    <ImageView        android:id="@+id/img"        android:layout_width="100dp"        android:layout_height="match_parent"        android:layout_gravity="left"        android:choiceMode="singleChoice"        android:src="@mipmap/ic_launcher"/></android.support.v4.widget.DrawerLayout>

F1
package com.bwei.my1108;import android.os.Bundle;import android.support.annotation.Nullable;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.v4.widget.DrawerLayout;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.widget.ImageView;import java.util.ArrayList;/** * author:Created by WangZhiQiang on 2017/11/9. */public class F1 extends Fragment{    private ViewPager viewpager;    private ArrayList<Fragment> fragmentList = new ArrayList<Fragment>();    private DrawerLayout drawerLayout;  /*  private FrameLayout frameLayout;    private ListView lv;*/    private ArrayList<String> lists;    //只写加载布局和找到控件    @Nullable    @Override    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {        View view = inflater.inflate(R.layout.f1, container, false);        viewpager = view.findViewById(R.id.vp);        return view;    }    /**     * 写fragment的逻辑;     * @param view     * @param savedInstanceState     */    @Override    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {        super.onViewCreated(view, savedInstanceState);        initFragment();        //fragment中嵌套子fragment要用getChildFragmentManager()        //fragment中嵌套子fragment要用getChildFragmentManager()        //fragment中嵌套子fragment要用getChildFragmentManager()        viewpager.setAdapter(new MPagerAdapter(getChildFragmentManager()));        drawerLayout = (DrawerLayout)view.findViewById(R.id.drawer);        ImageView img = (ImageView) view.findViewById(R.id.img);     //  getActivity(). getSupportFragmentManager().beginTransaction().replace(R.id.f1,mFragment).commit();        //关闭drawer        drawerLayout.closeDrawer(img);    }    private void initFragment() {        ChildFragment1 childFragment1 = new ChildFragment1();        ChildFragment2 childFragment2 = new ChildFragment2();        fragmentList.add(childFragment1);        fragmentList.add(childFragment2);    }    private class MPagerAdapter extends FragmentPagerAdapter {        public MPagerAdapter(FragmentManager fm) {            super(fm);        }        @Override        public Fragment getItem(int position) {            return fragmentList.get(position);        }        @Override        public int getCount() {            return fragmentList.size();        }    }}
f2:
<?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:background="#99ffcc">    <TextView        android:text="我是第二个子碎片"        android:layout_width="match_parent"        android:layout_height="match_parent" /></LinearLayout>

F2:
package com.bwei.my1108;import android.os.Bundle;import android.support.annotation.Nullable;import android.support.v4.app.Fragment;import android.support.v4.view.ViewPager;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;/** * author:Created by WangZhiQiang on 2017/11/8. */public class F2  extends Fragment {    private ViewPager viewPager;    @Nullable    @Override    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {        View view = inflater.inflate(R.layout.f2,container,false);        viewPager = view.findViewById(R.id.vp);        return view;    }    @Override    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {        super.onViewCreated(view, savedInstanceState);    }}