這篇文章主要介紹怎么使用Android實(shí)現(xiàn)底部切換標(biāo)簽,文中介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們一定要看完!
成都創(chuàng)新互聯(lián)公司是一家集網(wǎng)站建設(shè),山陽(yáng)企業(yè)網(wǎng)站建設(shè),山陽(yáng)品牌網(wǎng)站建設(shè),網(wǎng)站定制,山陽(yáng)網(wǎng)站建設(shè)報(bào)價(jià),網(wǎng)絡(luò)營(yíng)銷,網(wǎng)絡(luò)優(yōu)化,山陽(yáng)網(wǎng)站推廣為一體的創(chuàng)新建站企業(yè),幫助傳統(tǒng)企業(yè)提升企業(yè)形象加強(qiáng)企業(yè)競(jìng)爭(zhēng)力。可充分滿足這一群體相比中小企業(yè)更為豐富、高端、多元的互聯(lián)網(wǎng)需求。同時(shí)我們時(shí)刻保持專業(yè)、時(shí)尚、前沿,時(shí)刻以成就客戶成長(zhǎng)自我,堅(jiān)持不斷學(xué)習(xí)、思考、沉淀、凈化自己,讓我們?yōu)楦嗟钠髽I(yè)打造出實(shí)用型網(wǎng)站。
具體內(nèi)容如下
實(shí)現(xiàn)底部通用切換標(biāo)簽 ,嵌套Fragment,方便自定義布局
自定義控件:
widget_tab_view.xml
定義單個(gè)標(biāo)簽
public class TabView extends LinearLayout { private ImageView mTabImage; private TextView mTabLable; public TabView(Context context) { super(context); initView(context); } public TabView(Context context, @Nullable AttributeSet attrs) { super(context, attrs); initView(context); } public TabView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); initView(context); } private void initView(Context context) { setOrientation(VERTICAL); setGravity(Gravity.CENTER); LayoutInflater.from(context).inflate(R.layout.widget_tab_view, this, true); mTabImage = (ImageView) findViewById(R.id.tab_image); mTabLable = (TextView) findViewById(R.id.tab_label); } public void initData(TabItem tabItem) { mTabImage.setImageResource(tabItem.imageResId); mTabLable.setText(tabItem.lableResId); } }
定義單個(gè)標(biāo)簽的entity
public class TabItem { public int imageResId; public int lableResId; public Class extends Fragment> tagFragmentClz; public TabItem(int imageResId, int lableResId) { this.imageResId = imageResId; this.lableResId = lableResId; } public TabItem(int imageResId, int lableResId, Class extends Fragment> tagFragmentClz) { this.imageResId = imageResId; this.lableResId = lableResId; this.tagFragmentClz = tagFragmentClz; } }
定義底部切換標(biāo)簽控件
public class BottomTabLayout extends LinearLayout implements View.OnClickListener { private ArrayListtabs; private OnTabClickListener listener; private int tabCount; private View selectedView; public BottomTabLayout(Context context) { super(context); initView(); } public BottomTabLayout(Context context, @Nullable AttributeSet attrs) { super(context, attrs); initView(); } public BottomTabLayout(Context context, @Nullable AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); initView(); } private void initView() { setOrientation(HORIZONTAL); } public void setCurrentTab(int i) { if (i < tabCount && i >= 0) { View view = getChildAt(i); onClick(view); } } public void initData(ArrayList tabs, OnTabClickListener listener) { this.tabs = tabs; this.listener = listener; LayoutParams params = new LayoutParams(0, ViewGroup.LayoutParams.MATCH_PARENT); params.weight = 1; params.gravity = Gravity.CENTER; if (tabs != null && tabs.size() > 0) { tabCount = tabs.size(); TabView mTabView = null; for (int i = 0, len = tabs.size(); i < len; i++) { mTabView = new TabView(getContext()); mTabView.setTag(tabs.get(i)); mTabView.initData(tabs.get(i)); mTabView.setOnClickListener(this); addView(mTabView, params); } } else { throw new IllegalArgumentException("tabs can not be empty"); } } @Override public void onClick(View view) { if (selectedView != view) { listener.onTabClick((TabItem) view.getTag()); view.setSelected(true); if (selectedView != null) { selectedView.setSelected(false); } selectedView = view; } } public interface OnTabClickListener { void onTabClick(TabItem tabItem); } }
Activity
public class MainActivity extends AppCompatActivity implements BottomTabLayout.OnTabClickListener { private BottomTabLayout tab_layout; private ArrayListtabs; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); setTitle("底部切換標(biāo)簽"); tab_layout = (BottomTabLayout) findViewById(R.id.tab_layout); initBottomTab(); tab_layout.setCurrentTab(0); } private void initBottomTab() { tabs = new ArrayList<>(); tabs.add(new TabItem(R.drawable.selector_tab_msg, R.string.wechat, OneFragment.class)); tabs.add(new TabItem(R.drawable.selector_tab_contact, R.string.contacts, TwoFragment.class)); tabs.add(new TabItem(R.drawable.selector_tab_moments, R.string.discover, ThreeFragment.class)); tabs.add(new TabItem(R.drawable.selector_tab_profile, R.string.me, FourFragment.class)); tab_layout.initData(tabs, this); } private Fragment lastFragment; @Override public void onTabClick(TabItem tabItem) { try { Fragment tmpFragment = getSupportFragmentManager().findFragmentByTag(tabItem.tagFragmentClz.getSimpleName()); FragmentTransaction transaction = getSupportFragmentManager().beginTransaction(); if (tmpFragment == null) { tmpFragment = tabItem.tagFragmentClz.newInstance(); transaction.add(R.id.fl_container, tmpFragment, tabItem.tagFragmentClz.getSimpleName()); if (lastFragment != null) { transaction.hide(lastFragment); } transaction.commitAllowingStateLoss(); } else { transaction.show(tmpFragment); if (lastFragment != null) { transaction.hide(lastFragment); } transaction.commitAllowingStateLoss(); } lastFragment = tmpFragment; } catch (Exception e) { e.printStackTrace(); } } }
布局文件
以上是“怎么使用Android實(shí)現(xiàn)底部切換標(biāo)簽”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對(duì)大家有幫助,更多相關(guān)知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!