在如今的互联网时代,微信已是一个超级App。这篇通过ViewPager + Fragment实现一个类似于微信的界面,之前有用FragmentTabHost实现过类似界面,ViewPager的实现方式相对于FragmentTabHost的方式更简单明了。

ViewPager:

  ViewPager继承自ViewGroup,是一个容器类,可以往里添加View.

  ViewPager的使用很简单,通过setAdapter()方法设置一个PagerAdapter即可,这个PagerAdapter需要自己写,实现里面的一些方法。本篇要和Fragment结合,所以实现的是FragmentPagerAdapter类,FragmentPagerAdapter继承自PagerAdapter.

  ViewPager通过addOnPageChangeListener()方法可以设置一个ViewPager.OnPageChangeListener监听,当Pager发生变化时就调用相应的方法。

Fragment:

  Fragment有自己的生命周期, 有兴趣的可以自己通过各种方式研究下(自己打Log看是最简单的一种方式),这里就不在赘述。和ViewPager结合,有几个Pager就需要实现几个不同的Fragment.

先看一下最后实现的效果图:

布局上分为三部分:

  最上面的layout_top.xml,主要就是上面那个标题,就一个TextView,中间的ViewPager,最下面的layout_bottom.xml包括三个线性布局,每个线性布局包括一个ImageView和TextView.

activity_main.xml:

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:orientation="vertical"
tools:context="com.example.administrator.viewpagerl.MainActivity"> <include layout="@layout/layout_top"></include> <android.support.v4.view.ViewPager
android:id="@+id/ViewPagerLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1">
</android.support.v4.view.ViewPager> <include layout="@layout/layout_bottom"></include> </LinearLayout>

layout_top.xml

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="3dp"
android:paddingBottom="3dp"
android:background="@android:color/darker_gray"> <TextView
android:id="@+id/ViewTitle"
android:layout_marginLeft="20dp"
android:layout_marginTop="5dp"
android:textSize="25sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" /> </LinearLayout>

layout_bottom.xml

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:paddingTop="3dp"
android:paddingBottom="3dp"
android:background="@android:color/holo_green_light"> <LinearLayout
android:id="@+id/firstLinearLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:gravity="center_horizontal"
android:layout_weight="1">
<ImageView
android:id="@+id/firstImageView"
android:background="@drawable/tab_weixin"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/firstTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="微信"
/>
</LinearLayout>
<LinearLayout
android:id="@+id/secondLinearLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:gravity="center_horizontal"
android:layout_weight="1">
<ImageView
android:id="@+id/secondImageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/tab_setting"/>
<TextView
android:id="@+id/secondTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="朋友"
/>
</LinearLayout>
<LinearLayout
android:id="@+id/threeLinearLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:gravity="center_horizontal"
android:layout_weight="1">
<ImageView
android:id="@+id/threeImageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/tab_find"/>
<TextView
android:id="@+id/threeTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="发现"
/>
</LinearLayout>
</LinearLayout>

  上面有提到,ViewPager需要实现一个Pageradapter,很简单继承FragmentPagerAdapter,实现里面的getItem()和getCount()方法即可。

ViewPagerFragmentAdapter .java

 package com.example.administrator.viewpagerl;

 import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.util.Log; import java.util.ArrayList;
import java.util.List; public class ViewPagerFragmentAdapter extends FragmentPagerAdapter { private List<Fragment> mList = new ArrayList<Fragment>();
public ViewPagerFragmentAdapter(FragmentManager fm , List<Fragment> list) {
super(fm);
this.mList = list;
} @Override
public Fragment getItem(int position) {
return mList.get(position);
} @Override
public int getCount() {
return mList != null ? mList.size() : 0;
}
}

  ViewPager的每个Pager都需要一个Fragment,Fragment会实例化布局,显示在ViewPager的每个Pager中

ChatFragment.java

 package com.example.administrator.fragment;

 import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView; import com.example.administrator.viewpagerl.R; public class ChatFragment extends Fragment { View mView;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
if (mView == null) {
mView = inflater.inflate(R.layout.fragment_layout,null);
}
((TextView)mView.findViewById(R.id.mTextView)).setText("聊天界面");
return mView;
}
}

  这里需要三个Fragment,因为这里使用的布局很简单,三个布局基本是一致的,FriendFragment、FindFragment 这里就都不贴出代码了。微信里面的聊天列表,朋友列表都是在Fragment里面实例化的布局里有个ListView,通过ListView的方式实现的,这里只是为了记录ViewPager就没有实现那些,有兴趣的可以自己搞搞,其实也不难。

  在Activity里面只需要给ViewPager设置上面那个Adapter,设置一个监听知道Pager如何变化即可。点击最下面微信、朋友、发现三个按钮,通过ViewPager的setCurrentItem()方法就能跳转到对应的Pager,除了这些还有就是通过一些简单的逻辑,控制一下界面的改变就行,没有太难的东西。

MainActivity.java

 package com.example.administrator.viewpagerl;

 import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView; import com.example.administrator.fragment.ChatFragment;
import com.example.administrator.fragment.FindFragment;
import com.example.administrator.fragment.FriendFragment; import java.util.ArrayList;
import java.util.List; public class MainActivity extends AppCompatActivity implements View.OnClickListener { private static final String TAG = "MainActivity.TAG";
TextView titleTextView;
public LinearLayout firstLinearLayout;
public LinearLayout secondLinearLayout;
public LinearLayout threeLinearLayout;
ViewPager mViewPager;
ViewPagerFragmentAdapter mViewPagerFragmentAdapter;
FragmentManager mFragmentManager; String[] titleName = new String[] {"微信","朋友","发现"};
List<Fragment> mFragmentList = new ArrayList<Fragment>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mFragmentManager = getSupportFragmentManager();
setContentView(R.layout.activity_main);
initFragmetList();
mViewPagerFragmentAdapter = new ViewPagerFragmentAdapter(mFragmentManager,mFragmentList);
initView();
initViewPager();
} @Override
protected void onResume() {
super.onResume();
} public void initViewPager() {
mViewPager.addOnPageChangeListener(new ViewPagetOnPagerChangedLisenter());
mViewPager.setAdapter(mViewPagerFragmentAdapter);
mViewPager.setCurrentItem(0);
titleTextView.setText(titleName[0]);
updateBottomLinearLayoutSelect(true,false,false);
} public void initFragmetList() {
Fragment chat = new ChatFragment();
Fragment friend = new FriendFragment();
Fragment find = new FindFragment();
mFragmentList.add(chat);
mFragmentList.add(friend);
mFragmentList.add(find);
} public void initView() {
titleTextView = (TextView) findViewById(R.id.ViewTitle);
mViewPager = (ViewPager) findViewById(R.id.ViewPagerLayout);
firstLinearLayout = (LinearLayout) findViewById(R.id.firstLinearLayout);
firstLinearLayout.setOnClickListener(this);
secondLinearLayout = (LinearLayout) findViewById(R.id.secondLinearLayout);
secondLinearLayout.setOnClickListener(this);
threeLinearLayout = (LinearLayout) findViewById(R.id.threeLinearLayout);
threeLinearLayout.setOnClickListener(this);
} @Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.firstLinearLayout:
mViewPager.setCurrentItem(0);
updateBottomLinearLayoutSelect(true,false,false);
break;
case R.id.secondLinearLayout:
mViewPager.setCurrentItem(1);
updateBottomLinearLayoutSelect(false,true,false);
break;
case R.id.threeLinearLayout:
mViewPager.setCurrentItem(2);
updateBottomLinearLayoutSelect(false,false,true);
break;
default:
break;
}
}
private void updateBottomLinearLayoutSelect(boolean f, boolean s, boolean t) {
firstLinearLayout.setSelected(f);
secondLinearLayout.setSelected(s);
threeLinearLayout.setSelected(t);
}
class ViewPagetOnPagerChangedLisenter implements ViewPager.OnPageChangeListener {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
// Log.d(TAG,"onPageScrooled");
}
@Override
public void onPageSelected(int position) {
Log.d(TAG,"onPageSelected");
boolean[] state = new boolean[titleName.length];
state[position] = true;
titleTextView.setText(titleName[position]);
updateBottomLinearLayoutSelect(state[0],state[1],state[2]);
}
@Override
public void onPageScrollStateChanged(int state) {
Log.d(TAG,"onPageScrollStateChanged");
}
}
}

  其实就这么简单,只要动动手很容易实现的。有什么不对的地方,还望大神指点。

ViewPager + Fragment 实现类微信界面的更多相关文章

  1. 安卓开发笔记——Fragment+ViewPager组件(高仿微信界面)

    什么是ViewPager? 关于ViewPager的介绍和使用,在之前我写过一篇相关的文章<安卓开发复习笔记——ViewPager组件(仿微信引导界面)>,不清楚的朋友可以看看,这里就不再 ...

  2. 转-Fragment+ViewPager组件(高仿微信界面)

    http://www.cnblogs.com/lichenwei/p/3982302.html 什么是ViewPager? 关于ViewPager的介绍和使用,在之前我写过一篇相关的文章<安卓开 ...

  3. Android ViewPager + Fragment的布局

    ViewPager And Fragment 1.之前有篇博客是讲ViewPager的用法的:http://www.cnblogs.com/liangstudyhome/p/3773156.html ...

  4. 【原创】【ViewPager+Fragment】ViewPager中切换界面Fragment被销毁的问题分析

    ViewPager中切换界面Fragment被销毁的问题分析   1.使用场景 ViewPager+Fragment实现界面切换,界面数量>=3   2.Fragment生命周期以及与Activ ...

  5. 转载【ViewPager+Fragment】ViewPager中切换界面Fragment被销毁的问题分析

    ViewPager中切换界面Fragment被销毁的问题分析  原文链接 http://www.cnblogs.com/monodin/p/3866441.html 1.使用场景 ViewPager+ ...

  6. ViewPager Fragment 懒加载 可见 总结 MD

    Markdown版本笔记 我的GitHub首页 我的博客 我的微信 我的邮箱 MyAndroidBlogs baiqiantao baiqiantao bqt20094 baiqiantao@sina ...

  7. ViewPager Fragment PagerAdapter MD

    Markdown版本笔记 我的GitHub首页 我的博客 我的微信 我的邮箱 MyAndroidBlogs baiqiantao baiqiantao bqt20094 baiqiantao@sina ...

  8. Android ActionBar仿微信界面

    ActionBar仿微信界面 1.学习了别人的两篇关于ActionBar博客,在结合别人的文章来仿造一下微信的界面: 思路如下:1).利用ActionBar生成界面的头部,在用ActionBar的Ac ...

  9. 使用ViewPager+Fragment来实现带滚动条的多屏滑动-IndicatorFragmentActivity

    转载请注明出处:http://blog.csdn.net/singwhatiwanna/article/details/17201587 介绍 在android应用中,多屏滑动是一种很常见的风格,博主 ...

随机推荐

  1. php生成的中文文件名会变成乱码,应该这样解决

    现在php有很多类库,会生成文件,比如生成zip文件,生成二维码等等.这些类库用起来很爽,但是一旦生成带有中文的文件名,极有可能出现乱码. 问题:生成的中文文件名会变成乱码 解决:使用函数:iconv ...

  2. win7电脑自动关机怎么设置

    WIN7系统自带了关机工具的,下面是步骤 1.“开始”-右键点击“计算机”选择“管理”,在左侧界面中选择“任务计划程序”. 2.在右侧界面中选择“创建基本任务”(向导式创建任务,推荐新手使用)或者“创 ...

  3. 浅析深究什么是SOA?(转)

    http://blog.vsharing.com/fengjicheng/A1059842.html 阅读提示: 本文探讨SOA概念背后的核心内涵,如何将SOA落地的实务方法. 金蝶中间件作为全球领先 ...

  4. Spark(Hive) SQL数据类型使用详解(Python)

    Spark SQL使用时需要有若干“表”的存在,这些“表”可以来自于Hive,也可以来自“临时表”.如果“表”来自于Hive,它的模式(列名.列类型等)在创建时已经确定,一般情况下我们直接通过Spar ...

  5. Unity3d shader之次表面散射(Subsurface Scattering)

    次表面散射是一种非常常用的效果,可以用在很多材质上如皮肤,牛奶,奶油奶酪,番茄酱,土豆等等  初衷是想做一个牛奶shader的,但后来就干脆研究了sss这是在vray上的次表面散射效果 这是本文在un ...

  6. HDOJ(HDU) 2523 SORT AGAIN(推导排序、、)

    Problem Description 给你N个整数,x1,x2-xn,任取两个整数组合得到|xi-xj|,(0 < i,j<=N,i!=j). 现在请你计算第K大的组合数是哪个(一个组合 ...

  7. leetcode 字符串分割对称

    public class Solution { public List<List<String>> partition(String s) { int len=s.length ...

  8. mac下的改装人生——mbp拆卸的各种资源整理

    这几天弄了好多的mac上硬件的问题,我自己的mac也被我拆了3次,感觉自己终于跟硬件扯上关系了哈. 在这里放一些我找到比较好的资源教程,供大家学习,如果我以后还需要继续拆我的macbook,我也会回来 ...

  9. asp json

    <script language="JScript" runat="Server">function toObject(json) {    eva ...

  10. JVM性能调优-GC内存模型及垃圾收集算法

    JVM内存管理模型: http://developer.51cto.com/art/201002/184385.htm 一 JVM内存模型 1.1 Java栈 Java栈是与每一个线程关联的,JVM在 ...