我正在制作一个Android应用程序。我正在使用导航抽屉和我使用导航抽屉充气的一些片段包含标签布局,一些是带有textview的正常片段。
我正面临着两个问题
我打开带有标签的导航抽屉项目,即使对于没有标签的片段,它也会自动将这些标签保留在视图中。
当我用标签打开片段第二次或更多时。它复制了标签。我的意思是如果第一次没有标签是3然后下次当我打开片段时没有。选项卡加倍。
带导航抽屉的活动
public class MainFeedActivity extends FragmentActivity {
public DrawerLayout mDrawerLayout;
private ListView mDrawerList;
private ActionBarDrawerToggle mDrawerToggle;
private CharSequence mDrawerTitle;
private CharSequence mTitle;
private String[] mDrawertitles;
LinearLayout drawerll;
String username;
TextView t;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_mainfeed);
username = getIntent().getExtras().getString("username").toString();
t = (TextView) findViewById(R.id.drawer_uname_tv);
t.setText(username);
mTitle = mDrawerTitle = getTitle();
mDrawertitles = getResources().getStringArray(R.array.array);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerList = (ListView) findViewById(R.id.left_drawer);
drawerll = (LinearLayout) findViewById(R.id.drawerll);
// set a custom shadow that overlays the main content when the drawer
// opens
mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow,
GravityCompat.START);
mDrawerList.setAdapter(new ArrayAdapter<String>(this,
R.layout.drawer_list_item, mDrawertitles));
mDrawerList.setOnItemClickListener(new DrawerItemClickListener());
getActionBar().setDisplayHomeAsUpEnabled(true);
mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
R.drawable.ic_drawer, R.string.drawer_open,
R.string.drawer_close) {
public void onDrawerClosed(View view) {
getActionBar().setTitle(mTitle);
invalidateOptionsMenu(); // creates call to
// onPrepareOptionsMenu()
}
public void onDrawerOpened(View drawerView) {
getActionBar().setTitle(mDrawerTitle);
invalidateOptionsMenu(); // creates call to
// onPrepareOptionsMenu()
}
};
mDrawerLayout.setDrawerListener(mDrawerToggle);
if (savedInstanceState == null) {
selectItem(2);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// The action bar home/up action should open or close the drawer.
// ActionBarDrawerToggle will take care of this.
if (mDrawerToggle.onOptionsItemSelected(item)) {
return true;
}
return true;
}
private class DrawerItemClickListener implements
ListView.OnItemClickListener {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
selectItem(position);
}
}
private void selectItem(int position) {
if (position == 0) {
Fragment fragment = new Home();
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.content_frame, fragment).commit();
mDrawerList.setItemChecked(position, true);
mDrawerLayout.closeDrawer(drawerll);
}
else if (position == 1) {
Fragment fragment = new Statistics();
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.content_frame, fragment).commit();
// update selected item and title, then close the drawer
mDrawerList.setItemChecked(position, true);
mDrawerLayout.closeDrawer(drawerll);
} else {
Fragment fragment = new Help();
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.content_frame, fragment).commit();
// update selected item and title, then close the drawer
mDrawerLayout.closeDrawer(drawerll);
}
}
/*
*
* protected boolean isOnline() { ConnectivityManager cm =
* (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
* NetworkInfo netInfo = cm.getActiveNetworkInfo(); if (netInfo != null &&
* netInfo.isConnectedOrConnecting()) { return true; } else { return false;
* } }
*/
@Override
public void setTitle(CharSequence title) {
mTitle = title;
getActionBar().setTitle(mTitle);
}
/**
* When using the ActionBarDrawerToggle, you must call it during
* onPostCreate() and onConfigurationChanged()...
*/
@Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
// Sync the toggle state after onRestoreInstanceState has occurred.
mDrawerToggle.syncState();
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
mDrawerToggle.onConfigurationChanged(newConfig);
}
/**
* Fragment that appears in the "content_frame", shows a planet
*/
}
片段
public class Home extends Fragment {
ActionBar.Tab Tab1, Tab2, Tab3;
Fragment fragmentTab1 = new Connected();
Fragment fragmentTab2 = new Disconnected();
Fragment fragmentTab3 = new AllDevices();
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_tabs, container, false);
ActionBar actionBar = getActivity().getActionBar();
actionBar.setDisplayShowHomeEnabled(true);
// Hide Actionbar Title
actionBar.setDisplayShowTitleEnabled(true);
// Create Actionbar Tabs
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
Tab1 = actionBar.newTab().setText("Tab1");
Tab2 = actionBar.newTab().setText("Tab2");
Tab3 = actionBar.newTab().setText("Tab3");
Tab1.setTabListener(new TabListener(fragmentTab1));
Tab2.setTabListener(new TabListener(fragmentTab2));
Tab3.setTabListener(new TabListener(fragmentTab3));
actionBar.addTab(Tab1);
actionBar.addTab(Tab2);
actionBar.addTab(Tab3);
return view;
}
}
片段:
public class Help extends Fragment {
Button b;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.help, container, false);
setHasOptionsMenu(false);
return rootView;
}
}
答案 0 :(得分:3)
我解决了我的问题:
当位置!= 1时,您需要更改actionBar导航模式.... stackoverflow.com/questions/16298930 / ... - Rafiduzzaman Sonnet
ActionBar actionBar = getActivity()。getActionBar();
actionBar.removeAllTabs();
actionBar.setDisplayShowHomeEnabled(true);
actionBar.setDisplayShowTitleEnabled(true);
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
Home.java类中的
这解决了标签重复的问题。
感谢Stackoverflow帮助我:)
答案 1 :(得分:0)
我遇到了同样的问题所以解决方案很简单。
答案1:如果您不希望特定片段中的标签,则可以将导航栏设置为
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
答案2:每次你选择它时都要从主片段中删除标签,或者你可以检查是否已经存在必需的标签然后不再在其中添加任何标签
int tabCount= actionBar.getTabCount();
if(tabCount!=3){
Tab1 = actionBar.newTab().setText("Tab1");
Tab2 = actionBar.newTab().setText("Tab2");
Tab3 = actionBar.newTab().setText("Tab3");
Tab1.setTabListener(new TabListener(fragmentTab1));
Tab2.setTabListener(new TabListener(fragmentTab2));
Tab3.setTabListener(new TabListener(fragmentTab3));
actionBar.addTab(Tab1);
actionBar.addTab(Tab2);
actionBar.addTab(Tab3);
}
return view;
希望它能解决你的问题