基础框架搭建(一)

启动页面

1.启动页面代码:

public class SplashActivity extends Activity {


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_splash);
          //两秒后延迟进入主界面
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                startActivity(new Intent(SplashActivity.this,MainActivity.class));
                finish();
            }
        },2000);
    }
}
2_启动页面布局文件

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:orientation="vertical">

        <ImageView
            android:id="@+id/logo"
            android:layout_width="72dp"
            android:layout_height="72dp"
            android:layout_centerInParent="true"
            android:layout_gravity="center"
            android:focusable="true"
            android:src="@drawable/logo" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_gravity="center_horizontal"
            android:text="欢迎你"
            android:textColor="#eb2b84"
            android:textSize="18sp" />
    </LinearLayout>
</RelativeLayout>
主页面

1_主页面布局文件

<?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:orientation="vertical">

    <FrameLayout
        android:id="@+id/frameLayout"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1" />

    <RadioGroup
        android:id="@+id/rg_main"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:background="@drawable/home_bottom_parent_bg"
        android:orientation="horizontal">
    <RadioButton
        android:id="@+id/rb_home"
        style="@style/MainButtonStyle"
        android:drawableTop="@drawable/home_button_selector"
        android:text="首页" />
    <RadioButton
        android:id="@+id/rb_type"
        style="@style/MainButtonStyle"
        android:drawableTop="@drawable/type_button_selector"
        android:text="分类" />
    <RadioButton
        android:id="@+id/rb_community"
        style="@style/MainButtonStyle"
        android:drawableTop="@drawable/community_button_selector"
        android:paddingTop="10dp"
        android:text="发现" />
    <RadioButton
        android:id="@+id/rb_cart"
        style="@style/MainButtonStyle"
        android:drawableTop="@drawable/cart_button_selector"
        android:text="购物车" />
    <RadioButton
        android:id="@+id/rb_user"
        style="@style/MainButtonStyle"
        android:drawableTop="@drawable/user_button_selector"
        android:text="个人中心" />
    </RadioGroup>
</LinearLayout>
02_按钮样式

<style name="MainButtonStyle">
    <item name="android:layout_width">0dp</item>
    <item name="android:layout_height">wrap_content</item>
    <item name="android:layout_weight">1</item>
    <item name="android:textColor">@drawable/bottom_button_text_selector</item>
    <item name="android:button">@null</item>
    <item name="android:gravity">center</item>
</style>
03_设置按钮选择器

2_使用 Butterknife 初始化布局

01_配置快捷键

1)在 Setting->Plugins 中输入 butterknife 添加插件

2)Android ButterKnife Aelezny ->点击安装

02_Module 里的 build.gradle 里面添加

compile 'com.jakewharton:butterknife:8.0.1'

03_在 MainActivity 中使用

@Bind(R.id.frameLayout)
FrameLayout frameLayout;
@Bind(R.id.rb_home)
RadioButton rbHome;
@Bind(R.id.rb_type)
RadioButton rbType;
@Bind(R.id.rb_community)
RadioButton rbCommunity;
@Bind(R.id.rb_cart)
RadioButton rbCart;
@Bind(R.id.rb_user)
RadioButton rbUser;
@Bind(R.id.rg_main)
RadioGroup rgMain;
2_Fragment 的基类和各个子 Fragment

public abstract class BaseFragment extends Fragment{
    public Context mContext;
    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext=getActivity();
    }

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

        return initView();
    }

    public abstract View initView();
    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        initData();
        super.onViewCreated(view, savedInstanceState);
    }

    public  void initData(){

    }
}
02_各个子 Fragment 创建

首页:HomeFragment

分类:TypeFragment

发现:CommunityFragment

购物车:ShoppingCartFragment

用户中心:UserFragemnt


其中某个页面的内容如下:

public class UserFragment extends BaseFragment {
    private static final String TAG =
            UserFragment.class.getSimpleName();
    private TextView textView;
    @Override
    public View initView() {
        Log.e(TAG,"用户中心视图被初始化了");
        textView = new TextView(mContext);
        textView.setGravity(Gravity.CENTER);
        textView.setTextSize(25);
        textView.setTextColor(Color.RED);
        return textView;
    }


    @Override
    public void initData() {
        super.initData();
        Log.e(TAG,"用户中心数据被初始化了");
        textView.setText("用户中心");
    }
}
3_初始化各个 Fragment 并且切换

01_初始化各个 Fragment

记得在 onCreate 地方调用

private void initFrament() {
    fragments=new ArrayList<>();
    fragments.add(new HomeFragment());
    fragments.add(new TypeFragment());
    fragments.add(new CommunityFragment());
    fragments.add(new ShoppingCartFragment());
    fragments.add(new UserFragment());

}
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    ButterKnife.bind(this);
    initFrament();
    initListener();
}
02_各个 Fragment 的切换

private void initListener() {
    rgMain.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) {
            switch (checkedId){
                case R.id.rb_home:
                   position=0;
                    break;
                case R.id.rb_type:
                    position=1;
                    break;
                case R.id.rb_community:
                    position=2;
                    break;
                case R.id.rb_cart:
                    position=3;
                    break;
                case R.id.rb_user:
                    position=4;
                    break;
            }
            BaseFragment baseFragment = getFragment(position);
            switchFragment(tempFragemnt, baseFragment);
        }
    });
    rgMain.check(R.id.rb_home);
}
根据位置得到对应的 Fragment

private BaseFragment getFragment(int position) {
 if(fragments!=null&&fragments.size()>0){
     BaseFragment baseFragment = fragments.get(position);
     return baseFragment;
 }
 return null;
}
切换fragment

private void switchFragment(BaseFragment fromFragment, BaseFragment nextFragment) {
    if(tempFragemnt!=nextFragment){
        tempFragemnt=nextFragment;
        if(nextFragment!=null){
            FragmentTransaction ft=getSupportFragmentManager().beginTransaction();
            if(!nextFragment.isAdded()){
                if(fromFragment!=null){
                    ft.hide(fromFragment);
                }
                    ft.add(R.id.frameLayout,nextFragment).commit();

            }else{
                if(fromFragment!=null){
                    ft.hide(fromFragment);
                }
                ft.show(nextFragment).commit();
            }

        }
    }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值