激情久久久_欧美视频区_成人av免费_不卡视频一二三区_欧美精品在欧美一区二区少妇_欧美一区二区三区的

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - Android - Android 自定義View結合自定義TabLayout實現頂部標簽滑動效果

Android 自定義View結合自定義TabLayout實現頂部標簽滑動效果

2022-03-08 16:07奔跑的杰尼龜 Android

小編最近在做app的項目,需要用到tablayout實現頂部的滑動效果,文中代碼用到了自定義item,代碼也很簡單,感興趣的朋友跟隨腳本之家小編一起看看吧

最近需要做一個app,需要用到tablayout實現頂部的滑動,用到了自定義item,不過沒有用到tabitem,貼出來供大家學習,先看圖吧,覺得滿意的繼續往下面看

Android 自定義View結合自定義TabLayout實現頂部標簽滑動效果

具體代碼實現:

我直接貼啦,能說的不多

主布局: fragment_message.xml

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
<?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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<?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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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中進行設置:    

?
1
2
3
4
5
6
7
8
9
10
11
12
13
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實現頂部標簽滑動效果,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對服務器之家網站的支持!

原文鏈接:https://blog.csdn.net/crazyZhangxl/article/details/80106594

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 91视频网 | 黄色大片免费看 | 91九色论坛 | 精品久久久久久国产三级 | 日本高清黄色片 | 日韩黄色av | 成人精品视频网站 | 毛片网站视频 | 中文字幕在线播放不卡 | 国产福利不卡一区二区三区 | 亚洲综合一区在线观看 | 在线成人精品视频 | 竹内纱里奈和大战黑人 | 99久久精品免费视频 | 免费毛片视频播放 | 精品国产乱码久久久久久久久 | 成人三级黄色片 | 色av成人| 在线观看国产一区二区三区 | 性欧美视频在线观看 | 色淫视频 | 欧美在线观看视频一区 | 午夜91视频 | 久久免费精品视频 | 欧美成人免费小视频 | 特片网久久 | 国产精品成人av片免费看最爱 | 精品一区二区三区在线观看视频 | 黄色的视频在线观看 | 欧美一级一区二区三区 | 51国产偷自视频区视频小蝌蚪 | 久草视频福利在线观看 | 91伊人久久 | 国产成人aⅴ | 精品一区久久久 | 欧美性生活区 | 密室逃脱第一季免费观看完整在线 | 色淫网站免费视频 | 久久免费视频1 | 色av成人天堂桃色av | 他也色在线视频 |