Fragment的简单使用(切换界面)


最近在做一些Android的简单布局应用,以下就是我这次做的效果,自我感觉挺漂亮就拿出来给大家分享一下

首先,要定义四个FindWorkFragment,FindTalentsFragment,FindSchoolFragment,FindStudentFragment类

注意,继承的是 Fragment类(android.support.v4.app.Fragment)

由于我就做了第一个类的具体功能,所以也就拿FindWorkFragment类在展示吧

1、FindWorkFragment布局

<?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" 
  android:background="#31C4F7">
  
  <!-- 上间隙 -->
  <View 
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="10"/>
  
  <!-- 标题 -->
  <TextView 
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="30"
    android:text="找工作"
    android:textColor="#fff"
    android:textSize="40sp"
    android:gravity="center"/>
  
  <!-- 间隙 -->
  <View 
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="15"/>
  
  <!-- 姓名 -->
   <LinearLayout 
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="15"
    android:orientation="horizontal"
    >
    
    <!-- 名称 -->
    <TextView 
      android:layout_width="0dp"
      android:layout_height="match_parent"
      android:layout_weight="50"
      android:text="姓名:"
      android:textColor="#fff"
      android:textSize="20sp"
      android:gravity="right|center_vertical"/>
    
    <!-- 间隙 -->
    <View 
      android:layout_width="0dp"
      android:layout_height="match_parent"
      android:layout_weight="2"/>
    <!-- 输入框 -->
    <EditText android:id="@+id/etName"
      android:layout_width="0dp"
      android:layout_height="match_parent"
      android:layout_weight="100"
      android:hint="请输入姓名"
      android:background="#fff"
      android:gravity="left|center_vertical"
      android:paddingLeft="10dp"
      android:textSize="23sp"/>
    <!-- 间隙 -->
    <View 
      android:layout_width="0dp"
      android:layout_height="match_parent"
      android:layout_weight="11"/>
    
  </LinearLayout>
  
  <!-- 间隙 -->
  <View 
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="20"/>
  
  <!-- 手机号 -->
   <LinearLayout 
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="15"
    android:orientation="horizontal"
    >
    
    <!-- 名称 -->
    <TextView 
      android:layout_width="0dp"
      android:layout_height="match_parent"
      android:layout_weight="50"
      android:text="手机号:"
      android:textColor="#fff"
      android:textSize="20sp"
      android:gravity="right|center_vertical"/>
    
    <!-- 间隙 -->
    <View 
      android:layout_width="0dp"
      android:layout_height="match_parent"
      android:layout_weight="2"/>
    <!-- 输入框 -->
    <EditText android:id="@+id/etPhone"
      android:layout_width="0dp"
      android:layout_height="match_parent"
      android:layout_weight="100"
      android:hint="请输入手机号"
      android:background="#fff"
      android:gravity="left|center_vertical"
      android:paddingLeft="10dp"
      android:textSize="23sp"/>
    <!-- 间隙 -->
    <View 
      android:layout_width="0dp"
      android:layout_height="match_parent"
      android:layout_weight="11"/>
    
  </LinearLayout>
  
  <!-- 间隙 -->
  <View 
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="20"/>
  
  <!-- 选择工种 -->
  <LinearLayout 
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="15"
    android:orientation="horizontal"
    >
    
    <!-- 名称 -->
    <TextView 
      android:layout_width="0dp"
      android:layout_height="match_parent"
      android:layout_weight="50"
      android:text="选择工种:"
      android:textColor="#fff"
      android:textSize="20sp"
      android:gravity="right|center_vertical"/>
    
    <!-- 间隙 -->
    <View 
      android:layout_width="0dp"
      android:layout_height="match_parent"
      android:layout_weight="2"/>
    <!-- 选择工种 -->
    <Spinner android:id="@+id/spType"
      android:layout_width="0dp"
      android:layout_weight="102"
      android:layout_height="match_parent"
      android:paddingLeft="10dp"
      android:textSize="23sp"
      android:prompt="@string/select">
    </Spinner>
      
    <!-- 间隙 -->
    <View 
      android:layout_width="0dp"
      android:layout_height="match_parent"
      android:layout_weight="10"/>
    
  </LinearLayout>
  
  <!-- 间隙 -->
  <View 
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="20"/>
  
  <!-- 发布 -->
  <LinearLayout 
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="15"
    android:orientation="horizontal">
    
    <!-- 间隙 -->
    <View 
      android:layout_width="0dp"
      android:layout_height="match_parent"
      android:layout_weight="1"/>
    
    <!-- 发布 -->
    <Button android:id="@+id/btnCommit"
      android:layout_width="0dp"
      android:layout_weight="4"
      android:layout_height="match_parent"
      android:text="发布"
      android:textColor="#fff"
      android:textSize="20sp"
      android:background="#E4BA3F"/>
    
    <!-- 间隙 -->
    <View 
      android:layout_width="0dp"
      android:layout_height="match_parent"
      android:layout_weight="1"/>

    <!-- 重置 -->
    <Button android:id="@+id/btnReset"
      android:layout_width="0dp"
      android:layout_weight="4"
      android:layout_height="match_parent"
      android:text="重置"
      android:textColor="#fff"
      android:textSize="20sp"
      android:background="#E4BA3F"/>
    
    <!-- 间隙 -->
    <View 
      android:layout_width="0dp"
      android:layout_height="match_parent"
      android:layout_weight="1"/>
    
  </LinearLayout>
  
  <!-- 底间隙 -->
  <View 
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="20"/>

</LinearLayout>
2、FindWorkFragment类的具体操作,里面说的很详细,我就不做解释了
<pre name="code" class="java">package com.example.recruitget;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;

/**
 * 找工作
 * @author FenGKun
 *
 */
public class FindWorkFragment extends Fragment {

  /** 找工作 */
  private View m_vFindWorkFragment;
  /** 姓名 */
  private EditText m_etName;
  /** 电话 */
  private EditText m_etPhone;
  /** 发布 */
  private Button m_btnCommit;
  /** 重置 */
  private Button m_btnReset;
  /** 下拉列表 */
  private Spinner m_spType;
  private ArrayAdapter<String> adapter;
  /** 选项 */
  private String[] types = new String[] {"医   生","工   人","教   师","销   售","服   务"};
  
  public void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
  }
  
  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container,
      Bundle savedInstanceState) {
    m_vFindWorkFragment = inflater.inflate(R.layout.find_work, container,false);
    
    // 初始化变量
    initUi();
    return m_vFindWorkFragment;
  }
  
  /**
   * 初始化变量
   */
  private void initUi() {
    // 姓名
    m_etName = (EditText) m_vFindWorkFragment.findViewById(R.id.etName);
    // 电话
    m_etPhone = (EditText) m_vFindWorkFragment.findViewById(R.id.etPhone);
    // 发布
    m_btnCommit = (Button) m_vFindWorkFragment.findViewById(R.id.btnCommit);
    m_btnCommit.setOnClickListener(commit);
    // 重置
    m_btnReset = (Button) m_vFindWorkFragment.findViewById(R.id.btnReset);
    m_btnReset.setOnClickListener(reset);
    // 下拉列表
    m_spType = (Spinner) m_vFindWorkFragment.findViewById(R.id.spType);
    // 定义适配器
    adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_spinner_item, types);
    //设置下拉列表的风格
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        //将adapter 添加到spinner中
        m_spType.setAdapter(adapter);
        //添加事件Spinner事件监听  
        m_spType.setOnItemSelectedListener( new itemSelectedListener());
        m_spType.setVisibility(View.VISIBLE);
        m_spType.setSelection(0, true);
  }
  
  /**
   * spinner监听类
   */
  private class itemSelectedListener implements OnItemSelectedListener{

    @Override
    public void onItemSelected(AdapterView<?> parent, View view,
        int position, long id) {
    }

    @Override
    public void onNothingSelected(AdapterView<?> parent) {
      // TODO Auto-generated method stub
      
    }
    
  }
  
  /**
   * 发布事件
   */
  private View.OnClickListener commit = new View.OnClickListener() {
    
    @Override
    public void onClick(View v) {
      // TODO Auto-generated method stub
      Toast.makeText(getActivity(), "发布成功!请静静等待...", Toast.LENGTH_SHORT).show();
    }
  };
  
  /**
   * 重置事件
   */
  private View.OnClickListener reset = new View.OnClickListener() {
    
    @Override
    public void onClick(View v) {
      m_etName.setText("");
      m_etPhone.setText("");
      m_spType.setSelection(0, true);
    }
  };
  
}

3、最后就是主页操作了,首先是MainActivity布局

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools"
  android:id="@+id/LinearLayout1"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:orientation="vertical"
  tools:context=".MainActivity" 
  android:background="#fff">

  <!-- 中间内容 -->
  <fragment android:id="@+id/fragmentPager"
    android:name="com.example.recruitget.FindWorkFragment"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="677"
    />
  
  <!-- 底部导航栏 -->
  <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="73"
    android:orientation="horizontal" >
    
    <!-- 找工作 -->
    <TextView android:id="@+id/tvWork"
      android:layout_width="0dp"
      android:layout_weight="50"
      android:layout_height="match_parent"
      android:text="找工作"
      android:gravity="center"
      android:textColor="#000"
      android:background="#E4BA3F"/>
    <!-- 分割线 -->
    <View 
      android:layout_width="0dp"
      android:layout_weight="0.5"
      android:layout_height="match_parent"
      android:background="#cccccc"/>
    
    <!-- 招人才 -->
    <TextView android:id="@+id/tvTalents"
      android:layout_width="0dp"
      android:layout_weight="50"
      android:layout_height="match_parent"
      android:text="招人才"
      android:textColor="#E4BA3F"
      android:gravity="center"/>
    <!-- 分割线 -->
    <View 
      android:layout_width="0dp"
      android:layout_weight="0.5"
      android:layout_height="match_parent"
      android:background="#ccc"/>
    <!-- 找学校 -->
    <TextView android:id="@+id/tvSchool"
      android:layout_width="0dp"
      android:layout_weight="50"
      android:layout_height="match_parent"
      android:text="找学校"
      android:gravity="center"
      android:textColor="#E4BA3F"/>
    <!-- 分割线 -->
    <View 
      android:layout_width="0dp"
      android:layout_weight="0.5"
      android:layout_height="match_parent"
      android:background="#ccc"/>
    <!-- 招学生 -->
    <TextView android:id="@+id/tvStudent"
      android:layout_width="0dp"
      android:layout_weight="50"
      android:layout_height="match_parent"
      android:text="招学生"
      android:gravity="center"
      android:textColor="#E4BA3F"/>
  </LinearLayout>

</LinearLayout>
4、MainActivity类继承FragmentActivity,具体操作如下
package com.example.recruitget;

import android.R.color;
import android.os.Bundle;
import android.app.Activity;
import android.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.Menu;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.TextView;

/**
 * 功能首页
 * @author FenGKun
 *
 */
public class MainActivity extends FragmentActivity {
  

  /** 找工作 */
  private TextView tv_findWork;
  /** 招人才 */
  private TextView tv_findTalents;
  /** 找学校 */
  private TextView tv_findSchool;
  /** 招学生 */
  private TextView tv_findStudent;
  /** 具体内容 */
  private android.support.v4.app.Fragment contentFragment;
  /** 管理fragment */
  private FragmentManager fragmentManager;
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    
    // 初始化数据
    initUi();
  }
  /**
   * 初始化变量
   */
  private void initUi() {
    // 找工作
    tv_findWork = (TextView) findViewById(R.id.tvWork);
    tv_findWork.setOnClickListener(itemClick);
    // 招人才
    tv_findTalents = (TextView) findViewById(R.id.tvTalents);
    tv_findTalents.setOnClickListener(itemClick);
    // 找学校
    tv_findSchool = (TextView) findViewById(R.id.tvSchool);
    tv_findSchool.setOnClickListener(itemClick);
    // 招学生
    tv_findStudent = (TextView) findViewById(R.id.tvStudent);
    tv_findStudent.setOnClickListener(itemClick);
    // fragment管理者
    fragmentManager = getSupportFragmentManager();
  }
  
  /**
   * 点击选项
   */
  private View.OnClickListener itemClick = new View.OnClickListener() {
    
    @Override
    public void onClick(View v) {
      tv_findWork.setBackgroundColor(0xffffffff);
      tv_findWork.setTextColor(0XFFE4BA3F);
      tv_findTalents.setBackgroundColor(0Xffffffff);
      tv_findTalents.setTextColor(0XFFE4BA3F);
      tv_findSchool.setBackgroundColor(0xffffffff);
      tv_findSchool.setTextColor(0XFFE4BA3F);
      tv_findStudent.setBackgroundColor(0xffffffff);
      tv_findStudent.setTextColor(0XFFE4BA3F);
      TextView textview = (TextView) v;
          textview.setBackgroundColor(0XFFE4BA3F);
          textview.setTextColor(0xff000000);
          // 开启一个Fragment事务
      FragmentTransaction transaction = fragmentManager.beginTransaction();
      switch (v.getId()) {
      // 找工作
      case R.id.tvWork:
        contentFragment = new FindWorkFragment();
        transaction.replace(R.id.fragmentPager, contentFragment);
        break;
      // 招人才
      case R.id.tvTalents:
        contentFragment = new FindTalentsFragment();
        transaction.replace(R.id.fragmentPager, contentFragment);
        break;
      // 找学校
      case R.id.tvSchool:
        contentFragment = new FindSchoolFragment();
        transaction.replace(R.id.fragmentPager, contentFragment);
        break;
      // 招学生
      case R.id.tvStudent:
        contentFragment = new FindStudentFragment();
        transaction.replace(R.id.fragmentPager, contentFragment);
        break;

      default:
        break;
      }
      transaction.commit();
    }
  };


}

其实,Fragment用起来很方便,而且他的兼容性很好,希望新手们都能学习一下!!!

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值