TabLayout实现仿今日头条顶部tab导航效果
来源:互联网 发布:淘宝网民族风 连衣裙 编辑:程序博客网 时间:2024/05/16 13:07
今日头条的顶部tab导航效果的实现,我们一般会用RadioGroup+Fragment+ViewPager来实现,适配器繁多,代码量大,今天我们来介绍TabLayout的使用。TabLayout为Android的Design库中的一个控件。首先我们来看一下运行的效果图:
源码demo下载:http://download.csdn.net/detail/qq_30000411/9693921
整体的思路为,TabLayout+ViewPager来实现效果的布局
接下来我们看一下源码的具体实现:
主界面的XML布局为:TabLayout+ViewPager
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#ffffff"> <ImageView android:layout_width="match_parent" android:layout_height="49dp" android:background="@mipmap/search_background"/> <LinearLayout android:layout_width="match_parent" android:layout_height="35dp" android:orientation="horizontal" android:gravity="center_vertical" android:background="#eeeeee"> <android.support.design.widget.TabLayout android:id="@+id/tablayout_shouye" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" app:tabMode="scrollable" app:tabIndicatorHeight="2dp" app:tabIndicatorColor="@color/radiobuttonzhong"> </android.support.design.widget.TabLayout> </LinearLayout> <android.support.v4.view.ViewPager android:id="@+id/viewpager_ShouYe" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1"> </android.support.v4.view.ViewPager></LinearLayout>
紧着我们来实现TabLayout的适配器的代码实现:
package com.xyliwp.tablayoutdemo;import android.content.Context;import android.support.v4.app.Fragment;import android.support.v4.app.FragmentManager;import android.support.v4.app.FragmentPagerAdapter;import java.util.List;/** * Created by lwp940118 on 2016/11/25. */public class TabFragmentShouYeAdapter extends FragmentPagerAdapter { private Context context; private List<Fragment> fragments; private List<String> strings; public TabFragmentShouYeAdapter(List<Fragment> fragments, List<String> strings, FragmentManager fragmentManager, Context context){ super(fragmentManager); this.strings = strings; this.context = context; this.fragments = fragments; } @Override public Fragment getItem(int position) { return fragments.get(position); } @Override public int getCount() { return strings.size(); } @Override public CharSequence getPageTitle(int position) { return strings.get(position); }}
然后就是MainActivity的代码实现:
package com.xyliwp.tablayoutdemo;import android.support.v4.app.Fragment;import android.support.v4.app.FragmentActivity;import android.os.Bundle;import android.support.v4.view.ViewPager;import java.util.ArrayList;import java.util.List;import android.support.design.widget.TabLayout;import android.widget.RadioGroup;public class MainActivity extends FragmentActivity { private TabLayout tabLayout_shouye; private ViewPager viewPager_shouye; private List<String> strings = new ArrayList<String>();; private List<Fragment> fragments = new ArrayList<Fragment>();; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initdate(); initView(); } private void initView(){ tabLayout_shouye = (TabLayout)findViewById(R.id.tablayout_shouye); viewPager_shouye = (ViewPager)findViewById(R.id.viewpager_ShouYe); viewPager_shouye.setAdapter(new TabFragmentShouYeAdapter(fragments,strings, getSupportFragmentManager(),this)); tabLayout_shouye.setupWithViewPager(viewPager_shouye); tabLayout_shouye.setTabTextColors(getResources().getColor(R.color.radiobutton) ,getResources().getColor(R.color.radiobuttonzhong)); } private void initdate(){ Fragment1 fragment1 = new Fragment1(); fragments.add(fragment1); strings.add("推荐"); Fragement2 fragment2 = new Fragement2(); fragments.add(fragment2); strings.add("热点"); Fragement3 fragment3 = new Fragement3(); fragments.add(fragment3); strings.add("视频"); Fragment4 fragment4 = new Fragment4(); fragments.add(fragment4); strings.add("西安"); Fragment5 fragment5 = new Fragment5(); fragments.add(fragment5); strings.add("社会"); Fragment6 fragment6 = new Fragment6(); fragments.add(fragment6); strings.add("娱乐"); Fragment7 fragment7 = new Fragment7(); fragments.add(fragment7); strings.add("图片"); }}
其中ViewPager中加入的是Fragment,7个Fragment的实现可以参照一个Fragment来实现。Fragment及其XML的代码为:
package com.xyliwp.tablayoutdemo;import android.os.Bundle;import android.support.annotation.Nullable;import android.support.v4.app.Fragment;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;/** * Created by lwp940118 on 2016/11/25. */public class Fragment1 extends Fragment { private View rootView; @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { rootView = (View) inflater.inflate(R.layout.fragement1, container, false); return rootView; }}
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:layout_width="match_parent" android:layout_height="match_parent" android:textSize="24sp" android:gravity="center" android:text="推荐"/></LinearLayout>
代码及其简单这样就完成了今日头条顶部tab导航效果。
1 0
- TabLayout实现仿今日头条顶部tab导航效果
- TabLayout轻松实现仿今日头条顶部tab导航效果
- TabLayout轻松实现仿今日头条顶部tab导航效果
- 仿今日头条顶部导航效果
- Android之仿今日头条顶部导航栏效果
- Android之仿今日头条顶部导航栏效果
- Android之仿今日头条顶部导航栏效果
- Android之仿今日头条顶部导航栏效果
- 使用TabLayout实现仿今日头条的导航页效果
- Android仿今日头条、CSDN顶部Tab布局
- js实现左右滑动的导航菜单,仿今日头条顶部菜单
- Viewpager实现今日头条顶部导航的功能
- TabLayout和ViewPager实现今日头条效果
- Android仿今日头条和知乎等App顶部滑动导航实现代码分析及源码下载
- 仿今日头条顶部文字变色
- 仿今日头条渐变tab
- 仿今日头条最强顶部导航指示器,支持6种模式
- 安卓学习笔记---仿今日头条最强顶部导航指示器,支持6种模式
- HTTP状态码
- 方法重写和方法重载
- Android四大组件之broadcastReceiver
- 编译目录下面的所有的文件
- 判断整形数组是按升序还是降序排列的,采用递归的方法
- TabLayout实现仿今日头条顶部tab导航效果
- kafka笔记
- StringIndexOutOfBoundsException
- 简单完美解决PullToRefreshLayout中PullableListView嵌套ViewPager时滑动冲突的问题
- MyEclipse 6.5 Blue Edition - Milestone 1版本获取注册码(2)
- 计算机原理笔记1
- SQL基础--基础语句和运算
- Convert Number Into Roman Numeral
- 线程与进程的区别