一个小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. C#调用WebService实例和开发

    一.基本概念 Web Service也叫XML Web Service WebService是一种可以接收从Internet或者Intranet上的其它系统中传递过来的请求,轻量级的独立的通讯技术.是 ...

  2. js对象克隆, 深复制.

    亲测有效: //对象克隆 function clone(obj) { // Handle the 3 simple types, and null or undefined if (null == o ...

  3. 使用DataReader读取数据

    List<User> allUsers = new List<User>(); SqlConnection conn = new SqlConnection(连接字符串); S ...

  4. Biztalk2010安装及配置问题集

    在安装Biztalk2010时,碰到很多问题,有的是粗心有的也是比较bt的,如: 1)在win7 64下引入x86 的cab,有点粗心,幸亏给我报错版本不兼容(呵呵): 2)安装的时候 不知道为什么计 ...

  5. Python3.5入门学习记录-函数

    Python 函数 函数是组织好的,可重复使用的,用来实现单一,或相关联功能的代码段. 函数能提高应用的模块性,和代码的重复利用率.你已经知道Python提供了许多内建函数,比如print().但你也 ...

  6. javascript版1024游戏源码

    <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content ...

  7. CISC + RISC = Y86

    最近在读深入理解计算机系统,打算把读时的心得放上来 Y86有着CISC和RISC的属性Y86可以看成是CISC(IA32),但用RISC的原理简化了 CISC和RISC的竞争引发了许多争论CISC和R ...

  8. (原)C++中测试代码执行时间

    转载请注明出处(不过这个用法网上到处都是): http://www.cnblogs.com/darkknightzh/p/4987738.html LARGE_INTEGER nFreq, nBegi ...

  9. 设计模式 之 Organizing the Catalog 组织目录

    Design patterns vary in their granularity and level of abstraction. Because thereare many design pat ...

  10. 【solr专题之四】关于VelocityResponseWriter

    一.关于Velocity的基本配置 在Solr中,可以以多种方式返回搜索结果,如单纯的文本回复(XML.JSON.CSV等),也可以返回velocity,js等格式.而VelocityResponse ...