最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Android實(shí)現(xiàn)底部切換標(biāo)簽

 更新時(shí)間:2018年07月06日 11:22:15   作者:qq_14876133  
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)底部切換標(biāo)簽,嵌套Fragment,方便自定義布局,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了Android實(shí)現(xiàn)底部切換標(biāo)簽的具體代碼,供大家參考,具體內(nèi)容如下

實(shí)現(xiàn)底部通用切換標(biāo)簽 ,嵌套Fragment,方便自定義布局

自定義控件:

widget_tab_view.xml

<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="match_parent">

  <ImageView
    android:id="@+id/tab_image"
    android:layout_width="20dp"
    android:layout_height="20dp" />

  <TextView
    android:id="@+id/tab_label"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textColor="#666666"
    android:textSize="12sp" />
</merge>

定義單個(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 ArrayList<TabItem> tabs;
  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<TabItem> 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 ArrayList<TabItem> tabs;

  @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();
    }
  }
}

布局文件

<?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"
  xmlns:tools="http://schemas.android.com/tools"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:orientation="vertical"
  tools:context="com.sample.bottomtab.MainActivity">

  <FrameLayout
    android:id="@+id/fl_container"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1"
    android:background="#ffffff" />

  <View
    android:layout_width="match_parent"
    android:layout_height="1dp"
    android:background="#dcdcdc" />

  <com.sample.bottomtab.widget.bottomtab.BottomTabLayout
    android:id="@+id/tab_layout"
    android:layout_width="match_parent"
    android:layout_height="48dp"
    android:background="#ffffff" />
</LinearLayout>

代碼下載:Android底部切換標(biāo)簽

以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • android Matrix實(shí)現(xiàn)圖片隨意放大縮小或拖動(dòng)

    android Matrix實(shí)現(xiàn)圖片隨意放大縮小或拖動(dòng)

    這篇文章主要為大家詳細(xì)介紹了android Matrix實(shí)現(xiàn)圖片隨意放大縮小或拖動(dòng)效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-02-02
  • 基于Android SDK-在64位Linux中使用需要注意的問題

    基于Android SDK-在64位Linux中使用需要注意的問題

    本篇文章是對(duì)Android SDK-在64位Linux中使用需要注意的問題進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下
    2013-05-05
  • Glide實(shí)現(xiàn)加載圖片顯示進(jìn)度條效果

    Glide實(shí)現(xiàn)加載圖片顯示進(jìn)度條效果

    Glide作為安卓開發(fā)常用的圖片加載庫,有許多實(shí)用而且強(qiáng)大的功能,那么,下面這篇文章主要給大家介紹了利用Glide實(shí)現(xiàn)加載圖片顯示進(jìn)度條效果的相關(guān)資料,文中給出了詳細(xì)的示例代碼供大家參考學(xué)習(xí),需要的朋友們下來一起看看吧。
    2017-05-05
  • Android不壓縮圖片實(shí)現(xiàn)高清加載巨圖實(shí)例

    Android不壓縮圖片實(shí)現(xiàn)高清加載巨圖實(shí)例

    這篇文章主要為大家介紹了Android不壓縮圖片實(shí)現(xiàn)高清加載巨圖實(shí)例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-06-06
  • Android Handle原理(Looper,Handler和Message)三者關(guān)系案例詳解

    Android Handle原理(Looper,Handler和Message)三者關(guān)系案例詳解

    這篇文章主要介紹了Android Handle原理(Looper,Handler和Message三者關(guān)系案例詳解,本篇文章通過簡要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下
    2021-08-08
  • Android使用硬件加速的方式

    Android使用硬件加速的方式

    硬件加速是指利用設(shè)備的硬件資源來加速圖形渲染和圖像處理等操作,以提高應(yīng)用程序的性能和用戶體驗(yàn),Android使用硬件加速的目的是為了提高圖形渲染的性能和效果,本文給大家詳細(xì)介紹了Android如何使用硬件加速,需要的朋友可以參考下
    2023-10-10
  • 安卓逆向案例分析之蟬媽媽sign破解

    安卓逆向案例分析之蟬媽媽sign破解

    這篇文章主要為大家介紹了安卓逆向案例分析蟬媽媽sign破解的方式講解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步
    2022-02-02
  • 淺析安卓(Android)的性能優(yōu)化

    淺析安卓(Android)的性能優(yōu)化

    性能優(yōu)化是一個(gè)大的范疇,如果有人問你在Android中如何做性能優(yōu)化的,也許都不知道從哪開始說起。那么這篇文章我們從布局優(yōu)化和內(nèi)存優(yōu)化兩個(gè)方面來展開說如何進(jìn)行Android的性能優(yōu)化。
    2016-08-08
  • Android 自定義View實(shí)現(xiàn)多節(jié)點(diǎn)進(jìn)度條功能

    Android 自定義View實(shí)現(xiàn)多節(jié)點(diǎn)進(jìn)度條功能

    這篇文章主要介紹了Android 自定義View實(shí)現(xiàn)多節(jié)點(diǎn)進(jìn)度條,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-05-05
  • ReactiveCocoa代碼實(shí)踐之-RAC網(wǎng)絡(luò)請(qǐng)求重構(gòu)

    ReactiveCocoa代碼實(shí)踐之-RAC網(wǎng)絡(luò)請(qǐng)求重構(gòu)

    這篇文章主要介紹了ReactiveCocoa代碼實(shí)踐之-RAC網(wǎng)絡(luò)請(qǐng)求重構(gòu) 的相關(guān)資料,需要的朋友可以參考下
    2016-04-04

最新評(píng)論

塔河县| 根河市| 大港区| 兴国县| 临潭县| 常熟市| 浮山县| 彭泽县| 禹城市| 阳曲县| 泾阳县| 凌云县| 濮阳市| 沙河市| 永善县| 澄迈县| 嘉禾县| 辛集市| 冷水江市| 金山区| 和田市| 蓬溪县| 泰和县| 杭锦旗| 宜州市| 佛学| 西昌市| 郎溪县| 镇远县| 徐闻县| 合阳县| 县级市| 望都县| 隆回县| 长春市| 耒阳市| 黔西| 郧西县| 酉阳| 文昌市| 甘谷县|