ViewPager 与 Fragment相结合实现微信界面实例代码

在如今的互联网时代,微信已是一个超级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相结合实现微信界面实例代码,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对脚本之家网站的支持!

时间: 2024-07-31 04:37:21

ViewPager 与 Fragment相结合实现微信界面实例代码的相关文章

ViewPager 与 Fragment相结合实现微信界面实例代码_Android

在如今的互联网时代,微信已是一个超级App.这篇通过ViewPager + Fragment实现一个类似于微信的界面,之前有用FragmentTabHost实现过类似界面,ViewPager的实现方式相对于FragmentTabHost的方式更简单明了. ViewPager: ViewPager继承自ViewGroup,是一个容器类,可以往里添加View. ViewPager的使用很简单,通过setAdapter()方法设置一个PagerAdapter即可,这个PagerAdapter需要自己写

Android程序开发之Fragment实现底部导航栏实例代码_Android

流行的应用的导航一般分为两种,一种是底部导航,一种是侧边栏. 说明 IDE:AS,Android studio; 模拟器:genymotion; 实现的效果,见下图. 具体实现 为了讲明白这个实现过程,我们贴出来的代码多一写,这样更方便理解 [最后还会放出完整的代码实现] .看上图的界面做的比较粗糙,但实现过程的骨架都具有了,想要更完美的设计,之后自行完善吧 ^0^. 布局 通过观察上述效果图,发现任意一个选项页面都有三部分组成: 顶部去除ActionBar后的标题栏: 中间一个Fragment

Android程序开发之Fragment实现底部导航栏实例代码

流行的应用的导航一般分为两种,一种是底部导航,一种是侧边栏. 说明 IDE:AS,Android studio; 模拟器:genymotion; 实现的效果,见下图. 具体实现 为了讲明白这个实现过程,我们贴出来的代码多一写,这样更方便理解 [最后还会放出完整的代码实现] .看上图的界面做的比较粗糙,但实现过程的骨架都具有了,想要更完美的设计,之后自行完善吧 ^0^. 布局 通过观察上述效果图,发现任意一个选项页面都有三部分组成: 顶部去除ActionBar后的标题栏: 中间一个Fragment

高仿Windows Phone QQ登录界面实例代码

 这篇文章主要介绍了高仿Windows Phone QQ登录界面实例代码,有需要的朋友可以参考一下 给 TextBox文本框前添加图片   扩展PhoneTextBox:添加一个类"ExtentPhoneTextBox"继承 PhoneTextBox ,在"ExtentPhoneTextBox"类中添加属性项:     代码如下: public class ExtentPhoneTextBox : PhoneTextBox     {         /// <

JavaScript判断微信浏览器实例代码_javascript技巧

先给大家说下我的项目需求:用户扫一扫二维码会产生一个链接,该链接会向后端发送个请求,返回一个 apk 的下载地址,用户点击下载按钮可以下载此 apk.然后就发生了问题,经过测试,发现用微信扫一扫打开的页面点击下载按钮下载不了 apk,后百度之,原来是微信内置浏览器屏蔽了下载链接,后面和需求方沟通,需求改为如果用户是用微信内置浏览器打开的,则提示用户换一个浏览器打开页面,否则下载不了 apk.那么该如何判断用户是否是用微信浏览器呢? 我们知道 js 可以通过 window.navigator.us

使用Bootstrap框架制作查询页面的界面实例代码_javascript技巧

以Bootstrap框架来进行设计和开发,是目前国际上比较流行的一个趋势.很多软件公司在优化新产品时,因为其在js和控件上的综合优势,会选用这个开发框架. Bootstrap框架是一个前端UI设计的框架,它提供了统一的UI界面,简化了设计界面UI的过程(缺点是定制了界面,调整的余地不是太大).尤其是现在的响应时布局(我的理解是页面根据不同的分辨率,采用不同的页面元素的布局),在Bootstrap中很好的支持了,只要简单设置了属性,就能自动实现响应时布局,大大简化了程序员的界面的过程. 因此,本人

Boostrap实现的登录界面实例代码_jquery

Bootstrap它是一个开源的web开发前端框架. 这几天我看了下Bootstrap的官方文档.看到其中的Basic-form,突然想实现下登录界面.然后想了下实现的思路,于是就打开了桌面的H5 builder码起来.代码实现起来其实不难,但是碰到个问题,就是Bootstrap的布局控制好像用.col类难以实现居中显示,虽然可以用modal(模态框)实现弹出居中,但是我暂时不想用modal框.发现问题后,第一想法是自己再定义个css进行一个控制.但是又不知道行业内的大牛是不是只用Bootstr

Android实现客户端语音动弹界面实例代码

今天为大家介绍一下语音动弹界面的实现,新版本的客户端大家应该都看过了,这里我就只简单的介绍一下控件布局了.你可以在这里看到本控件的完整源码:http://git.oschina.net/oschina/android-app/blob/master/osc-android-app/src/net/oschina/app/widget/RecordButton.java 首先,整体界面分三部分,最上层自定义ActionBar相信不需要我讲大家就能看出来了. 中间部分是文字动弹部分,主体就是一个设置

Android仿拉手网团购App我的收藏界面实例代码

先给大家展示效果图,如果感觉还不错,请参考实例代码 效果图如下所示: 具体代码如下: private void initData() { BmobManager.getInstance(new BmobQueryCallback() { @Override public void onQuerySuccess(List<? extends BaseModel> dataList) { mDataList.clear(); List<FavorModel> list = (List&