导入依赖库:

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"><TextViewandroid: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;@Overridepublic 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;}@Overridepublic Fragment getItem(int position) {return fragments.get(position);}@Overridepublic int getCount() {return titles.length;}@Overridepublic 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">
<LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:background="#d33c3c"android:gravity="center_vertical"android:orientation="horizontal"><android.support.design.widget.TabLayoutandroid: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><ImageViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginRight="8dp"android:src="@drawable/ic_launcher"/></LinearLayout><android.support.v4.view.ViewPagerandroid: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"};@Overrideprotected 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() {@Overridepublic 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);}

转载于:https://www.cnblogs.com/hualuoshuijia/p/7048322.html

android 模仿今日头条ViewPager+TabLayout相关推荐

  1. Android模仿今日头条搜索页

    手闲,模拟今日头条写了个搜索页 基本功能 1.显示推荐词栏top:绑定推荐词top数据,使用RecyclerView显示: 2.显示历史记录:绑定历史记录数据,使用RecyclerView显示: 3. ...

  2. 模仿今日头条客户端的界面及简单功能实现

    模仿今日头条界面 前不久边学习边试着模仿了一下今日头条的界面,完善了一些功能,最近整理了一下,放在在gitHub上了,地址gitHub传送门. 目前客户端主要完成了界面间的交互,首页和视频两个界面的主 ...

  3. Android仿今日头条首页的顶部标签栏和底部导航栏

    Android仿今日头条首页的顶部标签栏和底部导航栏 先是底部导航栏TextView+ImageView+Fragment: 效果图: activity_main.xml布局: <?xml ve ...

  4. Android 仿今日头条首页标题栏效果

    今天带来的是仿今日头条首页的联动滑动效果,废话不多说,先上效果图: 思路: 做这个我们需要实现的效果有 1.滑动内容区域,标题栏会有变化来显示当前所处的位置. 2.点击标题栏,内容区域也会随着滑动并跳 ...

  5. android仿今日头条个人中心页面

    android仿今日头条个人中心页面 效果图 实现步骤: 自定义ScrollView,添加一个反弹的动画 代码: package com.example.administrator.gerenzhon ...

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

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

  7. Swift开源项目-模仿今日头条

    说明 首先声明,今日头条是我经常用的 app 之一,模仿今日头条也是因为感兴趣,代码仅用于学习交流.对于项目中的数据接口都是通过 Charles 抓包获得,基本每个界面都是有数据请求,不会抓包的朋友可 ...

  8. 转载 Android仿今日头条详情页实现

    转载自@ice_Anson Android仿今日头条详情页实现 源码地址: Android仿今日头条详情页实现 github源码地址 动态图 最近项目有个需求,需要实现一个和今日头条新闻详情页一样的体 ...

  9. android 今日头条加载动画,Android 仿今日头条简单的刷新效果实例代码

    点击按钮,先自动进行下拉刷新,也可以手动刷新,刷新完后,最后就多一行数据.有四个选项卡. 前两天导师要求做一个给本科学生预定机房座位的app,出发点来自这里.做着做着遇到很多问题,都解决了.这个效果感 ...

  10. Android 仿今日头条评论时键盘自动弹出的效果

    Android 仿今日头条评论时键盘自动弹出的效果:当点击评论时,弹出对话框,同时弹出软键盘,当点击返回键时,将对话框关闭,不只是关闭软键盘. 效果图: 对这个对话框设置一个style效果: < ...

最新文章

  1. python是什么语言开发的-专为人工智能和数据科学而生的Go语言,或将取代Python...
  2. mac基础操作:在Mac上怎么使用夜览
  3. restapi(0)- 平台数据维护,写在前面
  4. Hex Workshop(16进制编辑利器) 6.7.2绿色版
  5. Node编写API接口,ajax实现增删改查
  6. Java拦截器的简单使用
  7. 如何搭建tftp服务器
  8. 声音信号处理基频检测和时频分析
  9. Android Activity 硬件加速
  10. 本题要求你计算A−B。不过麻烦的是,A和B都是字符串 —— 即从字符串A中把字符串B所包含的字符全删掉,剩下的字符组成的就是字符串A−B。
  11. 前端基础知识第四章---CSS
  12. 2022年高考全国甲卷英语作文保护海洋
  13. M个苹果放到N个相同盘子和N个不同盘子的解法
  14. 美团买单与微信分付入局后,蚂蚁花呗和京东白条地位不保了?
  15. OCR文本识别,文本行检测技术。
  16. 二维数组的定义和使用
  17. 用SHGetFileInfo函数获取文件信息
  18. QQ群排名优化:“小百度”大蓝海有搞头
  19. js处理本地.bin音频文件和node.js的fs模块处理本地.bin音频区别
  20. Apache DolphinScheduler——开源大数据调度器神器

热门文章

  1. 讲python现状的文章_用 Python 分析 Python 工作现状
  2. python已知y求x_Python刷题:集合S(k)求|x-y|最小时的x和y(位运算)
  3. 【2019杭电多校第五场1007=HDU6630】permutation 2(打表找规律+分情况讨论)
  4. 图片打散存储JAVA_通过java的i/o机制进行图片流的存储以及对网络图片的存储
  5. 关于python随机抽取各类型不重复值的思考(sample与randint的区别)
  6. 马尔科夫决策过程(MDP) : GridWord(DP)
  7. Python:内置类型
  8. 不能忽视的情绪 -- 喜怒哀惧,也不过度关注
  9. AWS DeepRacer 参数调优 Amazon SageMaker 和 Amazon RoboMaker
  10. 算法:找出存在子字符串的最先位置28. Implement strStr()