欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android實現(xiàn)底部切換標簽

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

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

實現(xiàn)底部通用切換標簽 ,嵌套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>

定義單個標簽

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

定義單個標簽的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;
  }
}

定義底部切換標簽控件

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("底部切換標簽");

    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底部切換標簽

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

相關文章

  • Android自定義View實現(xiàn)QQ運動積分轉(zhuǎn)盤抽獎功能

    Android自定義View實現(xiàn)QQ運動積分轉(zhuǎn)盤抽獎功能

    這篇文章主要為大家詳細介紹了Android自定義View實現(xiàn)QQ運動積分轉(zhuǎn)盤抽獎功能,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-10-10
  • Android 各版本兼容性適配詳解

    Android 各版本兼容性適配詳解

    這篇文章主要為大家介紹了Android 各版本兼容性適配詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-12-12
  • Android編程實現(xiàn)圖標拖動效果的方法

    Android編程實現(xiàn)圖標拖動效果的方法

    這篇文章主要介紹了Android編程實現(xiàn)圖標拖動效果的方法,涉及Android事件響應及圖標變換的相關技巧,具有一定參考借鑒價值,需要的朋友可以參考下
    2015-11-11
  • Android實現(xiàn)文本排版

    Android實現(xiàn)文本排版

    這篇文章主要介紹了Android實現(xiàn)文本排版,對多行文本進行排版布局,每一行的內(nèi)容又分為兩部分,左邊為標題,右邊為描述,左邊內(nèi)容長度不確定,右邊的內(nèi)容需要對齊,需要的朋友可以參考下
    2016-04-04
  • Android自定義通用標題欄CustomTitleBar

    Android自定義通用標題欄CustomTitleBar

    這篇文章主要為大家詳細介紹了Android自定義通用標題欄CustomTitleBar,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-11-11
  • Android 編輯頭像功能簡單實現(xiàn)實例(圖片選取,裁剪)

    Android 編輯頭像功能簡單實現(xiàn)實例(圖片選取,裁剪)

    這篇文章主要介紹了Android 編輯頭像功能簡單實現(xiàn)實例(圖片選取,裁剪),非常具有實用價值,需要的朋友可以參考下
    2017-06-06
  • android自定義view實現(xiàn)推箱子小游戲

    android自定義view實現(xiàn)推箱子小游戲

    這篇文章主要為大家詳細介紹了android自定義view實現(xiàn)推箱子小游戲,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-04-04
  • Kotlin中內(nèi)置函數(shù)的用法和區(qū)別總結

    Kotlin中內(nèi)置函數(shù)的用法和區(qū)別總結

    眾所周知相比Java, Kotlin提供了不少高級語法特性。對于一個Kotlin的初學者來說經(jīng)常會寫出一些不夠優(yōu)雅的代碼。下面這篇文章主要給大家介紹了關于Kotlin中內(nèi)置函數(shù)的用法和區(qū)別的相關資料,需要的朋友可以參考下
    2018-06-06
  • Android 仿淘寶、京東商品詳情頁向上拖動查看圖文詳情控件DEMO詳解

    Android 仿淘寶、京東商品詳情頁向上拖動查看圖文詳情控件DEMO詳解

    本文給大家介紹android 仿淘寶、京東商品詳情頁向上拖動查看圖文詳情控件DEMO詳解,使用兩個scrollView,兩個scrollView 豎直排列,通過自定義viewGroup來控制兩個scrollView的豎直排列,以及滑動事件的處理。對android 拖動查看圖文詳情知識感興趣的朋友一起學習吧
    2016-09-09
  • android 下載時文件名是中文和空格會報錯解決方案

    android 下載時文件名是中文和空格會報錯解決方案

    項目中遇到了下載文件文件名是中文而且還有空格如果不對連接進行處理下載就會報錯要想解決這個問題只需對你的url進行編碼然后替換空格用編碼表示,感興趣的朋友可以詳細了解下
    2013-01-01

最新評論