使用TabHost点击第一次无法获得数据的问题,自定义TabHost。

来源:互联网 发布:平价凉鞋推荐知乎 编辑:程序博客网 时间:2024/06/06 20:15
刚开始使用TabHost时,第一次点击老是无法获取数据,(其实tabhost 点击每个tab 数据没有及时更新,

可以将onCreate()里面的获取数据的方法写到onResume()方法里面,

因为每次点击tab之后,只有第一次会执行onCreate,

但是每次都会执行onResume里面的方法

原生的TabHost的使用()

@Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.tabhost_activity_main);        //找到底部控件        mTabHost = (FragmentTabHost) findViewById(R.id.tabhost);        //初始化        mTabHost.setup(MainActivity.this, getSupportFragmentManager(), android.R.id.tabcontent);        //改变图片drawable资源        int[] img = {                R.drawable.tabhosthimgcolor_dotplayer,                R.drawable.tabhosthimgcolor_liveplayer,                R.drawable.tabhosthimgcolor_myinfoplayer        };        //文本        String[] tabhosttext = getResources().getStringArray(R.array.tabhosttext);        for (int i = 0; i < tabhosttext.length; i++) {            TabHost.TabSpec one = mTabHost.newTabSpec(String.valueOf(i));            one.setIndicator(getView(i, img, tabhosttext));            if (i == 0) {                //点播fr                mTabHost.addTab(one, DotPlayerFragment.class, null);            } else if (i == 1) {                //直播fr                mTabHost.addTab(one, LivePlayerFragment.class, null);            } else if (i == 2) {                //我的                mTabHost.addTab(one, MyInformationFragment.class, null);            }        }        mTabHost.setOnTabChangedListener(new TabHost.OnTabChangeListener() {            //每次变化的标签            @Override            public void onTabChanged(String tabId) {                //tabId是mTabHost.newTabSpec("1");里面的值(1)               // Toast.makeText(MainActivity.this,"切换了",Toast.LENGTH_SHORT).show();            }        });        //设置TabHost跳转到那个fragment        mTabHost.setCurrentTabByTag("0");    }    public View getView(int index, int[] img, String[] tabhosttext) {        LayoutInflater inflater = LayoutInflater.from(this);        View view = inflater.inflate(R.layout.tabhost_item_view, null);        //动态获取图片,文本        ImageView icon = (ImageView) view.findViewById(R.id.tabhostitemimg);        icon.setImageResource(img[index]);        TextView tx = (TextView) view.findViewById(R.id.tabhostitemtext);        tx.setText(tabhosttext[index]);        return view;    }

原生的xml布局:

<FrameLayout        android:id="@android:id/tabcontent"        android:layout_width="match_parent"        android:layout_height="0dip"        android:layout_weight="1" />    <android.support.v4.app.FragmentTabHost        android:id="@+id/tabhost"        android:layout_width="match_parent"        android:layout_height="wrap_content"        >        <LinearLayout            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:orientation="vertical"            >            <FrameLayout                android:id="@android:id/tabcontent"                android:layout_width="0dp"                android:layout_height="0dp"                android:layout_weight="0" />        </LinearLayout>    </android.support.v4.app.FragmentTabHost>




第二种就是自定义TabHost来解决

我直接放出我自定的一个TabHost的类,想要什么特效自己可以改。

public class BottomBar extends LinearLayout implements View.OnClickListener {    private ImageView mMainIv;    private TextView mMainTv;    private ImageView mCategoryIv;    private TextView mCategoryTv;    private ImageView mShopcarIv;    private TextView mShopcarTv;    private IBottomBarItemClickListener mListener;    private int mCurrentTabId = -1;    public void setIBottomBarItemClickListener(IBottomBarItemClickListener listener) {        mListener = listener;    }    public BottomBar(Context context, AttributeSet attrs) {        super(context, attrs);    }    /****     * 当布局加载完毕后才能获取子控件     ****/    @Override    protected void onFinishInflate() {        super.onFinishInflate();        initView();        //模拟被点击        findViewById(R.id.dot_main_ll).performClick();    }    private void initView() {        findViewById(R.id.dot_main_ll).setOnClickListener(this);        findViewById(R.id.live_find_ll).setOnClickListener(this);        findViewById(R.id.my_adv_ll).setOnClickListener(this);        mMainIv = (ImageView) findViewById(R.id.dot_main_iv);        mMainTv = (TextView) findViewById(R.id.dot_main);        mCategoryIv = (ImageView) findViewById(R.id.live_find_iv);        mCategoryTv = (TextView) findViewById(R.id.live_find);        mShopcarIv = (ImageView) findViewById(R.id.my_adv_iv);        mShopcarTv = (TextView) findViewById(R.id.my_adv);    }    @Override    public void onClick(View v) {        if (mCurrentTabId == v.getId()) {            return;        }        mCurrentTabId = v.getId();        defaultIndicator();        if (mListener != null) {            mListener.onItemClicked(v.getId());        }        switch (v.getId()) {            case R.id.dot_main_ll:                mMainIv.setSelected(true);                mMainTv.setSelected(true);                break;            case R.id.live_find_ll:                mCategoryIv.setSelected(true);                mCategoryTv.setSelected(true);                break;            case R.id.my_adv_ll:                mShopcarIv.setSelected(true);                mShopcarTv.setSelected(true);                break;        }    }    /****     * 初始化指示器     ****/    private void defaultIndicator() {        //默认设置为不选中状态        mMainIv.setSelected(false);        mMainTv.setSelected(false);        mCategoryIv.setSelected(false);        mCategoryTv.setSelected(false);        mShopcarIv.setSelected(false);        mShopcarTv.setSelected(false);    }}
TabHost的布局xml

<?xml version="1.0" encoding="utf-8"?><com.a520it.testtabhost.view.BottomBar    android:background="#FFF"    xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="55dp"    android:orientation="horizontal"  >    <LinearLayout        android:id="@+id/dot_main_ll"        style="@style/bottom_ll_style" >        <ImageView            android:id="@+id/dot_main_iv"            android:layout_width="30dp"            android:layout_height="30dp"            android:src="@drawable/tabhosthimgcolor_dotplayer" />        <TextView            android:id="@+id/dot_main"            style="@style/bottom_text_style"            android:text="点播" />    </LinearLayout>    <LinearLayout        android:id="@+id/live_find_ll"        style="@style/bottom_ll_style" >        <ImageView            android:id="@+id/live_find_iv"            android:layout_width="30dp"            android:layout_height="30dp"            android:src="@drawable/tabhosthimgcolor_liveplayer" />        <TextView            android:id="@+id/live_find"            style="@style/bottom_text_style"            android:text="直播" />    </LinearLayout>    <LinearLayout        android:id="@+id/my_adv_ll"        style="@style/bottom_ll_style" >        <ImageView            android:id="@+id/my_adv_iv"            android:layout_width="30dp"            android:layout_height="30dp"            android:src="@drawable/tabhosthimgcolor_myinfoplayer" />        <TextView            android:id="@+id/my_adv"            style="@style/bottom_text_style"            android:text="我的" />    </LinearLayout></com.a520it.testtabhost.view.BottomBar>


TabHost的控件的布局效果:



主界面的xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:background="@android:color/white"    android:orientation="vertical">    <!--<FrameLayout        android:id="@android:id/tabcontent"        android:layout_width="match_parent"        android:layout_height="0dip"        android:layout_weight="1" />    <android.support.v4.app.FragmentTabHost        android:id="@+id/tabhost"        android:layout_width="match_parent"        android:layout_height="wrap_content"        >        <LinearLayout            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:orientation="vertical"            >            <FrameLayout                android:id="@android:id/tabcontent"                android:layout_width="0dp"                android:layout_height="0dp"                android:layout_weight="0" />        </LinearLayout>    </android.support.v4.app.FragmentTabHost>-->    <FrameLayout        android:id="@+id/top_bar"        android:layout_width="match_parent"        android:layout_height="0dp"        android:layout_weight="1" />    <View        android:layout_width="match_parent"        android:layout_height="2dp"        android:background="#BB1F35" />    <include        android:id="@+id/bottom_bar"        layout="@layout/bottom_bar" /></LinearLayout>


主界面的效果图:



第三种简单暴力,直接去gethub里面搜索开源的项目(特效强)。

原创粉丝点击