gpt4 book ai didi

android - 带有选项卡的 Viewpager 下方的静态按钮 - Actionbarsherlock

转载 作者:行者123 更新时间:2023-11-30 03:51:20 26 4
gpt4 key购买 nike

所以我正在尝试创建一个布局,以便在页面顶部有一个带选项卡的 viewpager,底部有几个静态按钮,当我在选项卡上滑动时它们不会移动。虽然似乎遇到了障碍,但我的 fragment 没有加载,并且在我实现以下代码时失去了滑动功能。我是 Actionbarsherlock 的新手,不确定问题出在哪里,希望得到任何帮助。谢谢!

java:

public class Main extends SherlockFragmentActivity {

ViewPager mViewPager;
TabsAdapter mTabsAdapter;
TextView tabCenter;
TextView tabText;
Menu menu;
SubMenu subMenu1;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

mViewPager = new ViewPager(this);
mViewPager.setId(R.id.pager);

ActionBar bar = getSupportActionBar();
bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

mTabsAdapter = new TabsAdapter(this, mViewPager);

mTabsAdapter
.addTab(bar.newTab().setText("Tab 1"), Fragment1.class, null);
mTabsAdapter.addTab(bar.newTab().setText("Tab 2"), Fragment2.class,
null);
mTabsAdapter.addTab(bar.newTab().setText("Tab 3"), Fragment3.class,
null);

}

public static class TabsAdapter extends FragmentPagerAdapter implements
ActionBar.TabListener, ViewPager.OnPageChangeListener {
private final Context mContext;
private final ActionBar mActionBar;
private final ViewPager mViewPager;
private final ArrayList<TabInfo> mTabs = new ArrayList<TabInfo>();

static final class TabInfo {
private final Class<?> clss;
private final Bundle args;

TabInfo(Class<?> _class, Bundle _args) {
clss = _class;
args = _args;
}
}

public TabsAdapter(SherlockFragmentActivity activity, ViewPager pager) {
super(activity.getSupportFragmentManager());
mContext = activity;
mActionBar = activity.getSupportActionBar();
mViewPager = pager;
mViewPager.setAdapter(this);
mViewPager.setOnPageChangeListener(this);
}

public void addTab(ActionBar.Tab tab, Class<?> clss, Bundle args) {
TabInfo info = new TabInfo(clss, args);
tab.setTag(info);
tab.setTabListener(this);
mTabs.add(info);
mActionBar.addTab(tab);
notifyDataSetChanged();
}

@Override
public int getCount() {
return mTabs.size();
}

@Override
public Fragment getItem(int position) {
TabInfo info = mTabs.get(position);
return Fragment.instantiate(mContext, info.clss.getName(),
info.args);
}

public void onPageScrolled(int position, float positionOffset,
int positionOffsetPixels) {
}

public void onPageSelected(int position) {
mActionBar.setSelectedNavigationItem(position);

}

public void onPageScrollStateChanged(int state) {
}

public void onTabSelected(Tab tab, FragmentTransaction ft) {
Object tag = tab.getTag();
for (int i = 0; i < mTabs.size(); i++) {
if (mTabs.get(i) == tag) {
mViewPager.setCurrentItem(i);
}
}
}

public void onTabUnselected(Tab tab, FragmentTransaction ft) {
}

public void onTabReselected(Tab tab, FragmentTransaction ft) {
}
}
}

XML:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal"
android:orientation="vertical"
android:padding="4dip" >

<android.support.v4.view.ViewPager
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="0px"
android:layout_weight="1" >
</android.support.v4.view.ViewPager>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0"
android:gravity="center"
android:measureWithLargestChild="true"
android:orientation="horizontal" >

<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Example" >
</Button>
</LinearLayout>

</LinearLayout>

最佳答案

查看以下链接。这里定义了实现您需要的功能的方法。

1) Link 1
2) Link 2
3) Link 3
4) Link 4
5) Link 5

希望对您有所帮助。

关于android - 带有选项卡的 Viewpager 下方的静态按钮 - Actionbarsherlock,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14107728/

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