fragment + tabhost使用

总结下几种常见的搭建主页面方式,有普通导航栏,中间凸出导航栏

1.fragment + tabhost

  • activity_main
<?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.fengyun.collection.MainActivity">

    <FrameLayout
        android:id="@+id/contentLayout"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"/>

    <android.support.v4.app.FragmentTabHost
        android:id="@android:id/tabhost"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/white">

        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="0dp"
            android:layout_height="0dp"/>
    </android.support.v4.app.FragmentTabHost>

</LinearLayout>
  • MainActivity
public class MainActivity extends AppCompatActivity implements TabHost.OnTabChangeListener {

    private FragmentTabHost tabHost;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        tabHost = (FragmentTabHost) super.findViewById(android.R.id.tabhost);
        tabHost.setup(this, super.getSupportFragmentManager(), R.id.contentLayout);
        tabHost.getTabWidget().setDividerDrawable(null);
        tabHost.setOnTabChangedListener(this);
        initTab();
    }


    private void initTab() {
        String tabs[] = TabDb.getTabsTxt();
        for (int i = 0; i < tabs.length; i++) {
            TabHost.TabSpec tabSpec = tabHost.newTabSpec(tabs[i]).setIndicator(getTabView(i));
            tabHost.addTab(tabSpec, TabDb.getFragments()[i], null);
            tabHost.setTag(i);
        }
    }

    private View getTabView(int idx) {
        View view = LayoutInflater.from(this).inflate(R.layout.footer_tabs, null);
        ((TextView) view.findViewById(R.id.tvTab)).setText(TabDb.getTabsTxt()[idx]);
        if (idx == 0) {
            ((TextView) view.findViewById(R.id.tvTab)).setTextColor(getResources().getColor(R.color.brown));
            ((ImageView) view.findViewById(R.id.ivImg)).setImageResource(TabDb.getTabsImgLight()[idx]);
        } else {
            ((ImageView) view.findViewById(R.id.ivImg)).setImageResource(TabDb.getTabsImg()[idx]);
        }
        return view;
    }

    @Override
    public void onTabChanged(String tabId) {
        updateTab();
    }

    private void updateTab() {
        TabWidget tabw = tabHost.getTabWidget();
        for (int i = 0; i < tabw.getChildCount(); i++) {
            View view = tabw.getChildAt(i);
            ImageView iv = (ImageView) view.findViewById(R.id.ivImg);
            if (i == tabHost.getCurrentTab()) {
                ((TextView) view.findViewById(R.id.tvTab)).setTextColor(getResources().getColor(R.color.brown));
                iv.setImageResource(TabDb.getTabsImgLight()[i]);
            } else {
                ((TextView) view.findViewById(R.id.tvTab)).setTextColor(getResources().getColor(R.color.tab_normal_color));
                iv.setImageResource(TabDb.getTabsImg()[i]);
            }
        }
    }

}
  • footer_tabs
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:padding="5dp">

    <ImageView
        android:id="@+id/ivImg"
        android:layout_centerHorizontal="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

    <TextView
        android:id="@+id/tvTab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/ivImg"
        android:layout_centerHorizontal="true"
        android:text=""
        android:textColor="@color/tab_text_color"
        android:textSize="12sp"/>
</RelativeLayout>
  • TabDb
public class TabDb {
    public static String[] getTabsTxt(){
        String[] tabs={"首页","社区","消息","我的"};
        return tabs;
    }
    public static int[] getTabsImg(){
        int[] ids={R.drawable.home_normal,R.drawable.jianding_normal,R.drawable.xiaoxi_normal,R.drawable.wode_normal};
        return ids;
    }
    public static int[] getTabsImgLight(){
        int[] ids={R.drawable.home_active,R.drawable.jianding_active,R.drawable.xiaoxi_active,R.drawable.wode_active};
        return ids;
    }
    public static Class[] getFragments(){
        Class[] clz={HomeFragment.class,CommunityFragment.class,MessageFragment.class,MineFragment.class};
        return clz;
    }
}

2.自己画

  • 看效果图,思路参考网上资料。
  • 这里写图片描述

    1. MainActivity
public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private RelativeLayout mRl_home;
    private RelativeLayout mRl_community;
    private RelativeLayout mRl_message;
    private RelativeLayout mRl_mine;

    private Fragment[] mFragments;
    private CustomViewPager mViewPager;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initData();
        initView();
    }

    private void initData() {
        mFragments = new Fragment[4];
        mFragments[0] = new HomeFragment();
        mFragments[1] = new CommunityFragment();
        mFragments[2] = new MessageFragment();
        mFragments[3] = new MineFragment();
    }

    private void initView() {
        mViewPager = (CustomViewPager) findViewById(R.id.viewpager);
        mRl_home = (RelativeLayout) findViewById(R.id.tab_home);
        mRl_community = (RelativeLayout) findViewById(R.id.tab_community);
        mRl_message = (RelativeLayout) findViewById(R.id.tab_message);
        mRl_mine = (RelativeLayout) findViewById(R.id.tab_mine);

        mRl_home.setOnClickListener(this);
        mRl_community.setOnClickListener(this);
        mRl_message.setOnClickListener(this);
        mRl_mine.setOnClickListener(this);

        mViewPager.setAdapter(new MyAdapter(getSupportFragmentManager()));
        mViewPager.setOffscreenPageLimit(4);

        tabSelected(mRl_home);
    }

    private class MyAdapter extends FragmentPagerAdapter {
        MyAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            return mFragments[position];
        }

        @Override
        public int getCount() {
            return mFragments.length;
        }
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tab_home:
                mViewPager.setCurrentItem(0);
                tabSelected(mRl_home);
                break;
            case R.id.tab_community:
                mViewPager.setCurrentItem(1);
                tabSelected(mRl_community);
                break;
            case R.id.tab_message:
                mViewPager.setCurrentItem(2);
                tabSelected(mRl_message);
                break;
            case R.id.tab_mine:
                mViewPager.setCurrentItem(3);
                tabSelected(mRl_mine);
                ((MineFragment)mFragments[3]).tabSelect();
                break;
            default:
                break;
        }
    }

    private void tabSelected(RelativeLayout relativeLayout) {
        mRl_home.setSelected(false);
        mRl_community.setSelected(false);
        mRl_message.setSelected(false);
        mRl_mine.setSelected(false);
        relativeLayout.setSelected(true);
    }
}
  • 2.activity_main
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:clipChildren="false"
    android:orientation="vertical"
    tools:context="com.fengyun.collection.ui.MainActivity">

    <com.collection.view.CustomViewPager
        android:id="@+id/viewpager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@+id/ll_bottom"/>

    <LinearLayout
        android:id="@+id/ll_bottom"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_alignParentBottom="true"
        android:orientation="horizontal">

        <RelativeLayout
            android:id="@+id/tab_home"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_gravity="bottom"
            android:layout_weight="1"
            android:gravity="center">

            <ImageView
                android:id="@+id/dd"
                android:layout_width="30dp"
                android:layout_height="30dp"
                android:layout_centerHorizontal="true"
                android:src="@drawable/home_selector"/>

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@+id/dd"
                android:layout_centerInParent="true"
                android:text="@string/home"
                android:textColor="@color/tab_text_color"
                android:textSize="10sp"/>

        </RelativeLayout>

        <RelativeLayout
            android:id="@+id/tab_community"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:gravity="center">

            <ImageView
                android:id="@+id/dd2"
                android:layout_width="30dp"
                android:layout_height="30dp"
                android:layout_centerHorizontal="true"
                android:src="@drawable/community_selector"/>

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@+id/dd2"
                android:layout_centerHorizontal="true"
                android:text="@string/community"
                android:textColor="@color/tab_text_color"
                android:textSize="10sp"/>

        </RelativeLayout>

        <RelativeLayout
            android:layout_width="80dp"
            android:layout_height="90dp"
            android:layout_gravity="bottom">

            <ImageView
                android:id="@+id/dd5"
                android:layout_width="50dp"
                android:layout_height="50dp"
                android:layout_centerInParent="true"
                android:src="@drawable/jia"/>

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@+id/dd5"
                android:layout_centerInParent="true"
                android:padding="2dp"
                android:text="@string/send"
                android:textSize="10sp"/>

        </RelativeLayout>

        <RelativeLayout
            android:id="@+id/tab_message"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:gravity="center">

            <ImageView
                android:id="@+id/dd3"
                android:layout_width="30dp"
                android:layout_height="30dp"
                android:layout_alignParentTop="true"
                android:layout_centerHorizontal="true"
                android:src="@drawable/xiaoxi_selector"/>

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@+id/dd3"
                android:layout_centerHorizontal="true"
                android:text="@string/message"
                android:textColor="@color/tab_text_color"
                android:textSize="10sp"/>

        </RelativeLayout>

        <RelativeLayout
            android:id="@+id/tab_mine"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:gravity="center">

            <ImageView
                android:id="@+id/dd4"
                android:layout_width="30dp"
                android:layout_height="30dp"
                android:layout_centerHorizontal="true"
                android:src="@drawable/wode_selector"/>

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@+id/dd4"
                android:layout_centerHorizontal="true"
                android:text="@string/mine"
                android:textColor="@color/tab_text_color"
                android:textSize="10sp"/>

        </RelativeLayout>

    </LinearLayout>

</RelativeLayout>

  • 3.icon selector
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/home_active" android:state_selected="true"/>
    <item android:drawable="@drawable/home_active" android:state_checked="true"/>
    <item android:drawable="@drawable/home_normal"/>

</selector>
  • 4.text selector
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:color="@color/brown" android:state_selected="true" />
    <item android:color="@color/tab_normal_color" />
</selector>
  • 5.viewpager
/**
 * 设置viewpager不可滑动 默认可以滑动
 */

public class CustomViewPager extends ViewPager {

    private boolean isScroll = false;

    public CustomViewPager(Context context) {
        super(context);
    }

    public CustomViewPager(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    /**
     * Sets scroll. true 可以滑动  false 不可以滑动
     *
     * @param isScroll the is scroll
     */
    public void setScroll(boolean isScroll) {
        this.isScroll = isScroll;
    }

    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        return isScroll && super.onTouchEvent(ev);
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        return isScroll && super.onInterceptTouchEvent(ev);
    }

    @Override
    public void setCurrentItem(int item) {
        setCurrentItem(item,false);
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值