首页框架搭建

首先看下效果图

首页框架搭建_第1张图片
首页.gif

说下命名规范

《阿里巴巴的java开发手册》:https://yq.aliyun.com/articles/69327

Activity 的 layout 以 activity_module_layout 开头
fragment的layout以fragment_module_layout开头

添加依赖

  implementation 'com.squareup.okhttp3:okhttp:3.4.1'
    implementation 'com.google.code.gson:gson:2.7'
    implementation 'de.hdodenhof:circleimageview:2.1.0'

结构图如下


首页框架搭建_第2张图片
项目结构图.png

BaseApplication

public class BaseApplication extends Application {
    private static BaseApplication mApplication = null;

    @Override
    public void onCreate() {
        super.onCreate();
        mApplication = this;
    }

    public static BaseApplication getInstance() {
        return mApplication;
    }
}

BaseActivity是为我们所有的acitivity提供公共的行为

public class BaseActivity extends AppCompatActivity {
    /**
     * 输出日志所需要的tag
     */
    private String TAG;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        TAG=getComponentName().getShortClassName();
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onStart() {
        super.onStart();
    }
}

HomeActivity创建首页所有的activity

public class HomeActivity extends BaseActivity implements View.OnClickListener {
    private FragmentManager fm;
    private HomeFramgment 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 HomeFramgment();
        fm = getSupportFragmentManager();
        fm.beginTransaction().replace(R.id.content_layout, mHomeFragment).commit();//replace先移除后add(添加)
    }

    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);
    }

    /**
     * 隐藏具体的fragment
     */
    private void hideFragment(Fragment fragment, FragmentTransaction fragmentTransaction) {
        if (fragment != null) {
            fragmentTransaction.hide(fragment);
        }
    }

    @Override
    public void onClick(View view) {
        FragmentTransaction ft = fm.beginTransaction();
        switch (view.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);
                //隐藏其他所有fragment
                hideFragment(mMineFragment,ft);
                hideFragment(mMessageFragment,ft);
                //显示当前的fragment
                if (mHomeFragment == null) {
                    mHomeFragment = new HomeFramgment();
                    ft.add(R.id.content_layout, mHomeFragment);
                } else {
                    ft.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);
                //隐藏其他所有fragment
                hideFragment(mHomeFragment,ft);
                hideFragment(mMineFragment,ft);
                //显示当前的fragment
                if (mMessageFragment == null) {
                    mMessageFragment = new MessageFragment();
                    ft.add(R.id.content_layout, mMessageFragment);
                } else {
                    ft.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);
                //隐藏其他所有fragment
                hideFragment(mHomeFragment,ft);
                hideFragment(mMessageFragment,ft);
                //显示当前的fragment
                if (mMineFragment == null) {
                    mMineFragment = new MineFragment();
                    ft.add(R.id.content_layout, mMineFragment);
                } else {
                    ft.show(mMineFragment);
                }
                break;
        }
        ft.commit();
    }
}

BaseFragment主要就是为我们所有的fragment提供公共的行为或事件

public class BaseFragment extends Fragment{
    protected Activity mContext;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public void onResume() {
        super.onResume();
    }

    @Override
    public void onPause() {
        super.onPause();
    }
}

HomeFramgment首页

public class HomeFramgment extends BaseFragment {
    private View mContentView;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        mContext = getActivity();
        mContentView = inflater.inflate(R.layout.fragment_home_layout, container, false);
        return mContentView;
    }
}

MessageFragment

public class MessageFragment extends BaseFragment {
    private View mContentView;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mContentView = inflater.inflate(R.layout.fragment_message_layout, container, false);
        return mContentView;
    }
}

MineFragment我的

public class MineFragment extends BaseFragment {
    private View mContentView;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mContentView = inflater.inflate(R.layout.fragment_mine_layout, container, false);
        return mContentView;

    }
}

布局

activity_home_layout



    

    

        

            

            
        

        

            

            
        

        

            

            
        

        

            

            
        
    

    


fragment_home_layout:



   

       

       

       
   

   

   


fragment_message_layout:



 

     
 

 

     

     

     

     
 

 

 

     

     

     

     
 

 

 

     

     

     

     
 

 


fragment_mine_layout:



    

        

        

        

    

    

        

        

            

            
        
    

    

    

    

    

    

    

    

    

    

    

你可能感兴趣的:(首页框架搭建)