Android 自定义View结合自定义TabLayout实现顶部标签滑动效果

 更新时间:2018年07月04日 10:15:21   作者:奔跑的杰尼龟  
小编最近在做app的项目,需要用到tablayout实现顶部的滑动效果,文中代码用到了自定义item,代码也很简单,感兴趣的朋友跟随脚本之家小编一起看看吧

最近需要做一个app,需要用到tablayout实现顶部的滑动,用到了自定义item,不过没有用到tabitem,贴出来供大家学习,先看图吧,觉得满意的继续往下面看

具体代码实现:

我直接贴啦,能说的不多

主布局: fragment_message.xml

<?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">
 <!--
  app:tabIndicatorHeight="1dp" 指示器高度
  app:tabIndicatorColor="@color/white" 指示器颜色
  app:tabMode 默认是fixed:固定的,标签很多时候会被挤压,不能滑动。
     scrollable 可滑动伸缩式的
 -->
 <android.support.design.widget.TabLayout
  android:id="@+id/fg_mg_tab"
  android:layout_gravity="center"
  android:layout_width="match_parent"
  android:layout_height="@dimen/toolbar_height"
  android:background="@color/colorPrimary"
  app:tabIndicatorHeight="@dimen/common_dimension_2"
  app:tabMode="fixed"
  android:fillViewport="false"
  app:tabIndicatorColor="@color/green1"
  app:tabTextAppearance="@style/core_IconTabLayout"
  app:tabTextColor="@color/white"
  />
 <android.support.v4.view.ViewPager
  android:id="@+id/fg_mg_viewpager"
  android:layout_width="match_parent"
  android:layout_height="match_parent">
 </android.support.v4.view.ViewPager>
</LinearLayout>

item布局: item_table_msg.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="wrap_content"
android:gravity="center_vertical"
android:layout_height="wrap_content">
<ImageView
 android:id="@+id/iv_msg_tab"
 android:src="@drawable/ic_msg_find"
 android:scaleType="centerInside"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content" />
<TextView
 android:layout_marginLeft="@dimen/common_dimension_2"
 android:id="@+id/tv_msg_tab"
 android:textColor="@color/white"
 android:text="会话"
 android:textSize="@dimen/SmallerTextSize"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content" />
</LinearLayout>

适配器:MessagePagerAdapter

public class MessagePagerAdapter extends FragmentPagerAdapter {
 private final List<Pair<BaseFragment,MsgTabBean>> mFragmentList = new ArrayList<>();
 private Context mContext;
 public MessagePagerAdapter(Context mContext,FragmentManager fm) {
  super(fm);
  this.mContext = mContext;
 }
 public void addFlag(Pair<BaseFragment,MsgTabBean> msgTabBeanPair){
  mFragmentList.add(msgTabBeanPair);
 }
 @Override
 public Fragment getItem(int position) {
  return mFragmentList.get(position).first;
 }
 @Override
 public int getCount() {
  return mFragmentList.size();
 }
 public View getTabView(int position, ViewGroup mGroup){
  View view;
  view = LayoutInflater.from(mContext).inflate(R.layout.view_table_msg,mGroup,false);
  ImageView img = view.findViewById(R.id.iv_msg_tab);
  TextView tv = view.findViewById(R.id.tv_msg_tab);
  img.setImageResource(mFragmentList.get(position).second.getResId());
  tv.setText(mFragmentList.get(position).second.getTitle());
  return view;
 }
}

Fragment中进行设置:    

mMessagePagerAdapter = new MessagePagerAdapter(getActivity(),getActivity().getSupportFragmentManager());
  mMessagePagerAdapter.addFlag(new Pair<>(MsgFragmentFactory.getInstance().getMsgCommListFragment(),new MsgTabBean("通讯录",R.drawable.ic_msg_comm_list)));
  mMessagePagerAdapter.addFlag(new Pair<>(MsgFragmentFactory.getInstance().getMsgCrowdFragment(),new MsgTabBean("群聊",R.drawable.ic_msg_crowd)));
  mMessagePagerAdapter.addFlag(new Pair<>(MsgFragmentFactory.getInstance().getMsgConverFragment(),new MsgTabBean("消息",R.drawable.ic_msg_conver)));
  mMessagePagerAdapter.addFlag(new Pair<>(MsgFragmentFactory.getInstance().getMsgFindFragment(),new MsgTabBean("发现",R.drawable.ic_msg_find)));
  mFgMgViewpager.setAdapter(mMessagePagerAdapter);
  mFgMgTab.setupWithViewPager(mFgMgViewpager);
  for (int index =0 ;index<mMessagePagerAdapter.getCount();index++){
   mFgMgTab.getTabAt(index).setCustomView(mMessagePagerAdapter.getTabView(index,mFgMgTab));
  }
  mFgMgViewpager.setOffscreenPageLimit(mMessagePagerAdapter.getCount());
  mFgMgViewpager.setCurrentItem(3);
 }

总结

以上所述是小编给大家介绍的Android 自定义View结合自定义TabLayout实现顶部标签滑动效果,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对脚本之家网站的支持!

相关文章

  • Android适配安卓6.0蓝牙通讯实现过程

    Android适配安卓6.0蓝牙通讯实现过程

    这篇文章主要为大家详细介绍了Android适配安卓6.0蓝牙通讯实现过程,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2016-09-09
  • Android实现为Tab添加Menu的方法

    Android实现为Tab添加Menu的方法

    这篇文章主要介绍了Android实现为Tab添加Menu的方法,分析了两种解决方法的思路并对比分析了相应的优缺点,具有一定参考借鉴价值,需要的朋友可以参考下
    2016-10-10
  • Android自定义控件仿QQ编辑和选取圆形头像

    Android自定义控件仿QQ编辑和选取圆形头像

    这篇文章主要为大家详细介绍了Android自定义控件编辑和选取圆形头像,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2016-09-09
  • Android自制九宫格解锁控件

    Android自制九宫格解锁控件

    这篇文章主要为大家详细介绍了Android自制九宫格解锁控件的方法,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-03-03
  • Android 6.0动态权限申请教程

    Android 6.0动态权限申请教程

    本文主要介绍了Android 6.0动态权限申请的教程,具有很好的参考价值。下面跟着小编一起来看下吧
    2017-03-03
  • Android调用google地图生成路线图实现代码

    Android调用google地图生成路线图实现代码

    Android程序调用本机google地图并且传递起始和终点位置生成路线图,有需要的朋有可以参考下,或许本文对你有所帮助,好了话不多说,看代码
    2013-02-02
  • Android如何禁止横屏竖屏的变换

    Android如何禁止横屏竖屏的变换

    android4.0 禁止横竖屏切换使:android:configChanges="orientation|keyboardHidden|screenSize" 感兴趣的朋友可以了解下哈
    2013-06-06
  • Android设置个性化Dialog小图标的方法

    Android设置个性化Dialog小图标的方法

    这篇文章主要介绍了Android设置个性化Dialog小图标的方法,涉及Android针对系统资源的设置与调用相关操作技巧,需要的朋友可以参考下
    2016-08-08
  • Android Universal ImageLoader 缓存图片

    Android Universal ImageLoader 缓存图片

    Universal Image Loader for Android的目的是为了实现异步的网络图片加载、缓存及显示,支持多线程异步加载,通过本文给大家介绍Android Universal ImageLoader缓存图片相关资料,涉及到imageloader缓存图片相关知识,对imageloader缓存图片相关知识感兴趣的朋友一起学习
    2016-01-01
  • Android实现指定时间定时触发方法

    Android实现指定时间定时触发方法

    这篇文章主要为大家详细介绍了Android实现指定时间定时触发方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2019-05-05

最新评论