当前位置: 移动技术网 > IT编程>移动开发>Android > Android 使用Fragment模仿微信界面的实例代码

Android 使用Fragment模仿微信界面的实例代码

2019年07月24日  | 移动技术网IT编程  | 我要评论

netstat命令详解,Arturas Luskinas,色欲档案之麻雀台上淫

什么是fragment

  自从android 3.0中引入fragments 的概念,根据词海的翻译可以译为:碎片、片段。其目的是为了解决不同屏幕分辩率的动态和灵活ui设计。大屏幕如平板小屏幕如手机,平板电脑的设计使得其有更多的空间来放更多的ui组件,而多出来的空间存放ui使其会产生更多的交互,从而诞生了fragments 。

  fragments 的设计不需要你来亲自管理view hierarchy 的复杂变化,通过将activity 的布局分散到frament 中,可以在运行时修改activity 的外观,并且由activity 管理的back stack 中保存些变化。当一个片段指定了自身的布局时,它能和其他片段配置成不同的组合,在活动中为不同的屏幕尺寸修改布局配置(小屏幕可能每次显示一个片段,而大屏幕则可以显示两个或更多)。

  fragment必须被写成可重用的模块。因为fragment有自己的layout,自己进行事件响应,拥有自己的生命周期和行为,所以你可以在多个activity中包含同一个fragment的不同实例。这对于让你的界面在不同的屏幕尺寸下都能给用户完美的体验尤其重要。

fragment优点

fragment可以使你能够将activity分离成多个可重用的组件,每个都有它自己的生命周期和ui。

fragment可以轻松得创建动态灵活的ui设计,可以适应于不同的屏幕尺寸。从手机到平板电脑。

fragment是一个独立的模块,紧紧地与activity绑定在一起。可以运行中动态地移除、加入、交换等。

fragment提供一个新的方式让你在不同的安卓设备上统一你的ui。

fragment 解决activity间的切换不流畅,轻量切换。

fragment 替代tabactivity做导航,性能更好。

fragment 在4.2.版本中新增嵌套fragment使用方法,能够生成更好的界面效果。

fragment做局部内容更新更方便,原来为了到达这一点要把多个布局放到一个activity里面,现在可以用多fragment来代替,只有在需要的时候才加载fragment,提高性能。

可以从startactivityforresult中接收到返回结果,但是view不能。

这里写图片描述

图片中给出了实例的效果,在点击下方的按钮时,上半部分会自动切换成对应的内容。这里使用的技术是fragment。

想必大家对fragment已经有所了解,就算不清楚,百度也有详细的介绍。在这里就着重介绍实现的过程。

首先,拿其中的一个部分“首页”来讲:

这里写图片描述 

上面一部分是fragment,下面则是相对固定的按钮区。也就是说,当点击按钮时,切换的只是上半部分内容。所以,每一个fragment都有一个自己的xml布局文件。就想图中所示的,“首页”这个fragment的xml文件就是由一个textview构成。
完成fragment的xml文件后,需要定义一个对应的java类来找到它,比如:首页对应的类是homefragment.java。注意,这个类需要继承fragment,并且每一个这样继承fragment的类都需要重写其oncreateview的方法。具体代码是:

import android.app.fragment;
import android.os.bundle;
import android.support.annotation.nullable;
import android.view.layoutinflater;
import android.view.view;
import android.view.viewgroup;
import com.example.cerian.marcon.r;
/**
 * created by cerian on 2017/7/9.
 */
public class homefragment extends fragment {
  @override
  public view oncreateview(layoutinflater inflater, viewgroup container,
               bundle savedinstancestate) {
    view view=inflater.inflate(r.layout.fragment_home, null);
    //找到按钮前要加view.
    return view;
  }
}

完成到这步时,每一个fragment的内容就已经完成了。接下来要做的是,将每一个fragment与一个页面绑定并在其上显示。这里我用了一个menufunction.xml

这里写图片描述

代码是:

<?xml version="1.0" encoding="utf-8"?>
<relativelayout
  android:id="@+id/rl_layout"
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="match_parent"
  android:layout_height="match_parent">
  <linearlayout
    android:id="@+id/ll_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
  </linearlayout>
  <linearlayout
    android:showdividers="beginning|end|middle"
    android:background="#ffffff"
    android:layout_width="match_parent"
    android:layout_height="40dp"
    android:orientation="horizontal"
    android:layout_alignparentbottom="true"
    android:layout_alignparentleft="true"
    android:layout_alignparentstart="true">
    <imageview
      android:id="@+id/ig_home"
      android:clickable="true"
      android:layout_weight="1"
      android:layout_width="0dp"
      android:layout_height="wrap_content"
      android:src="@mipmap/homepage1"/>
    <imageview
      android:id="@+id/ig_lib"
      android:clickable="true"
      android:layout_weight="1"
      android:layout_width="0dp"
      android:layout_height="wrap_content"
      android:src="@mipmap/library1"/>
    <imageview
      android:id="@+id/ig_my"
      android:clickable="true"
      android:layout_weight="1"
      android:layout_width="0dp"
      android:layout_height="wrap_content"
      android:src="@mipmap/my1"/>
  </linearlayout>
</relativelayout>

在这个布局中,上面的linearlayout是用来显示fragment内容的,下面的是按钮。

然后,在这个menufunction.xml的对应java类中,找到定义好的fragment,并显示。主要的思想是:①拿到一个管理者②开启一个事务③替换fragment内容④提交,注意,这里的第四步很容易被遗忘。

代码是:

import android.app.fragmentmanager;
import android.app.fragmenttransaction;
import android.os.bundle;
import android.support.v7.app.appcompatactivity;
import android.view.view;
import android.widget.imageview;
import com.example.cerian.marcon.fragment.homefragment;
import com.example.cerian.marcon.fragment.libfragment;
import com.example.cerian.marcon.fragment.myfragment;
/**
 * created by cerian on 2017/7/9.
 */
public class home extends appcompatactivity implements view.onclicklistener {
  private imageview ig_home, ig_lib, ig_my;
  @override
  protected void oncreate(bundle savedinstancestate) {
    super.oncreate(savedinstancestate);
    setcontentview(r.layout.menufunction);
    ig_home = (imageview) findviewbyid(r.id.ig_home);
    ig_lib = (imageview) findviewbyid(r.id.ig_lib);
    ig_my = (imageview) findviewbyid(r.id.ig_my);
    ig_home.setonclicklistener(this);
    ig_lib.setonclicklistener(this);
    ig_my.setonclicklistener(this);
/**
 * 第一步:拿到管理者
 * 第二步:开启事务
 * 第三步:替换
 * 第四步:提交
 */
    fragmentmanager fragmentmanager = getfragmentmanager();
    fragmenttransaction begintransaction = fragmentmanager.begintransaction();
    begintransaction.replace(r.id.ll_layout, new homefragment());
    ig_home.setimageresource(r.mipmap.homepage2);
    begintransaction.commit();
  }
  @override
  public void onclick(view view) {
    fragmentmanager fragmentmanager = getfragmentmanager();
    fragmenttransaction begintransaction = fragmentmanager.begintransaction();
    switch (view.getid()) {
      case r.id.ig_home: //点击的是主页
        begintransaction.replace(r.id.ll_layout, new homefragment());
        ig_home.setimageresource(r.mipmap.homepage2);
        ig_my.setimageresource(r.mipmap.my1);
        ig_lib.setimageresource(r.mipmap.library1);
        break;
      case r.id.ig_lib: //点击的是收藏
        begintransaction.replace(r.id.ll_layout, new libfragment());
        ig_home.setimageresource(r.mipmap.homepage1);
        ig_my.setimageresource(r.mipmap.my1);
        ig_lib.setimageresource(r.mipmap.library2);
        break;
      case r.id.ig_my: //点击的是我的
        begintransaction.replace(r.id.ll_layout, new myfragment());
        ig_home.setimageresource(r.mipmap.homepage1);
        ig_my.setimageresource(r.mipmap.my2);
        ig_lib.setimageresource(r.mipmap.library1);
        break;
    }
    begintransaction.commit();
  }
}

其中,因为涉及到的点击事件有点多且相似,我用到了一个特殊的写法,也就是setonclicklistener(this),参数用了this,并重新定义了一个click方法。注意:这样写,必须要继承一个clicklistener的接口。
最后,提交就ok。

效果是:

这里写图片描述

这就是利用fragment来模拟微信界面。

以上所述是小编给大家介绍的android 使用fragment模仿微信界面的实例代码,希望对大家有所帮助

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网