TabLayout+ViewPager+Fragment制作页卡
本人很懒,直接上代码了。
布局文件:
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/main_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="com.ztd.nahu.activity.HomeActivity">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.TabLayout
android:id="@+id/tab_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabIndicatorColor="@android:color/transparent"
app:tabIndicatorHeight="0dp"
app:tabSelectedTextColor="@android:color/holo_red_light"
app:tabTextColor="@android:color/black"
android:layout_alignParentBottom="true">
</android.support.design.widget.TabLayout>
<android.support.v4.view.ViewPager
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@+id/tab_layout"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />
</RelativeLayout>
</android.support.design.widget.CoordinatorLayout>
Tab自定义布局:
<?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"
android:gravity="center"
android:orientation="horizontal">
<ImageView
android:id="@+id/img"
android:layout_width="20dp"
android:layout_height="20dp" />
<TextView
android:id="@+id/title_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/tab_text_color"/>
</LinearLayout> Activity代码:
public class HomeActivity extends AppCompatActivity {
@BindView(R.id.tab_layout)
TabLayout mTabLayout;
@BindView(R.id.container)
ViewPager mViewPager;
private TabPagerAdapter mSectionsPagerAdapter;
private String[] titleArra = new String[]{"最新", "娱乐", "财经", "体育"};
private int[] imgArra = {R.mipmap.ic_launcher, R.mipmap.ic_launcher, R.mipmap.ic_launcher, R.mipmap.ic_launcher};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
ButterKnife.bind(this);
// Create the adapter that will return a fragment for each of the three
// primary sections of the activity.
mSectionsPagerAdapter = new TabPagerAdapter(this, getSupportFragmentManager(), titleArra, imgArra);
//TabLayout设置为宽度占满屏幕或者为可滚动
mTabLayout.setTabMode(TabLayout.MODE_FIXED);
// Set up the ViewPager with the sections adapter.
mViewPager.setAdapter(mSectionsPagerAdapter);
mTabLayout.setupWithViewPager(mViewPager);
//Add the tag elements
for (int i = 0; i < titleArra.length; i++) {
TabLayout.Tab tab = mTabLayout.getTabAt(i);
tab.setCustomView(mSectionsPagerAdapter.getTabView(i));
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_home, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
适配器:
public class TabPagerAdapter extends FragmentPagerAdapter {
private Context mContext;
private String[] titleArra = null;
private int[] imgArra = null;
public TabPagerAdapter(Context context, FragmentManager fm, String[] titleArra, int[] imgArra) {
super(fm);
this.mContext = context;
this.titleArra = titleArra;
this.imgArra = imgArra;
}
@Override
public Fragment getItem(int position) {
// getItem is called to instantiate the fragment for the given page.
// Return a PlaceholderFragment (defined as a static inner class below).
switch (position) {
case 0:
break;
case 1:
break;
case 2:
break;
case 3:
break;
}
return MapFragment.newInstance(position + 1);
}
@Override
public int getCount() {
return titleArra == null ? 0 : titleArra.length;
}
@Override
public CharSequence getPageTitle(int position) {
return titleArra != null ? titleArra[position] : null;
}
public View getTabView(int position) {
View view = LayoutInflater.from(mContext).inflate(R.layout.icon_layout, null);
TextView tv = (TextView) view.findViewById(R.id.title_tv);
ImageView imageView = (ImageView) view.findViewById(R.id.img);
if (imgArra == null || titleArra == null) {
return view;
}
tv.setText(titleArra[position]);
imageView.setImageResource(imgArra[position]);
return view;
}
}
TabLayout+ViewPager+Fragment制作页卡的更多相关文章
- [置顶]
xamarin Tablayout+Viewpager+Fragment顶部导航栏
最近几天不忙,所以把项目中的顶部导航栏的实现归集一下.android中使用TabLayout+ViewPager+Fragment制作顶部导航非常常见,代码实现也比较简单.当然我这个导航栏是基于xam ...
- Android开发之漫漫长途 Fragment番外篇——TabLayout+ViewPager+Fragment
该文章是一个系列文章,是本人在Android开发的漫漫长途上的一点感想和记录,我会尽量按照先易后难的顺序进行编写该系列.该系列引用了<Android开发艺术探索>以及<深入理解And ...
- 安卓TabLayout+ViewPager实现切页
安卓使用TabLayout+ViewPager+Fragment 实现页面切换,可实现左右滑动切换视图界面和点击切换 可自定义菜单栏是在顶部还是在底部 一.实现效果: 二.实现过程: 2.1 一些重要 ...
- TabLayout和ViewPager简单实现页卡的滑动
首先需要在当前的module中的build Gradle的 dependencies中加入以下句子 compile 'com.android.support:design:23.0.1' 因为我们用到 ...
- 关于tablayout+viewpager+fragment配合使用的一点记录
最近在写项目的时候遇到要求使用tablayout和fragment,遇到了这里记录一下大致思路. tablayout是头部可以左右切换的头部控制栏控件,配合viewpager使用,fragment是碎 ...
- viewpager+fragment滑动切换卡顿问题
最近在做项目的时候遇到个问题,viewpager中的fragment添加使用listview添加数据后出现滑动卡顿,造成用户体验感极差.找了很久的资料,也试了很多大方法,在这里给大家分享下: 1.添加 ...
- TabLayout ViewPager Fragment 简介 案例 MD
Markdown版本笔记 我的GitHub首页 我的博客 我的微信 我的邮箱 MyAndroidBlogs baiqiantao baiqiantao bqt20094 baiqiantao@sina ...
- ViewPager实现页卡的最新方法--简洁的TabLayout(谷歌支持包)
效果图: 添加依赖包: compile ‘com.android.support:design:‘ 布局文件: <?xml version="1.0" encoding=&q ...
- ViewPager实现页卡的3种方法(谷歌组件)
----方法一:---- 效果图: 须要的组件: ViewPager+PagerTabStrip 布局文件代码: <!--xmlns:android_custom="http://sc ...
随机推荐
- JS案例之2——cycle元素轮播
元素轮播效果是页面中经常会使用的一种效果.这个例子实现了通过元素的隐藏和显示来表现轮播效果.效果比较简单. 效果图如下: 源代码如下: <!DOCTYPE html> <html&g ...
- C#高级知识点&(ABP框架理论学习高级篇)——白金版
前言摘要 很早以前就有要写ABP高级系列教程的计划了,但是迟迟到现在这个高级理论系列才和大家见面.其实这篇博客很早就着手写了,只是楼主一直写写停停.看看下图,就知道这篇博客的生产日期了,谁知它的出厂日 ...
- 博客已经迁移至 http://barretlee.com/entry/,时而同步分享到这里
博客园是一个十分好的写作平台,不过个人比较喜欢倒腾,所以将文章都做了搬迁. 博客已经迁移至 http://barretlee.com/entry/,感谢一直以来的关注和支持. 博客订阅地址: http ...
- Eclipse安装SVN教程
第1种安装方法 下载SVN安装包.地址:subclipse.tigris.org/servlets/ProjectDocumentList?folderID=2240. 根据Eclipse版本进行下载 ...
- ES 学习总结
ES 总结: es 是基于lucene的, 是java 实现的, 很多概念和lucene是相同的 索引-- 对应数据库的表,mongoDB中的集合 文档,由字段组成, 一个字段可以出现多次. 字段,其 ...
- 未关闭InputStream 引起的血案
下面的方法是从aws s3 读取文件对象下载到本地 public int downloadFile(HttpServletResponse httpResponse, String storePath ...
- angular1
1.双向绑定: 可进可出 2.依赖注入 函数有定义方定义 3.MVC M: Model 模型--数据 V: View 视图--表现层 C: Controller 控制器--业务逻辑 4.模板: {{ ...
- jQuery系列:选择器
jQuery选择器通过标签名.属性名或内容对DOM元素进行选择,而不用担心浏览器的兼容性. 1. 基本选择器 基本选择器是jQuery中使用最频繁的选择器,由元素ID.class.元素名.多个选择符组 ...
- EF Code First Migrations数据库迁移
1.EF Code First创建数据库 新建控制台应用程序Portal,通过程序包管理器控制台添加EntityFramework. 在程序包管理器控制台中执行以下语句,安装EntityFramewo ...
- MySQL binlog中的事件类型
MySQL binlog记录的所有操作实际上都有对应的事件类型的,譬如STATEMENT格式中的DML操作对应的是QUERY_EVENT类型,ROW格式下的DML操作对应的是ROWS_EVENT类型. ...