簡體   English   中英

在片段中帶有選項卡的導航抽屜

[英]Navigation drawer with tabs in fragment

我正在制作一個 android 應用程序。 我正在使用導航抽屜,我使用導航抽屜膨脹的一些片段包含選項卡布局,有些是帶有 textview 的普通片段。

我面臨兩個問題,當

  1. 我正在打開帶有標簽的導航抽屜項目,然后它會自動將這些標簽保留在視圖中,即使對於沒有標簽的片段也是如此。

  2. 當我第二次或更多次使用標簽打開片段時。 它復制選項卡。 我的意思是,如果第一次沒有選項卡是 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;
}

}

我得到了我的問題的解決方案:

  1. 在 denvercoder9 的幫助下解決了第一個問題

當 position!= 1 時,您需要更改 actionBar 導航模式.... Android ActionBar:動態顯示/隱藏選項卡? – denvercoder9

  1. 第二個問題的解決方案是我需要使用這個

ActionBar actionBar = getActivity().getActionBar();

    actionBar.removeAllTabs();

    actionBar.setDisplayShowHomeEnabled(true);

    actionBar.setDisplayShowTitleEnabled(true);

    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

在 Home.java 類中

這解決了標簽重復的問題。

感謝 Stackoverflow 幫助我:)

我遇到了同樣的問題,所以解決方案很簡單。

Ans 1: 如果你不想在特定片段中使用 Tabs 那么你可以將導航欄設置為

actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);

Ans 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;

希望它能解決你的問題

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM