国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

Android實現底部切換標簽

2019-12-12 00:44:06
字體:
來源:轉載
供稿:網友

本文實例為大家分享了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底部切換標簽

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持武林網。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 阳高县| 田阳县| 海城市| 垫江县| 洪江市| 哈巴河县| 大庆市| 博罗县| 怀宁县| 沁源县| 长海县| 中西区| 石屏县| 开鲁县| 界首市| 曲周县| 巴青县| 澜沧| 长武县| 广水市| 泽州县| 石狮市| 陇南市| 抚松县| 桃源县| 西乌| 林西县| 灵璧县| 容城县| 浑源县| 宁强县| 米脂县| 济南市| 长丰县| 北京市| 阿坝县| 乌拉特中旗| 丹东市| 澜沧| 都江堰市| 乐清市|