导入依赖库:

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. SharePoint 2013 一次性上传文件大小限制

    默认是250MB,最大一次上传文件不超过2G The defaut size is 250MB and maximun upload size is 2047MB. It's can't greate ...

  2. SQL Server 2008 收缩日志(log)文件

    USE TestDB; GO ALTER DATABASE TestDB SET RECOVERY SIMPLE; --设置简单恢复模式 GO ); GO ALTER DATABASE TestDB ...

  3. 【PyMongo】连接10055错误的处理--windows7

    http://stackoverflow.com/questions/25621893/mongodump-utility-raise-socketexception

  4. 做BS开发,你应该知道的一些东西

    界面和用户体验(Interface and User Experience) 知道各大浏览器执行Web标准的情况,保证你的站点在主要浏览器上都能正常运行.你至少要测试以下引擎:Gecko(用于Fire ...

  5. CSS3 经典教程系列:CSS3 圆角(border-radius)详解

    http://www.cnblogs.com/lhb25/archive/2013/01/30/css3-border-radius.html 特别好的一篇文章

  6. tiny6410的linux操作系统实验开发

    ---恢复内容开始--- 1.前期由于2440 的4.3寸屏太小.后来修改程序准备在tiny6410增强版的S70屏上用.但是前期移植再用yaffs的文件系统,但是6410是(MLC)的磁盘,根本就不 ...

  7. STL 源代码剖析 算法 stl_algo.h -- search_n

    本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie search_n ------------------------------------- ...

  8. thinkjs——两表联查

    问题来源: 现有一张texture以及一张tradename表,两者的联系是texture表中有一字段名为tid对应tradename表中的id,而tradename表中却有一字段type,要求根据t ...

  9. 控制input框的内容输入为数字

    <script> function toNum(v) { return v.replace(/[^\d.]/g, '').replace(/^\./g, "").rep ...

  10. PHP中str_replace和substr_replace有什么区别?

    两个函数的定义:(1)str_replace() 函数替换字符串中的一些字符(区分大小写). 该函数必须遵循下列规则: 如果搜索的字符串是一个数组,那么它将返回一个数组. 如果搜索的字符串是一个数组, ...