Android 自定義View結(jié)合自定義TabLayout實(shí)現(xiàn)頂部標(biāo)簽滑動(dòng)效果
最近需要做一個(gè)app,需要用到tablayout實(shí)現(xiàn)頂部的滑動(dòng),用到了自定義item,不過沒有用到tabitem,貼出來供大家學(xué)習(xí),先看圖吧,覺得滿意的繼續(xù)往下面看

具體代碼實(shí)現(xiàn):
我直接貼啦,能說的不多
主布局: 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 默認(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
<?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
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è)置:
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ì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
相關(guān)文章
Android基于Sensor感應(yīng)器獲取重力感應(yīng)加速度的方法
這篇文章主要介紹了Android基于Sensor感應(yīng)器獲取重力感應(yīng)加速度的方法,涉及Android使用Sensor類實(shí)現(xiàn)感應(yīng)重力變化的功能,需要的朋友可以參考下2015-12-12
Android基礎(chǔ)之使用Fragment適應(yīng)不同屏幕和分辨率(分享)
以下是對(duì)Fragment的使用進(jìn)行了詳細(xì)的分析介紹,需要的朋友可以過來參考下2013-07-07
Android開發(fā)實(shí)現(xiàn)錄屏小功能
這篇文章主要介紹了Android開發(fā)實(shí)現(xiàn)錄屏小功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-07-07
android studio 新建項(xiàng)目報(bào)錯(cuò)的解決之路
這篇文章主要介紹了android studio 新建工程報(bào)錯(cuò),本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-03-03
Android開發(fā)框架MVC-MVP-MVVM-MVI的演變Demo
這篇文章主要為大家介紹了Android開發(fā)框架MVC-MVP-MVVM-MVI的演變Demo,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-10-10
Android實(shí)現(xiàn)多張圖片合成加載動(dòng)畫
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)多張圖片合成加載動(dòng)畫,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-07-07
Android Zxing 轉(zhuǎn)換豎屏掃描且提高識(shí)別率的方法
本篇文章主要介紹了Android Zxing 轉(zhuǎn)換豎屏掃描且提高識(shí)別率的方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-05-05
Android?JetPack組件的支持庫Databinding詳解
DataBinding是Google發(fā)布的一個(gè)數(shù)據(jù)綁定框架,它能夠讓開發(fā)者減少重復(fù)性非常高的代碼,如findViewById這樣的操作。其核心優(yōu)勢(shì)是解決了數(shù)據(jù)分解映射到各個(gè)view的問題,在MVVM框架中,實(shí)現(xiàn)的View和Viewmode的雙向數(shù)據(jù)綁定2022-08-08
微信公眾平臺(tái)開發(fā)入門教程(SAE方倍工作室)
在這篇微信公眾平臺(tái)開發(fā)教程中,我們假定你已經(jīng)有了PHP語言程序、MySQL數(shù)據(jù)庫、計(jì)算機(jī)網(wǎng)絡(luò)通訊、及HTTP/XML/CSS/JS等基礎(chǔ)2014-05-05

