android fragment页面切换,Fragment + TabLayout 实现切换页面

今天使用Fragment + TabLayout 实现切换

依赖

implementation 'com.android.support:design:27.1.1' //防止版本错误

xml文件

布局页面,activity_main.xml

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=".MainActivity">

android:id="@+id/toolBar"

android:layout_width="match_parent"

android:layout_height="?attr/actionBarSize"

android:background="@color/colorPrimary">

android:id="@+id/tv_title"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_gravity="center"

android:text="首页" />

android:id="@+id/frame"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:layout_weight="1" />

android:id="@+id/tab"

android:layout_width="match_parent"

android:layout_height="50dp">

fragment 页面 ,fragment_home.xml、fragment_upload.xml

fragment_home.xml

xmlns:tools="http://schemas.android.com/tools"

android:layout_width="match_parent"

android:layout_height="match_parent"

tools:context=".fragment.HomeFragment">

android:layout_width="match_parent"

android:layout_height="match_parent"

android:text="@string/hello_blank_fragment" />

fragment_upload.xml

xmlns:tools="http://schemas.android.com/tools"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:gravity="center"

tools:context=".fragment.UploadFragment">

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="@string/hello_blank_fragment" />

java文件

布局页面,MainActivity.java

public class MainActivity extends AppCompatActivity implements TabLayout.OnTabSelectedListener {

private FrameLayout mFrame;

private TabLayout mTab;

private HomeFragment mHomeFragment;

private UploadFragment mUploadFragment;

private Toolbar mToolBar;

private TextView mTvTitle;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

initView();

initTab(); // 设置TabLayout的标题

initToolBar(); // 设置ToolBar

initFragmentReplace(); // 设置开启页面时fragment的显示和影藏

mTab.addOnTabSelectedListener(this); // 设置TabLayout选择监听

}

private void initTab() {

mTab.addTab(mTab.newTab().setText("首页"));

mTab.addTab(mTab.newTab().setText("上传"));

}

private void initToolBar() {

mToolBar.setTitle("");

setSupportActionBar(mToolBar);

}

private void initFragmentReplace() {

// 获取到fragment碎片管理器

FragmentManager manager = getSupportFragmentManager();

// 开启事务

FragmentTransaction transaction = manager.beginTransaction();

// 获取到fragment的对象

mHomeFragment = new HomeFragment();

mUploadFragment = new UploadFragment();

// 设置要显示的fragment 和 影藏的fragment

transaction.add(R.id.frame, mHomeFragment, "home").show(mHomeFragment);

transaction.add(R.id.frame, mUploadFragment, "upload").hide(mUploadFragment);

// 提交事务

transaction.commit();

}

private void initView() {

// 获取控件对象

mTab = (TabLayout) findViewById(R.id.tab);

mFrame = (FrameLayout) findViewById(R.id.frame);

mToolBar = (Toolbar) findViewById(R.id.toolBar);

mTvTitle = (TextView) findViewById(R.id.tv_title);

}

@Override

public void onTabSelected(TabLayout.Tab tab) {

// 设置标题

mTvTitle.setText(tab.getText());

// 设置选中时fragment的显示和影藏

switch (tab.getPosition()) {

case 0:

FragmentManager fragmentManager = getSupportFragmentManager();

FragmentTransaction transaction = fragmentManager.beginTransaction();

transaction.show(mHomeFragment).hide(mUploadFragment).commit();

break;

case 1:

FragmentManager fragmentManager1 = getSupportFragmentManager();

FragmentTransaction transaction1 = fragmentManager1.beginTransaction();

transaction1.show(mUploadFragment).hide(mHomeFragment).commit();

break;

}

}

@Override

public void onTabUnselected(TabLayout.Tab tab) {

}

@Override

public void onTabReselected(TabLayout.Tab tab) {

}

}

fragment页面,HomeFragment.java、UploadFragment.java

HomeFragment.java

public class HomeFragment extends Fragment {

public HomeFragment() {

// Required empty public constructor

}

@Override

public View onCreateView(LayoutInflater inflater, ViewGroup container,

Bundle savedInstanceState) {

// Inflate the layout for this fragment

return inflater.inflate(R.layout.fragment_home, container, false);

}

}

UploadFragment.java

public class UploadFragment extends Fragment {

public UploadFragment() {

// Required empty public constructor

}

@Override

public View onCreateView(LayoutInflater inflater, ViewGroup container,

Bundle savedInstanceState) {

// Inflate the layout for this fragment

return inflater.inflate(R.layout.fragment_upload, container, false);

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值