导入依赖库:

compile 'com.android.support:design:25.3.1'

1.fg_content_demo2.xml

<?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"> <TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="123"
android:gravity="center"
android:id="@+id/fg_content_demo2_textview"/> </LinearLayout>

2.Demo2Fragment

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.TextView; import com.example.mac.mainapplication.R;
import com.example.mac.mainapplication.adapter.MainAdapter; import org.w3c.dom.Text; /**
* Created by mac on 17/6/16.
*/
public class Demo2Fragment extends Fragment { private TextView textView;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fg_content_demo2, container, false);
// tv_content = (TextView) view.findViewById(R.id.tv_content);
// String text = getArguments().getString("text");
// tv_content.setText(text); textView = (TextView)view.findViewById(R.id.fg_content_demo2_textview);
String text = getArguments().getString("text");
textView.setText(text); return view;
} // fg_content_demo2_textview
}

Demo2Adaper代码

public class Demo2Adapter extends FragmentPagerAdapter {

    private final String[] titles;
private Context context;
private List<Fragment> fragments; public Demo2Adapter(List<Fragment> fragments, String[] titles, FragmentManager fm, Context context) {
super(fm);
this.context = context;
this.fragments = fragments;
this.titles = titles;
} @Override
public Fragment getItem(int position) {
return fragments.get(position);
} @Override
public int getCount() {
return titles.length;
} @Override
public CharSequence getPageTitle(int position) {
return titles[position];
}
}

3.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">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#d33c3c"
android:gravity="center_vertical"
android:orientation="horizontal">
<android.support.design.widget.TabLayout
android:id="@+id/demo2_tablayout"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
app:tabIndicatorColor="@color/colorPrimary"
app:tabMode="scrollable"> </android.support.design.widget.TabLayout> <ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="8dp"
android:src="@drawable/ic_launcher"/> </LinearLayout> <android.support.v4.view.ViewPager
android:id="@+id/demo2_viewPager"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"> </android.support.v4.view.ViewPager> </LinearLayout>

4.main代码:

 private LinearLayout backLayout;
private String[]titles = {"项目1","项目2","项目4","项目5","项目6","项目7","项目8","项目9","项目10","项目11"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_demo2); backLayout = (LinearLayout)findViewById(R.id.default_nav_left_layout);
backLayout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
onBackPressed();
}
}); ViewPager viewPager = (ViewPager) findViewById(R.id.demo2_viewPager);
List<Fragment> fragments = new ArrayList<>();
for (int i = 0; i < titles.length; i++) {
Fragment fragment = new Demo2Fragment();
Bundle bundle = new Bundle();
bundle.putString("text",titles[i]);
fragment.setArguments(bundle);
fragments.add(fragment);
}
viewPager.setAdapter(new Demo2Adapter(fragments, titles, getSupportFragmentManager(), this)); // 初始化
TabLayout tablayout = (TabLayout) findViewById(R.id.demo2_tablayout);
// 将ViewPager和TabLayout绑定
tablayout.setupWithViewPager(viewPager);
// 设置tab文本的没有选中(第一个参数)和选中(第二个参数)的颜色
tablayout.setTabTextColors(getResources().getColor(R.color.colorPrimaryDark), Color.WHITE); }

android 模仿今日头条ViewPager+TabLayout的更多相关文章

  1. Android 代码实现viewPager+fragment 模仿今日头条的顶部导航

    模仿今日头条的顶部导航:    下载地址: http://download.csdn.net/detail/u014608640/9917700 效果图:

  2. Android 仿今日头条频道管理(下)(GridView之间Item的移动和拖拽)

    前言 上篇博客我们说到了今日头条频道管理的操作交互体验,我也介绍了2个GridView之间Item的相互移动.详情请參考:Android 仿今日头条频道管理(上)(GridView之间Item的移动和 ...

  3. [Android] Android 类似今日头条顶部的TabLayout 滑动标签栏 效果

    APP市场中大多数新闻App都有导航菜单,导航菜单是一组标签的集合,在新闻客户端中,每个标签标示一个新闻类别,对应下面ViewPager控件的一个分页面,今日头条, 网易新闻等. 本文主要讲的是用:T ...

  4. android仿今日头条App、多种漂亮加载效果、选择器汇总、记事本App、Kotlin开发等源码

    Android精选源码 android漂亮的加载效果 android各种 选择器 汇总源码 Android仿bilibili搜索框效果 Android记事本app.分类,涂鸦.添加图片或者其他附件 仿 ...

  5. Android 仿今日头条频道管理(上)(GridView之间Item的移动和拖拽)

    前言 常常逛今日头条.发现它的频道管理功能做的特别赞.交互体验很好.如图: watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fo ...

  6. Android仿今日头条和知乎等App顶部滑动导航实现代码分析及源码下载

    一.本文所涉及到的知识点 源码下载 二.目标 通过利用ViewPager+FragmentStatePagerAdapter+TabLayout 实现顶部滑动效果,如图: 三.知识点讲解 1.View ...

  7. Android仿今日头条手界面

    public class MyIndicator extends HorizontalScrollView implements ViewPager.OnPageChangeListener { pr ...

  8. (android高仿系列)今日头条 --新闻阅读器 (三) 完结 、总结 篇

    从写第一篇今日头条高仿系列开始,到现在已经过去了1个多月了,其实大体都做好了,就是迟迟没有放出来,因为我觉得,做这个东西也是有个过程的,我想把这个模仿中一步一步学习的过程,按照自己的思路写下来,在根据 ...

  9. 自适应 Tab 宽度可以滑动文字逐渐变色的 TabLayout(仿今日头条顶部导航)

    TabLayout相信大家都用过,2015年Google大会上发布了新的Android Support Design库里面包含了很多新的控件,其中就包含TabLayout,它可以配合ViewPager ...

随机推荐

  1. 讨论CSS中的各类居中方式

    今天主要谈一谈CSS中的各种居中的办法. 首先是水平居中,最简单的办法当然就是 margin:0 auto; 也就是将margin-left和margin-right属性设置为auto,从而达到水平居 ...

  2. 什么是集群(Cluster)技术

    什么是集群(Cluster)技术Cluster集群技术可如下定义:一组相互独立的服务器在网络中表现为单一的系统,并以单一系统的模式加以管理.此单一系统为客户工作站提供高可*性的服务.大多数模式下,集群 ...

  3. Java精选笔记_Filter(过滤器)

    Filter(过滤器) Filter入门 什么是Filter Filter被称作过滤器或者拦截器,其基本功能就是对Servlet容器调用Servlet的过程进行拦截,从而在Servlet进行响应处理前 ...

  4. docker学习-docker镜像

    docker就是一系列文件,

  5. Ubuntu13.10:[3]如何开启SSH SERVER服务

    作为最新版本的UBUNTU系统而言,开源,升级全部都不在话下.传说XP已经停止补丁更新了,使用UBUNTU也是一个很好的选择.ubuntu默认安装完成后只有ssh-agent(客户端模式),宾哥百度经 ...

  6. mysql concat

    CONCAT_WS() 代表 CONCAT With Separator ,是CONCAT()的特殊形式. 第一个参数是其它参数的分隔符.分隔符的位置放在要连接的两个字符串之间. 分隔符可以是一个字符 ...

  7. 《转》python学习--基础下

    转自http://www.cnblogs.com/BeginMan/archive/2013/04/12/3016323.html 一.数字 在看<Python 核心编程>的时候,我就有点 ...

  8. 【GIT】Git Flow最佳实践

    Git Flow 工作流一共包含五种分支: 两个长期分支: 主分支 master:用于存放对外发布的版本,任何时候在这个分支拿到的,都是稳定的分布版 开发分支 develop:用于日常开发,存放最新的 ...

  9. Window PHP 使用命令行模式

    电脑系统: win7 php环境: phpstudy 1 把php目录放到环境变量path下面: 我的电脑->属性->高级->环境变量->系统变量->Path->编 ...

  10. css基础---->学习html(一)

    这里零散的总结一下观看css权威指南书的知识.生命中的诸多告别,比不辞而别更让人难过的,是说一句再见,就再也没见过. 一.首字母与首行的伪类 <dvi> <p>I love y ...