TabHost和ViewPager结合第一个页面不显示
============问题描述============
我使用TabHost和ViewPager想实现滑动切换的效果,但一打开程序,ViewPager的第一个页面是不会显示的(空白),只有点击了其他Tab,再点回第一个Tab,才会显示ViewPager的第一个页面,我在一开始已经用了mViewPager.setCurrentItem(0)指令,但还是无效,请哪位大侠指点一下!!!
package com.product.shiningbaby.fragment;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.TabHost;
import android.widget.TabHost.OnTabChangeListener;
import android.widget.TabWidget;
import android.widget.TextView;
import com.product.shiningbaby.R;
public class FragmentImage extends Fragment {
private TabHost mTabHost;
//private TabWidget mTabWidget;
private ViewPager mViewPager;
private PagerAdapter mPagerAdapter;
private String[] tags={"最热","最新","动态"};
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
View root= inflater.inflate(R.layout.fragment_image, container,false);
mViewPager=(ViewPager)root.findViewById(R.id.viewpager1);
mPagerAdapter=new SlidePagerAdapter(getFragmentManager());
mViewPager.setAdapter(mPagerAdapter);
mViewPager.setCurrentItem(0);
mTabHost=(TabHost)root.findViewById(android.R.id.tabhost);
mTabHost.setup();
mTabHost.addTab(mTabHost.newTabSpec("tab1").setIndicator(tags[0]).setContent(R.id.viewpager1));
mTabHost.addTab(mTabHost.newTabSpec("tab2").setIndicator(tags[1]).setContent(R.id.viewpager1));
mTabHost.addTab(mTabHost.newTabSpec("tab3").setIndicator(tags[2]).setContent(R.id.viewpager1));
mTabHost.setOnTabChangedListener(new OnTabChangeListener()
{
//第一次打开程序,TabChange时间不会发生,只有点击后才会发生
@Override
public void onTabChanged(String tabId)
{
mViewPager.setCurrentItem(mTabHost.getCurrentTab());
Log.d("myfather","currentTab is: "+mTabHost.getCurrentTab());
}
});
mViewPager.setOnPageChangeListener(new OnPageChangeListener()
{
@Override
public void onPageSelected(int index)
{
mTabHost.setCurrentTab(index);
}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2)
{
}
@Override
public void onPageScrollStateChanged(int arg0)
{
}
});
return root;
}
@Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
}
private class SlidePagerAdapter extends FragmentStatePagerAdapter
{
public SlidePagerAdapter(FragmentManager fm)
{
super(fm);
}
@Override
public Fragment getItem(int position)
{
return SlideFragment.create(tags[position]);
}
@Override
public int getCount()
{
return tags.length;
}
}
public static class SlideFragment extends Fragment
{
public static SlideFragment create(String tag)
{
SlideFragment f=new SlideFragment();
Bundle b=new Bundle();
b.putString("tag", tag);
f.setArguments(b);
return f;
}
@Override
public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState)
{
View v=inflater.inflate(R.layout.fragment_image_newest, container,false);
TextView t=(TextView)v.findViewById(R.id.text1);
t.setText(getArguments().getString("tag"));
return v;
}
}
}
============解决方案1============
这个确实不显示,找半天也没找到原因,最后的方法时先将其设到1然后到0
在你的代码中就变成
mViewPager.setCurrentItem(1);
mViewPager.setCurrentItem(0);
posted on 2014-10-22 00:03 android基础教程 阅读(564) 评论(0) 编辑 收藏 举报