gpt4 book ai didi

Android 滚动到标签

转载 作者:行者123 更新时间:2023-11-29 22:14:09 25 4
gpt4 key购买 nike

EDIT2:我会说得更清楚,以便每个人都能理解我的问题。我希望更多的人能帮助我!

我制作的是一个应用程序,其中包含 fragment 和用于在 fragment 之间切换的选项卡。我还可以使用 ViewPager 在 fragment 之间滚动/滑动。

现在唯一的问题是选项卡没有随 fragment 一起滚动。所以当我滑到最后一个 fragment 时,我看不到所选的选项卡,因为它超出了查看范围。

如何在 fragment 之间滑动时滚动到选项卡?

我想我必须对这部分代码做点什么:

public void onPageSelected(int position) {
// TODO Auto-generated method stub
this.mTabHost.setCurrentTab(position);
}

但我没有任何线索,而且我尝试的一切都不起作用。为了更容易,这里有代码的两个重要部分。我希望有人能帮助我!!

我的主要代码:

public class MainActivity extends FragmentActivity implements TabHost.OnTabChangeListener, ViewPager.OnPageChangeListener {

private TabHost mTabHost;
private ViewPager mViewPager;
private HashMap<String, TabInfo> mapTabInfo = new HashMap<String, MainActivity.TabInfo>();
private PagerAdapter mPagerAdapter;

private class TabInfo {
private String tag;
TabInfo(String tag, Class<?> clazz, Bundle args) {
this.tag = tag;
}
}

class TabFactory implements TabContentFactory {
private final Context mContext;

public TabFactory(Context context) {
mContext = context;
}

public View createTabContent(String tag) {
View v = new View(mContext);
v.setMinimumWidth(0);
v.setMinimumHeight(0);
return v;
}
}

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tabs_layout);
this.initialiseTabHost(savedInstanceState);
if (savedInstanceState != null) {
mTabHost.setCurrentTabByTag(savedInstanceState.getString("tab"));
}
this.intialiseViewPager();
}

protected void onSaveInstanceState(Bundle outState) {
outState.putString("tab", mTabHost.getCurrentTabTag());
super.onSaveInstanceState(outState);
}

private void intialiseViewPager() {
List<Fragment> fragments = new Vector<Fragment>();
fragments.add(Fragment.instantiate(this, TabFragment1.class.getName()));
fragments.add(Fragment.instantiate(this, TabFragment2.class.getName()));
fragments.add(Fragment.instantiate(this, TabFragment3.class.getName()));
fragments.add(Fragment.instantiate(this, TabFragment4.class.getName()));
fragments.add(Fragment.instantiate(this, TabFragment5.class.getName()));
this.mPagerAdapter = new PagerAdapter(super.getSupportFragmentManager(), fragments);
//
this.mViewPager = (ViewPager)super.findViewById(R.id.viewpager);
this.mViewPager.setAdapter(this.mPagerAdapter);
this.mViewPager.setOnPageChangeListener(this);
}

private void initialiseTabHost(Bundle args) {
mTabHost = (TabHost)findViewById(android.R.id.tabhost);
mTabHost.setup();
TabInfo tabInfo = null;
MainActivity.AddTab(this, this.mTabHost, this.mTabHost.newTabSpec("Tab1").setIndicator("Home"), ( tabInfo = new TabInfo("Tab1", TabFragment1.class, args)));
this.mapTabInfo.put(tabInfo.tag, tabInfo);
MainActivity.AddTab(this, this.mTabHost, this.mTabHost.newTabSpec("Tab2").setIndicator("Messages"), ( tabInfo = new TabInfo("Tab2", TabFragment2.class, args)));
this.mapTabInfo.put(tabInfo.tag, tabInfo);
MainActivity.AddTab(this, this.mTabHost, this.mTabHost.newTabSpec("Tab3").setIndicator("Search"), ( tabInfo = new TabInfo("Tab3", TabFragment3.class, args)));
this.mapTabInfo.put(tabInfo.tag, tabInfo);
MainActivity.AddTab(this, this.mTabHost, this.mTabHost.newTabSpec("Tab4").setIndicator("Profile"), ( tabInfo = new TabInfo("Tab4", TabFragment4.class, args)));
this.mapTabInfo.put(tabInfo.tag, tabInfo);
MainActivity.AddTab(this, this.mTabHost, this.mTabHost.newTabSpec("Tab5").setIndicator("Settings"), ( tabInfo = new TabInfo("Tab5", TabFragment5.class, args)));
this.mapTabInfo.put(tabInfo.tag, tabInfo);
mTabHost.setOnTabChangedListener(this);
}

private static void AddTab(MainActivity activity, TabHost tabHost, TabHost.TabSpec tabSpec, TabInfo tabInfo) {
tabSpec.setContent(activity.new TabFactory(activity));
tabHost.addTab(tabSpec);
}

public void onTabChanged(String tag) {
int pos = this.mTabHost.getCurrentTab();
this.mViewPager.setCurrentItem(pos);
}

public void onPageScrolled(int position, float positionOffset,
int positionOffsetPixels) {
// TODO Auto-generated method stub
}

public void onPageSelected(int position) {
// TODO Auto-generated method stub
this.mTabHost.setCurrentTab(position);
}

public void onPageScrollStateChanged(int state) {
// TODO Auto-generated method stub
}
}

到目前为止我所做的导致标签消失并在我点击它们时重新出现:

public void onPageSelected(int position) {
// TODO Auto-generated method stub
this.mTabHost.setCurrentTab(position);

this.hScrollView = (HorizontalScrollView)super.findViewById(R.id.scroller);
this.hScrollView.scrollTo(((int)(mTabHost.getWidth() * (position / ((double)(mTabHost.getChildCount() - 1))))), 0);
}

最佳答案

我不知道答案,但是 this guy为你正在做的事情实现了一个开源库。希望对您有所帮助!

关于Android 滚动到标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8881553/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com