一个小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. 关于EventHandler的使用

    也就是委托和事件的使用.使用过,但了解的不够深入,现在逐渐来填以前留下的坑吧. EventHandler, EventHandler<TEventArg> 是.net Framework内 ...

  2. 一篇文章讲清楚android ImageView.ScaleType

    2016-01-10 刚开始android编程的时候, 关于ImageView.ScaleType网络上好多, 说实话没看懂. 本文就是为了讲清楚这个, 有用的话转走, 请注明原地址和作者. 典型的代 ...

  3. Comparator TreeSet

    package study; import java.util.Comparator;import java.util.TreeSet; public class TreeSetTest { publ ...

  4. js post传值

    一种是ajax传值,另一种是post传值, ajax传值: $.ajax({ url: "AjaxTxt/Fild.ashx?Name=duibi&dates=" suzk ...

  5. JavaScript、Ajax与jQuery的关系

    简单总结: 1.JS是一门前端语言. 2.Ajax是一门技术,它提供了异步更新的机制,使用客户端与服务器间交换数据而非整个页面文档,实现页面的局部更新. 3.jQuery是一个框架,它对JS进行了封装 ...

  6. jQuery UI Datepicker&Datetimepicker添加 时-分-秒 并且,判断

    jQuery UI Datepicker时间(年-月-日) 相关代码: <input type="text" value="" name="ad ...

  7. mysql 二进制安装的基本步骤

    -1     mysql数据库位置      二进制包:/usr/local/mysql/data           这里手动创建新的位置:           创建一个新的lv,挂在点:mydat ...

  8. Mysql bigint 类型转为datetime

    最近在使用quartz,在mysql中其数据库表中的时间都是使用bigint类型存储的,要想使其查询结果显示为yyyy-mm-dd hh:MM:ss的格式需要使用 from_unixtime()函数, ...

  9. js 实现二叉树

    二叉树是每个结点最多有两个子树的有序树.通常子树的根被称作“左子树”(left subtree)和“右子树”(right subtree),右边的总是大于左边的!二叉树的每个结点至多只有二棵子树(不存 ...

  10. Android 匿名共享内存Java接口分析

    在Android 匿名共享内存驱动源码分析中介绍了匿名共享内存的驱动实现过程,本文在Android匿名共享内存驱动基础上,介绍Android匿名共享内存对外Android系统的匿名共享内存子系统的主体 ...