一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務(wù)器之家:專(zhuān)注于服務(wù)器技術(shù)及軟件下載分享
分類(lèi)導(dǎo)航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術(shù)|正則表達(dá)式|C/C++|IOS|C#|Swift|Android|VB|R語(yǔ)言|JavaScript|易語(yǔ)言|vb.net|

服務(wù)器之家 - 編程語(yǔ)言 - Android - Android 自定義View結(jié)合自定義TabLayout實(shí)現(xiàn)頂部標(biāo)簽滑動(dòng)效果

Android 自定義View結(jié)合自定義TabLayout實(shí)現(xiàn)頂部標(biāo)簽滑動(dòng)效果

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

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

最近需要做一個(gè)app,需要用到tablayout實(shí)現(xiàn)頂部的滑動(dòng),用到了自定義item,不過(guò)沒(méi)有用到tabitem,貼出來(lái)供大家學(xué)習(xí),先看圖吧,覺(jué)得滿意的繼續(xù)往下面看

Android 自定義View結(jié)合自定義TabLayout實(shí)現(xiàn)頂部標(biāo)簽滑動(dòng)效果

具體代碼實(shí)現(xiàn):

我直接貼啦,能說(shuō)的不多

主布局: 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 默認(rèn)是fixed:固定的,標(biāo)簽很多時(shí)候會(huì)被擠壓,不能滑動(dòng)。
     scrollable 可滑動(dòng)伸縮式的
 -->
 <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="會(huì)話"
 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中進(jìn)行設(shè)置:    

?
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("發(fā)現(xiàn)",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);
 }

總結(jié)

以上所述是小編給大家介紹的Android 自定義View結(jié)合自定義TabLayout實(shí)現(xiàn)頂部標(biāo)簽滑動(dòng)效果,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)服務(wù)器之家網(wǎng)站的支持!

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

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 欧美又黄又激烈真实床戏 | 天天色天天色天天色 | 国产精品免费久久久久影院小说 | 亚洲狠狠婷婷综合久久久久网站 | 国产成人一区二区三区 | 桥本有菜ssni-677在线观看 | 国产福利资源网在线观看 | 四虎影视e456fcom四虎影视 | 99久久国语露脸精品国产 | 精品第一国产综合精品蜜芽 | 青青在线国产视频 | 沉沦艳妇杨幂肉体小说 | 青青草伊人久久 | sao虎在线精品永久 s0e一923春菜花在线播放 | 操闺蜜| 国产亚洲sss在线观看 | 青草视频在线观看免费网站 | 国自产拍在线天天更新91 | 天天白天天谢天天啦 | 法国贵妇一级伦理hd | 欧美日韩亚洲国内综合网俺 | 99精品国产自在现线观看 | 精品久久一区 | 精品91 | 国产码一区二区三区 | 午夜影院c绿象 | 韩国日本在线观看 | 美女光屁股网站 | 午夜福利理论片在线播放 | 国产麻豆传媒在线观看 | 欧美精品色精品一区二区三区 | 91看片淫黄大片在看 | 77成人影院 | 草啪啪| 午夜一级免费视频 | 动漫精品一区二区三区3d | 动漫美女被吸乳羞羞小说 | 龟甲情感超市全文阅读 小说 | 羞羞色男人的天堂伊人久久 | 日韩无遮挡大尺度啪啪影片 | 1024国产看片在线观看 |