怎么在Android中自定义View和TabLayout实现顶部标签滑动效果

怎么在Android中自定义View和TabLayout实现顶部标签滑动效果?针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。

创新互联是一家专注于成都网站建设、网站建设与策划设计,溆浦网站建设哪家好?创新互联做网站,专注于网站建设十年,网设计领域的专业建站公司;建站业务涵盖:溆浦等地区。溆浦做网站价格咨询:18982081108

主布局: 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中自定义View和TabLayout实现顶部标签滑动效果
转载来源:https://www.cdcxhl.com/article32/johhsc.html

成都网站建设公司_创新互联,为您提供服务器托管动态网站ChatGPT响应式网站商城网站

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联

商城网站建设