Android TabLayout
来源:互联网 发布:人工智能专业大学排名 编辑:程序博客网 时间:2024/06/02 06:11
Android中的TabLayout配合ViewPager使用组合是目前非常常见的一种展现方式。具体的xml配置如下:
<RelativeLayout android:id="@+id/layout_tabs" android:layout_width="match_parent" android:layout_height="42dp" android:layout_below="@id/layout_title"> <android.support.design.widget.TabLayout android:id="@+id/sliding_tabs" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@color/white" app:tabGravity="fill" app:tabIndicatorColor="@color/app_main_color" app:tabIndicatorHeight="3dp" app:tabMode="fixed" app:tabSelectedTextColor="@color/app_main_color" app:tabTextAppearance="@style/MyPopularityTabTextAppearance" app:tabTextColor="@color/black" /> </RelativeLayout> <android.support.v4.view.ViewPager android:id="@+id/viewpager" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_below="@id/layout_tabs" android:background="@color/white" app:layout_behavior="@string/appbar_scrolling_view_behavior" />
下图是我的需求:
这里就需要自定义TabLayout.Tab 中的mCustomView;
TabLayout的用法这里就不阐述了,只描述上图实现
具体步骤如下:
1:初始化TabLayout后,重设一下每个Tab的View
private void setupTabViews() { for (int i = 0, size = mTabLayout.getTabCount(); i < size; i++) { mTabLayout.getTabAt(i).setCustomView(getTabView(i)); } }
2:生成View,并用一个集合装载(后面会用到)
private LinkedList<GQRecordTabView> tabViews = new LinkedList<>(); public View getTabView(int position) { GQRecordTabView tabView = (GQRecordTabView) LayoutInflater.from(this).inflate(R.layout.widget_tab_text, null); tabView.setText(mTitleList.get(position).getTitleName()); tabView.setPopData(mPopData.get(position)); tabView.setOnDateChangeListener(this); if (position == 0) { tabView.setChecked(true); tabView.setCanShowPop(true); } tabViews.add(tabView); return tabView; }
3:GQRecordTabView是一个自定义的View,我在里面给他绑定了一个PopWindow用于实现上面的下拉菜单(代码就不贴出来了,同学你按自己的逻辑写就好了)
4:自定义View后需要我们手动去change View的状态,这里我们监听ViewPager的切换事件
mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() { @Override public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { } @Override public void onPageSelected(int position) { //手动变更tabView的状态 for (int i = 0; i < tabViews.size(); i++) { if (i == position) { tabViews.get(i).setChecked(true); tabViews.get(i).setCanShowPop(true); } else { tabViews.get(i).setChecked(false); tabViews.get(i).setCanShowPop(false); } } mCurrentIndex = position; } @Override public void onPageScrollStateChanged(int state) { } });
先写到这里了,需求又改了,我得加班去了.,有问题的同学可以留言..
1 0
- Android TabLayout
- android desgin TabLayout 、Snackbar 、自定义TabLayout学习
- Android TabLayout使用以及自定义TabLayout
- TabLayout+Fragment android design
- Android 新特性 - TabLayout
- android support design tablayout
- Android TabLayout学习资料
- Android:TabLayout的用法
- android 新版 TabLayout
- Android TabLayout分析
- Android tablayout and toolbar
- Android TabLayout修改标题
- Android TabLayout的使用
- android Tablayout的使用
- Android 5.0 之TabLayout
- 一个Android TabLayout库
- Android--TabLayout的用法
- Android Material TabLayout
- Java Networking: Socket
- Liblinphone 3.9.1中文--Modules--Placing and receiving calls
- PHP 连接web services 代码
- Android FragmentPagerAdapter 更新数据
- Extjs的grid的单元格中加载超链接和按钮
- Android TabLayout
- 获取iOS设备唯一标示
- Spring <context:annotation-config>和<context:component-scan>区别
- Sublime Text 3的使用教程
- 友元函数和友元类
- POJ1065Wooden Sticks(最小上升序列)
- php框架(CI,TP,LAVAREL,SWOOLE)源码学习之TP
- 简单的线程池 c++ 11
- Big data