App主界面布局的实现方式(一)

RadioGroup + Fragment实现

  • 01 效果图
    该实现方式只能通过切换RadioButton来切换页面,并不能通过滑动来实现。
App主界面布局的实现方式(一)_第1张图片
MainInterface.png
  • 02 layout
    4个fragment的相似布局文件
contact_fragment.xml


    

主布局文件

activity_main.xml

    
    
    
        
        
        
        
    

  • 03 Activity
    4个Fragment的相似Java代码
public class ContactFragment extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        // 这里注意inflate的第三个参数要设置为false
        View view = inflater.inflate(R.layout.contact_fragment, container, false);
        return view;
    }
}

MainActivity.java

public class MainActivity extends Activity implements OnCheckedChangeListener {
    // 用于对Fragment进行管理
    private FragmentManager fragmentManager;
    private TalkFragment talkFragment;
    private ContactFragment contactFragment;
    private DiscoveryFragment discoveryFragment;
    private PersonalFragment personalFragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_main);
        fragmentManager = getFragmentManager();
        initUI();
    }

    private void initUI() {
        RadioGroup rg_control = (RadioGroup) findViewById(R.id.rg_control);
        rg_control.setOnCheckedChangeListener(this);
        // 设置默认的Fragment
        setDefaultFragment();
    }

    private void setDefaultFragment() {
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        talkFragment = new TalkFragment();
        transaction.add(R.id.fl_content, talkFragment);
        transaction.commit();
    }

    // Radio切换的事件处理
    @Override
    public void onCheckedChanged(RadioGroup radioGroup, int checkedId) {
        switch (checkedId) {
        case R.id.rb_talk:
            setTabSelection(0);
            break;
        case R.id.rb_contact:
            setTabSelection(1);
            break;
        case R.id.rb_discovery:
            setTabSelection(2);
            break;
        case R.id.rb_personal:
            setTabSelection(3);
            break;
        }
    }

    private void setTabSelection(int index) {
        // 开启一个Fragment事务
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        // 先隐藏掉所有的Fragment
        hideFragments(transaction);
        switch (index) {
        case 0:
            if (talkFragment == null) {
                // 如果TalkFragment为空,则创建一个并添加到界面上
                talkFragment = new TalkFragment();
                transaction.add(R.id.fl_content, talkFragment);
            } else {
                // 如果TalkFragment不为空,则直接将它显示出来
                transaction.show(talkFragment);
            }
            break;
        case 1:
            if (contactFragment == null) {
                contactFragment = new ContactFragment();
                transaction.add(R.id.fl_content, contactFragment);
            } else {
                transaction.show(contactFragment);
            }
            break;
        case 2:
            if (discoveryFragment == null) {
                discoveryFragment = new DiscoveryFragment();
                transaction.add(R.id.fl_content, discoveryFragment);
            } else {
                transaction.show(discoveryFragment);
            }
            break;
        case 3:
            if (personalFragment == null) {
                personalFragment = new PersonalFragment();
                transaction.add(R.id.fl_content, personalFragment);
            } else {
                transaction.show(personalFragment);
            }
            break;
        }
        transaction.commit();
    }

    /**
     * 将所有的Fragment都置为隐藏状态。
     * 
     * @param transaction
     *            用于对Fragment执行操作的事务
     */
    private void hideFragments(FragmentTransaction transaction) {
        if (talkFragment != null) {
            transaction.hide(talkFragment);
        }
        if (contactFragment != null) {
            transaction.hide(contactFragment);
        }
        if (discoveryFragment != null) {
            transaction.hide(discoveryFragment);
        }
        if (personalFragment != null) {
            transaction.hide(personalFragment);
        }
    }
}
  • 04 横竖屏切换
    如果参考这个Demo后,在横竖屏切换时发现重叠问题,那么在AndroidManifest.xml文件中做如下设置。
    横竖屏切换时,activity不重新创建。

你可能感兴趣的:(App主界面布局的实现方式(一))