android框架的基本搭建
2017-05-31 09:19
387 查看
<?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.zjly.androidkuangjia.MainActivity">
<include layout="@layout/title_layout" />
<FrameLayout
android:id="@+id/frame_main"
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:background="#ccc"
android:orientation="horizontal">
<RadioButton
android:id="@+id/rb_home"
style="@style/RadioButton_Style"
android:drawableTop="@drawable/home_drawabel_rb"
android:text="首页" />
<RadioButton
android:id="@+id/rb_recycle"
style="@style/RadioButton_Style"
android:drawableTop="@drawable/recycle_drawabel_rb"
android:text="回收" />
<RadioButton
android:id="@+id/rb_huanwu"
style="@style/RadioButton_Style"
android:drawableTop="@drawable/huanwu_drawabel_rb"
android:text="换物" />
<RadioButton
android:id="@+id/rb_mine"
style="@style/RadioButton_Style"
android:drawableTop="@drawable/mine_drawabel_rb"
android:text="我的" />
</RadioGroup>
</LinearLayout>
android官方文档,Fragment包在FrameLayout中,建议用FrameLayout;
<?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="50dp"
android:background="@color/colorGreen"
android:gravity="center"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="android框架基本搭建"
android:textColor="#fff"
android:textSize="18sp" />
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:drawable="@drawable/icon_home" android:state_checked="false" /> <item android:drawable="@drawable/icon_home_checked" android:state_checked="true" /> </selector>
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:drawable="@drawable/icon_huanwu" android:state_checked="false" /> <item android:drawable="@drawable/icon_huanwu_checked" android:state_checked="true" /> </selector>
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:drawable="@drawable/icon_mine" android:state_checked="false" /> <item android:drawable="@drawable/icon_mine_cheched" android:state_checked="true" /> </selector>
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:color="#000" android:state_checked="false" /> <item android:color="@color/colorGreen" android:state_checked="true" /> </selector>
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:drawable="@drawable/icon_recycle" android:state_checked="false" /> <item android:drawable="@drawable/icon_recycle_checked" android:state_checked="true" /> </selector>
package com.zjly.androidkuangjia;
import android.os.Bundle;
import android.support.annotation.IdRes;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;
import android.widget.FrameLayout;
import android.widget.RadioGroup;
import java.util.ArrayList;
import java.util.List;
import butterknife.Bind;
import butterknife.ButterKnife;
public class MainActivity extends FragmentActivity {
@Bind(R.id.frame_main)
FrameLayout frameMain;
@Bind(R.id.rg_main)
RadioGroup rgMain;
private List<Fragment> fragments;
private int position;
private Fragment mContent;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);
initView();
initEvent();
rgMain.check(R.id.rb_home);
}
private void initEvent() {
rgMain.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup radioGroup, @IdRes int i) {
switch (i) {
case R.id.rb_home:
position = 0;
break;
case R.id.rb_recycle:
position = 1;
break;
case R.id.rb_huanwu:
position = 2;
break;
case R.id.rb_mine:
position = 3;
break;
default:
position = 0;
break;
}
Fragment toFragment = getFragment();
switchFragment(mContent,toFragment);
}
});
}
private void switchFragment(Fragment fromFragment,Fragment toFragment) {
if (fromFragment != toFragment) {
mContent=toFragment;
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
//判断有没有被添加
if (!toFragment.isAdded()) {
if (fromFragment != null) {
transaction.hide(fromFragment);
}
if (toFragment != null) {
transaction.add(R.id.frame_main, toFragment).commit();
}
} else {//已经被添加过
//隐藏以前的
if (fromFragment != null) {
4000
transaction.hide(fromFragment);
}
//显示新的
if (toFragment != null) {
transaction.show(toFragment).commit();
}
}
}
}
private Fragment getFragment() {
return fragments.get(position);
}
private void initView() {
fragments = new ArrayList<>();
fragments.add(new HomeFragment());
fragments.add(new RecycleFragment());
fragments.add(new HuanWuFragment());
fragments.add(new MineFragment());
}
}
package com.zjly.androidkuangjia;
import android.content.Context;
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/5/27.
*/
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();
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
initData();
}
protected void initData() {
}
protected abstract View initView();
}
package com.zjly.androidkuangjia; import android.graphics.Color; import android.util.Log; import android.view.Gravity; import android.view.View; import android.widget.TextView; import static android.content.ContentValues.TAG; /** * Created by Administrator on 2017/5/27. */ public class HomeFragment extends BaseFragment { @Override protected View initView() { Log.e(TAG, "HomeFragment初始化 "); TextView tv = new TextView(mContext); tv.setText("HomeFragment"); tv.setTextSize(18); tv.setTextColor(Color.RED); tv.setGravity(Gravity.CENTER); return tv; } @Override protected void initData() { Log.e(TAG, "HomeFragment加载数据 "); super.initData(); } }
package com.zjly.androidkuangjia;
import android.graphics.Color;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.widget.TextView;
import static android.content.ContentValues.TAG;
/**
* Created by Administrator on 2017/5/27.
*/
public class HuanWuFragment extends BaseFragment {
@Override
protected View initView() {
Log.e(TAG, "HuanWuFragment ");
TextView tv = new TextView(mContext);
tv.setText("HuanWuFragment");
tv.setTextSize(18);
tv.setTextColor(Color.RED);
tv.setGravity(Gravity.CENTER);
return tv;
}
@Override
protected void initData() {
Log.e(TAG, "HuanWuFragment ");
super.initData();
}
}
package com.zjly.androidkuangjia; import android.graphics.Color; import android.util.Log; import android.view.Gravity; import android.view.View; import android.widget.TextView; import static android.content.ContentValues.TAG; /** * Created by Administrator on 2017/5/27. */ public class RecycleFragment extends BaseFragment { @Override protected View initView() { Log.e(TAG, "RecycleFragment "); TextView tv = new TextView(mContext); tv.setText("RecycleFragment"); tv.setTextSize(18); tv.setTextColor(Color.RED); tv.setGravity(Gravity.CENTER); return tv; } @Override protected void initData() { Log.e(TAG, "RecycleFragment "); super.initData(); } }
package com.zjly.androidkuangjia; import android.graphics.Color; import android.util.Log; import android.view.Gravity; import android.view.View; import android.widget.TextView; import static android.content.ContentValues.TAG; /** * Created by Administrator on 2017/5/27. */ public class MineFragment extends BaseFragment { @Override protected View initView() { Log.e(TAG, "MineFragment "); TextView tv = new TextView(mContext); tv.setText("MineFragment"); tv.setTextSize(18); tv.setTextColor(Color.RED); tv.setGravity(Gravity.CENTER); return tv; } @Override protected void initData() { Log.e(TAG, "MineFragment "); super.initData(); } }
相关文章推荐
- Android实战(橘子娱乐)-首页(第一篇 MVP框架基本搭建和实现准备)
- android框架搭建(一)-基本工具[log]的建立
- Android快速开发之——最常用的基本列表框架搭建
- Android第一讲——基本框架、环境搭建
- (转)Android QQ空间(Apad)项目总结(三)---应用UI框架的搭建!!!
- Android学习之——自己搭建Http框架(1)
- android 框架搭建附源码
- 使用EasySYS搭建驱动开发基本框架
- Android图片加载框架最全解析(一),Glide的基本用法
- Android java层音频相关的分析与理解(一)基本框架
- Android中是用okhttp和son搭建项目框架是,这两个怎么联系在一起
- ssm框架的基本搭建文件
- Hibernate入门-搭建框架实现基本的增删改查
- weex sdk集成到Android工程一. 阿里Weex框架快速体验与环境搭建
- hibernate框架基本搭建及使用
- 从零搭建Android框架Android-Tractor(三) 模式选择
- IOS彩票第一天基本框架搭建
- kotlin搭建的Android框架
- [iOS微博项目 - 1.0] - 搭建基本框架
- Android GDI基本框架