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开发岛屿数量算法示例解析

    Android开发岛屿数量算法示例解析

    这篇文章主要为大家介绍了Android开发岛屿数量算法示例解析,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-03-03
  • Android使用ViewDragHelper实现QQ聊天气泡拖动效果

    Android使用ViewDragHelper实现QQ聊天气泡拖动效果

    这篇文章主要为大家详细介绍了Android使用ViewDragHelper实现QQ聊天气泡拖动效果,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2019-01-01
  • 实例讲解Android中的AIDL内部进程通信接口使用

    实例讲解Android中的AIDL内部进程通信接口使用

    这篇文章主要通过实例介绍了Android中的AIDL内部进程通信接口使用,文中通过一个音乐播放的服务编写例子来讲解AIDL的传递对象及一般使用步骤,需要的朋友可以参考下
    2016-04-04
  • Flutter事件监听与EventBus事件的应用详解

    Flutter事件监听与EventBus事件的应用详解

    EventBus的核心是基于Streams。它允许侦听器订阅事件并允许发布者触发事件,使得不同组件的数据不需要一层层传递,可以直接通过EventBus实现跨组件通讯
    2023-04-04
  • Android中获取设备的各种信息总结

    Android中获取设备的各种信息总结

    相信各位Android的开发者们都知道,现在几乎所有的app都需要获得设备信息,那么下面这篇文章就来详细说说获取设备信息的方法。
    2016-09-09
  • Android实现悬浮窗效果

    Android实现悬浮窗效果

    这篇文章主要为大家详细介绍了Android实现悬浮窗效果,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-04-04
  • Android编程读取sd卡中图片的方法

    Android编程读取sd卡中图片的方法

    这篇文章主要介绍了Android读取sd卡中图片的方法,涉及Android权限操作,目录及文件操作的相关技巧,非常简单实用,需要的朋友可以参考下
    2016-04-04
  • Android AIDL中Map参数传递的问题详解

    Android AIDL中Map参数传递的问题详解

    这篇文章主要给大家介绍了关于Android AIDL中Map参数传递问题的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友下面来一起看看吧。
    2017-12-12
  • Android 实现将Bitmap 保存到本地

    Android 实现将Bitmap 保存到本地

    这篇文章主要介绍了Android 实现将Bitmap 保存到本地,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-03-03
  • Android仿知乎日报开屏页效果

    Android仿知乎日报开屏页效果

    这篇文章主要为大家详细介绍了Android仿知乎日报开屏页效果,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-06-06

最新评论