0
我想在我的應用中實現此功能,如下所示。
- 活動與操作欄和行動項目
- 標籤下方的動作條
- 每個選項卡中都會有內容。
我用Tab分段和TabListeners來編碼。現在,當我運行應用程序時,我正在獲取選項卡視圖,但查看傳呼機和指示器都缺失。我正在獲得如下所示的活動。
我的代碼如下所示。
TabViewActivity的.java
public class TabViewActivity extends SherlockFragmentActivity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
copyPrivateRawResuorceToPubliclyAccessibleFile();
ActionBar actionBar = getSupportActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
// add tabs
Tab tab1 = actionBar
.newTab()
.setText("Details")
.setTabListener(
new TabListener<TabFragment>(this, "tab1",
TabFragment.class));
actionBar.addTab(tab1);
Tab tab2 = actionBar
.newTab()
.setText("Maps")
.setTabListener(
new TabListener<TabFragment>(this, "tab2",
TabFragment.class));
actionBar.addTab(tab2);
// check if there is a saved state to select active tab
if (savedInstanceState != null) {
getSupportActionBar().setSelectedNavigationItem(
savedInstanceState.getInt(ACTIVE_TAB));
}
}
TabListener.java
public class TabListener<T extends SherlockFragment> implements
ActionBar.TabListener {
private SherlockFragment mFragment;
private final SherlockFragmentActivity mActivity;
private final String mTag;
private final Class<T> mClass;
public TabListener(Activity activity, String tag, Class<T> clz) {
mActivity = (SherlockFragmentActivity) activity;
mTag = tag;
mClass = clz;
}
public void onTabSelected(Tab tab, FragmentTransaction ft) {
// Check if the fragment has already been initialised
SherlockFragment preInitializedFragment = (SherlockFragment) mActivity
.getSupportFragmentManager().findFragmentByTag(mTag);
if (mFragment != null) {
// If it exists, simply attach it in order to show it
ft.attach(mFragment);
} else if (preInitializedFragment != null) {
mFragment = preInitializedFragment;
ft.attach(mFragment);
} else {
// Not found, so instantiate and add it to the activity
mFragment = (SherlockFragment) SherlockFragment.instantiate(
mActivity, mClass.getName());
ft.add(android.R.id.content, mFragment, mTag);
}
}
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
if (mFragment != null) {
// Detach the fragment, because another one is being attached
ft.detach(mFragment);
}
}
public void onTabReselected(Tab tab, FragmentTransaction ft) {
// User selected the already selected tab. Usually do nothing.
}
TabFragment.java
public class TabFragment extends SherlockFragment {
// your member variables here
@Override
public View onCreateView(LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.main, container, false);
// do your view initialization here
return view;
}
}
我會爲每個選項卡中單擊命名details.java和地圖兩個不同的類。 java的。 請幫我把這段代碼放在哪裏。 我也實現了視圖尋呼機。這是我的查看傳呼機代碼的詳細信息選項卡。
DetailsActivity.java
public class DetailsActivity extends BaseSampleActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.place_details_layout);
mAdapter = new TestFragmentAdapter(getSupportFragmentManager());
mPager = (ViewPager)findViewById(R.id.pager);
mPager.setAdapter(mAdapter);
mIndicator = (CirclePageIndicator)findViewById(R.id.indicator);
mIndicator.setViewPager(mPager);
}
}
BaseSampleActivity.java
public abstract class BaseSampleActivity extends FragmentActivity {
private static final Random RANDOM = new Random();
TestFragmentAdapter mAdapter;
ViewPager mPager;
PageIndicator mIndicator;
//FragmentTabHost mTabHost;
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.random:
final int page = RANDOM.nextInt(mAdapter.getCount());
Toast.makeText(this, "Changing to page " + page, Toast.LENGTH_SHORT);
mPager.setCurrentItem(page);
return true;
case R.id.add_page:
if (mAdapter.getCount() < 10) {
mAdapter.setCount(mAdapter.getCount() + 1);
mIndicator.notifyDataSetChanged();
}
return true;
case R.id.remove_page:
if (mAdapter.getCount() > 1) {
mAdapter.setCount(mAdapter.getCount() - 1);
mIndicator.notifyDataSetChanged();
}
return true;
}
return super.onOptionsItemSelected(item);
}
}
由於檢查出的示例代碼作爲答覆。我也實施了viewpager。我編輯了我的問題,並且可以找到視圖尋呼機代碼。請指導如何前進。 –