一个小demo。用button+fragment实现导航栏切换界面,适合刚接触的新手看一下。

效果图

点击第二个后

源码:

主界面

<span style="font-size:18px;"><span style="font-size:14px;">package com.example.fragment;

import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button; public class MainActivity extends FragmentActivity implements OnClickListener { Button b1, b2;
FragmentManager fragmentManager;
FragmentTransaction fragmentTransaction;
ArticleFragment fragment;
ArticleFragment1 fragment1; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
fragmentManager = getFragmentManager(); fragment = new ArticleFragment();
fragment1 = new ArticleFragment1();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.fg, fragment);
fragmentTransaction.commit(); initView();
} private void initView() {
// TODO Auto-generated method stub
b1 = (Button) findViewById(R.id.b1);
b2 = (Button) findViewById(R.id.b2);
b1.setOnClickListener(this);
b2.setOnClickListener(this); } @Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.b1:
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.fg, fragment);
fragmentTransaction.commit();
break;
case R.id.b2:
FragmentTransaction fragmentTransaction1 = fragmentManager.beginTransaction();
fragmentTransaction1.replace(R.id.fg, fragment1);
fragmentTransaction1.commit();
break;
default:
break;
}
}
}

主界面布局:

<span style="font-size:18px;"><span style="font-size:14px;"><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="${relativePackage}.${activityClass}" > <LinearLayout
android:id="@+id/fg"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</LinearLayout> <LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_alignParentBottom="true"
android:weightSum="1" > <Button
android:id="@+id/b1"
android:layout_width="0dp"
android:layout_height="40dp"
android:layout_weight="0.5"
android:text="第一个" /> <Button
android:id="@+id/b2"
android:layout_width="0dp"
android:layout_height="40dp"
android:layout_weight="0.5"
android:text="第二个" />
</LinearLayout> </RelativeLayout>

第一个fragment界面

<span style="font-size:18px;"><span style="font-size:14px;">package com.example.fragment;

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup; public class ArticleFragment extends Fragment { @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
return inflater.inflate(R.layout.article_view, container,false);
} }

第一个fragment的布局

<span style="font-size:18px;"><span style="font-size:14px;"><?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" >
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/homepage"/> </ScrollView> </LinearLayout>

第二个Fragment界面

<span style="font-size:18px;"><span style="font-size:14px;">package com.example.fragment;

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup; public class ArticleFragment1 extends Fragment { @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
return inflater.inflate(R.layout.article_view1, container,false);
} }

第二个fragment的布局文件

<span style="font-size:18px;"><span style="font-size:14px;"><?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" >
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/section"/> </ScrollView> </LinearLayout>

源码下载:

http://download.csdn.net/detail/shoneworn/8316915

极致精简的fragment实现导航栏切换demo的更多相关文章

  1. [置顶] xamarin Tablayout+Viewpager+Fragment顶部导航栏

    最近几天不忙,所以把项目中的顶部导航栏的实现归集一下.android中使用TabLayout+ViewPager+Fragment制作顶部导航非常常见,代码实现也比较简单.当然我这个导航栏是基于xam ...

  2. mui底部导航栏切换分页

    使用Hbuilder的mui框架开发移动端非常便利.高效: 底部导航栏切换功能也是移动APP开发中必须实现的: 引入mui文件.下面会用到jquery,同时引进 <link href=" ...

  3. Flutter - TabBar导航栏切换后,状态丢失

    上一篇讲到了 Flutter - BottomNavigationBar底部导航栏切换后,状态丢失 里面提到了TabBar,这儿专门再写一下吧,具体怎么操作,来不让TabBar的状态丢失.毕竟大家99 ...

  4. Flutter - BottomNavigationBar底部导航栏切换后,状态丢失

    如果你用过BottomNavigationBar.TabBar.还有Drawer,你就会发现,在切换页面之后,原来的页面状态就会丢失. 要是上一页有一个数据列表,很多数据,你滚动到了下头,切换页面后, ...

  5. Flutter实战视频-移动电商-04.底部导航栏切换效果

    04.底部导航栏切换效果 博客地址: https://jspang.com/post/FlutterShop.html#toc-291 我们要做的效果图: 新建四个页面 home_page.dart ...

  6. 模仿虎牙App 导航栏切换

    昨天看虎牙直播,发现导航栏挺有意思,自己也做个玩玩 <view class="tab_list row"> <view class="tab_item ...

  7. html5 导航栏切换效果

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  8. MUI底部导航栏切换效果

    首先是html代码: <nav class="mui-bar mui-bar-tab"> <a href="view/templates/home/ho ...

  9. vue 导航栏切换

    <template> <footer class="menu"> <router-link to="/" class=" ...

随机推荐

  1. maven:安装m2eclipse插件

    一.安装maven 1.下载maven:http://pan.baidu.com/s/1hqIbx6s 2.解压并配置path路径(直接替换最新的maven包,即可升级) 3.测试 二.安装m2ecl ...

  2. .net 基础之截取字符串

    在实际开发中有时难免会遇到需要获取某个字符串中的某些字符串,这里我们可以用到字符串截取的办法. 截取字符串的方法很容易(暂不包含中文字符串),只要稍微有点.net基础的人看了都能看懂. /// < ...

  3. PHP学习笔记三十二【Exception】

    <?php // $fp=fopen("a.txt","r"); // echo "ok"; if(!file_exists(&quo ...

  4. pom.xml配置

    1:头部引用 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3 ...

  5. StringList 自定义快速排序

    unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms ...

  6. (原+转)error C2872: “ACCESS_MASK”: 不明确的符号 C:\Program Files (x86)\Windows Kits\8.1\Include\um\ktmw32.h 125

    转载请注明出处: http://www.cnblogs.com/darkknightzh/p/5500795.html 参考网址: http://www.lxway.com/88515256.htm ...

  7. 加载php_curl.dll和php_openssl.dll出错原因及解决办法

    今天在XP下安装PHP开发环境时,需要加载php_curl.dll这个动态库,自己想当然的在php.ini里把extension=php_curl.dll打开以为就可以了.可以在apache的logs ...

  8. Python学习(七) 流程控制if语句

    在Python中流程控制if语句采用如下格式: if expression : statement elif expression : statement elif expression : stat ...

  9. Lost Cows(BIT poj2182)

    Lost Cows Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10609   Accepted: 6797 Descri ...

  10. IOS开发之动态获取模型的属性值

    #import "model.h" #import <objc/runtime.h> @implementation model +(instancetype)mode ...