MOOK实战之组件化思想封装app——1.首页框架搭建

1.整个程序的框架搭建:

HomeActivity是主Activity,因为有三个页面,所以需要三个Fragment,分别是HomeFragment,MessageFragment,MineFragment

2.命名规范:

HomeActivity,BaseActivity,HomeFragment,activity_layout_home,fragment_layout_home

3.fragment的三种切换方式:

add,replace,remove:replace:保证始终只有一个fragment
show,hide(最常用):把旧的fragment隐藏,添加新的fragment,比较消耗内存,但是最常用,不会销毁fragment
detach,attach:几乎不用,不会销毁fragment,但是会销毁里面的view

4.BaseActivity和BaseFragment的使用

1.fragment非常多,有公共的行为,如果每一个fragment都这样写,那么一旦公共行为改变,就要在每一个fragment里面修改,工作量非常大。
2.有了baseFragment,公共行为可以只在一个文件中修改
3.BaseFragment为所有fragments提供公共的行为或者事件
4.BaseActivity为所有Activity提供公共的行为

5.Application的使用

1.程序入口
2.初始化工作
3.为整个应用的其他模块提供上下文环境
4.常以单例模式构建对象
5.需要在AndroidManifest.xml文件中声明使用我们自己定义的application

6.框架布局

这里写图片描述
源代码:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >
//用来放fragment的布局
    <RelativeLayout
        android:id="@+id/content_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true" />

    <LinearLayout
        android:id="@+id/linearLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:background="@android:color/white"
        android:orientation="horizontal"
        android:paddingBottom="4dp"
        android:paddingTop="4dp">

        <RelativeLayout
            android:id="@+id/home_layout_view"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            >

            <TextView
                android:id="@+id/home_image_view"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentTop="true"
                android:layout_centerHorizontal="true"
                android:background="@drawable/comui_tab_home" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@+id/home_image_view"
                android:layout_centerHorizontal="true"
                android:layout_marginTop="4dp"
                android:text="@string/home_image_view_text"
                android:textColor="@color/color_333333"
                android:textSize="16sp"
                />
        </RelativeLayout>

        <RelativeLayout
            android:id="@+id/pond_layout_view"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:visibility="gone"
            >

            <TextView
                android:id="@+id/fish_image_view"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerHorizontal="true"
                android:background="@drawable/comui_tab_pond"
                />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@+id/fish_image_view"
                android:layout_centerHorizontal="true"
                android:layout_marginTop="4dp"
                android:text="@string/pond_image_view_text"
                android:textColor="@color/color_333333"
                android:textSize="16sp"
                />
        </RelativeLayout>

        <RelativeLayout
            android:id="@+id/message_layout_view"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            >

            <TextView
                android:id="@+id/message_image_view"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerHorizontal="true"
                android:background="@drawable/comui_tab_message"
                />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@+id/message_image_view"
                android:layout_centerHorizontal="true"
                android:layout_marginTop="4dp"
                android:text="@string/message_image_view_text"
                android:textColor="@color/color_333333"
                android:textSize="16sp"
                />
        </RelativeLayout>

        <RelativeLayout
            android:id="@+id/mine_layout_view"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            >

            <TextView
                android:id="@+id/mine_image_view"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerHorizontal="true"
                android:background="@drawable/comui_tab_home"
                />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@+id/mine_image_view"
                android:layout_centerHorizontal="true"
                android:layout_marginTop="4dp"
                android:text="@string/mine_image_view_text"
                android:textColor="@color/color_333333"
                android:textSize="16sp"
                />
        </RelativeLayout>
    </LinearLayout>

    <View
        android:layout_width="match_parent"
        android:layout_height="0.5dp"
        android:layout_above="@+id/linearLayout"
        android:background="@color/color_333333"
        />
</RelativeLayout>

HomeActivity实现页面切换代码

public class HomeActivity extends BaseActivity implements View.OnClickListener{

    private FragmentManager fm;
    private HomeFragment mHomeFragment;
    private Fragment mCommonFragmentOne;
    private MessageFragment mMessageFragment;
    private MineFragment mMineFragment;
    private Fragment mCurrent;

    private RelativeLayout mHomeLayout;
    private RelativeLayout mPondLayout;
    private RelativeLayout mMessageLayout;
    private RelativeLayout mMineLayout;
    private TextView mHomeView;
    private TextView mPondView;
    private TextView mMessageView;
    private TextView mMineView;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home_layout);
        //初始化控件
        initView();

        //添加默认要显示的fragment
        mHomeFragment=new HomeFragment();
        fm=getSupportFragmentManager();
        FragmentTransaction fragmentTransaction=fm.beginTransaction();//开启事务
        //事务的添加等操作
        fragmentTransaction.replace(R.id.content_layout,mHomeFragment);//replace-->先remove,再add,因为fragment本身为空,所以相当于是执行add操作

        //提交事务
        fragmentTransaction.commit();

    }

    private void initView() {
        mHomeLayout = (RelativeLayout) findViewById(R.id.home_layout_view);
        mHomeLayout.setOnClickListener(this);
        mPondLayout = (RelativeLayout) findViewById(R.id.pond_layout_view);
        mPondLayout.setOnClickListener(this);
        mMessageLayout = (RelativeLayout) findViewById(R.id.message_layout_view);
        mMessageLayout.setOnClickListener(this);
        mMineLayout = (RelativeLayout) findViewById(R.id.mine_layout_view);
        mMineLayout.setOnClickListener(this);

        mHomeView = (TextView) findViewById(R.id.home_image_view);
        mPondView = (TextView) findViewById(R.id.fish_image_view);
        mMessageView = (TextView) findViewById(R.id.message_image_view);
        mMineView = (TextView) findViewById(R.id.mine_image_view);
        mHomeView.setBackgroundResource(R.drawable.comui_tab_home_selected);
    }


    /*
    创建隐藏方法,用来隐藏具体的fragments
     */
    public void hideFragment(Fragment fragment,FragmentTransaction fragmentTransaction){
        if(fragment!=null){
            fragmentTransaction.hide(fragment);
        }
    }


    @Override
    public void onClick(View v) {
        //使用hide,show来切换fragment
        FragmentTransaction fragmentTransaction=fm.beginTransaction();
        switch (v.getId()){
            case R.id.home_layout_view:
                mHomeView.setBackgroundResource(R.drawable.comui_tab_home_selected);
                mPondView.setBackgroundResource(R.drawable.comui_tab_pond);
                mMessageView.setBackgroundResource(R.drawable.comui_tab_message);
                mMineView.setBackgroundResource(R.drawable.comui_tab_person);

                //隐藏其他两个fragments
                hideFragment(mMineFragment,fragmentTransaction);
                hideFragment(mMessageFragment,fragmentTransaction);

                //将我们的homefragment显示到用户面前
                if(mHomeFragment==null){
                    mHomeFragment=new HomeFragment();
                    fragmentTransaction.add(R.id.content_layout,mHomeFragment);
                }else{
                    //以及创建,直接把其他fragment隐藏,显示homeFragment
                    fragmentTransaction.show(mHomeFragment);
                }
                break;

            case R.id.message_layout_view:
                mHomeView.setBackgroundResource(R.drawable.comui_tab_home);
                mPondView.setBackgroundResource(R.drawable.comui_tab_pond);
                mMessageView.setBackgroundResource(R.drawable.comui_tab_message_selected);
                mMineView.setBackgroundResource(R.drawable.comui_tab_person);

                hideFragment(mHomeFragment,fragmentTransaction);
                hideFragment(mMineFragment,fragmentTransaction);

                if(mMessageFragment==null){
                    mMessageFragment=new MessageFragment();
                    fragmentTransaction.add(R.id.content_layout,mMessageFragment);
                }else{
                    fragmentTransaction.show(mMessageFragment);
                }
                break;

            case R.id.mine_layout_view:
                mHomeView.setBackgroundResource(R.drawable.comui_tab_home);
                mPondView.setBackgroundResource(R.drawable.comui_tab_pond);
                mMessageView.setBackgroundResource(R.drawable.comui_tab_message);
                mMineView.setBackgroundResource(R.drawable.comui_tab_person_selected);

                hideFragment(mHomeFragment,fragmentTransaction);
                hideFragment(mMessageFragment,fragmentTransaction);

                if(mMineFragment==null){
                    mMineFragment=new MineFragment();
                    fragmentTransaction.add(R.id.content_layout,mMineFragment);
                }else{
                    fragmentTransaction.show(mMineFragment);
                }
                break;
        }
        //提交事务
        fragmentTransaction.commit();
    }
}

猜你喜欢

转载自blog.csdn.net/ayangann915/article/details/80840036
今日推荐