Android实现底部切换标签

 更新时间:2018年07月06日 11:22:15   作者:qq_14876133  
这篇文章主要为大家详细介绍了Android实现底部切换标签,嵌套Fragment,方便自定义布局,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了Android实现底部切换标签的具体代码,供大家参考,具体内容如下

实现底部通用切换标签 ,嵌套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底部切换标签

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

  • android Matrix实现图片随意放大缩小或拖动

    android Matrix实现图片随意放大缩小或拖动

    这篇文章主要为大家详细介绍了android Matrix实现图片随意放大缩小或拖动效果,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-02-02
  • 基于Android SDK-在64位Linux中使用需要注意的问题

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

    本篇文章是对Android SDK-在64位Linux中使用需要注意的问题进行了详细的分析介绍,需要的朋友参考下
    2013-05-05
  • Glide实现加载图片显示进度条效果

    Glide实现加载图片显示进度条效果

    Glide作为安卓开发常用的图片加载库,有许多实用而且强大的功能,那么,下面这篇文章主要给大家介绍了利用Glide实现加载图片显示进度条效果的相关资料,文中给出了详细的示例代码供大家参考学习,需要的朋友们下来一起看看吧。
    2017-05-05
  • Android不压缩图片实现高清加载巨图实例

    Android不压缩图片实现高清加载巨图实例

    这篇文章主要为大家介绍了Android不压缩图片实现高清加载巨图实例,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-06-06
  • Android Handle原理(Looper,Handler和Message)三者关系案例详解

    Android Handle原理(Looper,Handler和Message)三者关系案例详解

    这篇文章主要介绍了Android Handle原理(Looper,Handler和Message三者关系案例详解,本篇文章通过简要的案例,讲解了该项技术的了解与使用,以下就是详细内容,需要的朋友可以参考下
    2021-08-08
  • Android使用硬件加速的方式

    Android使用硬件加速的方式

    硬件加速是指利用设备的硬件资源来加速图形渲染和图像处理等操作,以提高应用程序的性能和用户体验,Android使用硬件加速的目的是为了提高图形渲染的性能和效果,本文给大家详细介绍了Android如何使用硬件加速,需要的朋友可以参考下
    2023-10-10
  • 安卓逆向案例分析之蝉妈妈sign破解

    安卓逆向案例分析之蝉妈妈sign破解

    这篇文章主要为大家介绍了安卓逆向案例分析蝉妈妈sign破解的方式讲解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步
    2022-02-02
  • 浅析安卓(Android)的性能优化

    浅析安卓(Android)的性能优化

    性能优化是一个大的范畴,如果有人问你在Android中如何做性能优化的,也许都不知道从哪开始说起。那么这篇文章我们从布局优化和内存优化两个方面来展开说如何进行Android的性能优化。
    2016-08-08
  • Android 自定义View实现多节点进度条功能

    Android 自定义View实现多节点进度条功能

    这篇文章主要介绍了Android 自定义View实现多节点进度条,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2020-05-05
  • ReactiveCocoa代码实践之-RAC网络请求重构

    ReactiveCocoa代码实践之-RAC网络请求重构

    这篇文章主要介绍了ReactiveCocoa代码实践之-RAC网络请求重构 的相关资料,需要的朋友可以参考下
    2016-04-04

最新评论