【发布时间】:2016-02-23 09:41:01
【问题描述】:
我是 android 新手,在我的应用程序中使用选项卡,我已经成功实现了应用程序中的选项卡,但是我想让一个选项卡在单击后激活,它是一个自定义选项卡主机,我的代码如下,我已经尝试过许多链接用于使标签在 android 中处于活动状态但不起作用,请帮助我解决这个问题。
xml
<TabHost
android1:id="@android:id/tabhost"
android1:layout_width="match_parent"
android1:layout_height="match_parent"
android1:layout_alignParentBottom="true"
android1:layout_alignParentLeft="true"
android1:layout_alignParentRight="true"
android1:layout_alignParentTop="true" >
<RelativeLayout
android1:id="@+id/RelativeLayout1"
android1:layout_width="match_parent"
android1:layout_height="wrap_content"
android1:orientation="vertical" >
<TabWidget
android1:id="@android:id/tabs"
android1:layout_width="match_parent"
android1:layout_height="50dp"
android1:layout_alignParentLeft="true"
android1:layout_alignParentTop="true"
android1:background="@color/app_color" >
</TabWidget>
.
.
.
java
TabHost mTabHost;
mTabHost = (TabHost)getActivity().findViewById(android.R.id.tabhost);
mTabHost.setup();
mTabHost.setOnTabChangedListener(this);
setupTab(new TextView(getActivity()), "Popular");
setupTab(new TextView(getActivity()), "Recent");
mProgressBarLoading = (ProgressBar) getActivity().findViewById(com.company.stush.R.id.progressBarLoading);
// mTextViewNoItems = (TextView) getActivity().findViewById(R.id.textViewNoItems);
// mTextViewNoItems.setVisibility(View.GONE);
mPopularAdapter = new PopularAdapter(getActivity());
mGridViewPopular = (GridView) getActivity().findViewById(com.company.stush.R.id.gridViewPopular);
mGridViewPopular.setEmptyView(mProgressBarLoading);
mGridViewPopular.setAdapter(mPopularAdapter);
mGridViewPopular.setOnItemClickListener(this);
mGridViewRecent = (GridView) getActivity().findViewById(com.company.stush.R.id.gridViewRecent);
mGridViewRecent.setEmptyView(mProgressBarLoading);
mGridViewRecent.setOnItemClickListener(this);
getLoaderManager().initLoader(0, null, this);
}
public void onResume () {
super.onResume();
getActivity().invalidateOptionsMenu();
getLoaderManager().destroyLoader(0);
getLoaderManager().initLoader(0, null, this);
}
@Override
public Loader<List<Post>> onCreateLoader(int id, Bundle args) {
if (mTabHost.getCurrentTab() == 0) {
//popular tab is clicked...
mTabHost.setCurrentTab(0);
mPopularLoader = new PopularLoader(getActivity(), PopularLoader.PopularOption.PopularOptionPopular);
} else {
mTabHost.setCurrentTab(1);
//recent tab is clicked...
mPopularLoader = new PopularLoader(getActivity(), PopularLoader.PopularOption.PopularOptionRecent);
}
return mPopularLoader;
}
@Override
public void onLoadFinished(Loader<List<Post>> arg0, List<Post> data) {
mPopularAdapter.setData(data);
mPopularAdapter.notifyDataSetChanged();
//mTextViewNoItems.setVisibility(View.INVISIBLE);
mProgressBarLoading.setVisibility(View.INVISIBLE);
//mGridViewPopular.setEmptyView(mTextViewNoItems);
//mGridViewRecent.setEmptyView(mTextViewNoItems);
}
@Override
public void onLoaderReset(Loader<List<Post>> arg0) {
mPopularAdapter.setData(null);
}
@Override
public void onTabChanged(String tabId) {
getLoaderManager().destroyLoader(0);
getLoaderManager().initLoader(0, null, this);
}
private void setupTab(final View view, final String tag) {
View tabview = createTabView(mTabHost.getContext(), tag);
TabSpec setContent = mTabHost.newTabSpec(tag).setIndicator(tabview).setContent(new TabContentFactory() {
public View createTabContent(String tag) {return view;}
});
mTabHost.addTab(setContent);
}
private static View createTabView(final Context context, final String text) {
View view = LayoutInflater.from(context).inflate(com.company.stush.R.layout.tabs_bg, null);
TextView tv = (TextView) view.findViewById(com.company.stush.R.id.tabsText);
tv.setText(text);
return view;
}
@Override
public void onItemClick (AdapterView<?> parent, View view, int position, long id) {
Intent intentPostDetail = new Intent(getActivity(), PostDetailActivity.class);
Post post = mPopularAdapter.getItem(position);
intentPostDetail.putExtra("Post", post);
startActivity(intentPostDetail);
}
【问题讨论】:
-
使用选择器类并更改它们的文本。为此,您必须自定义标签。
-
@AnshulTyagi-Thankas 兄弟,但是你能给我一些代码或参考,因为我对它很陌生
标签: android tabs android-tabhost