如何打开的抽屉式导航新的片段?片段、抽屉式

2023-09-04 11:12:23 作者:她哭了你慌了我笑了

我用developer.android.com指导来构建一个应用程序。我选用导航:导航抽屉当我在做Android的Studio中的一个新项目。我已在网上搜索答案,以我的问题,但我找不到任何工作。很抱歉,我是新来的节目。

I'm using developer.android.com guides to build an app. I choosed "Navigation: Navigation Drawer" when I made a new project in Android Studio. I have searched the internet for answers to my questions but I can't find any that works. Sorry about this, I'm new to programming.

如何让我的应用程序的主视图导航抽屉时,点击打开一个新的片段? 在导航抽屉单击后可以打开与标签多个swipeable片段? 如何做一个头衔展开/折叠的?

http://developer.android.com/design/patterns/navigation-drawer.html http://developer.android.com/training/implementing-navigation/nav-drawer.html

这是我想要的布局是这样的:

This is how I want the layout to be like:

title_section *不section_title;)

title_section* not section_title ;)

推荐答案

抽屉式导航是一种新的设计趋势,这些天。我们使用两种布局:主要内容布局,在设计xml.layout(布局)导航抽屉活动抽屉列表布局。在这里,我回答你所有的无聊的问题。

Navigation Drawer is a new and trending design these days. We use two layouts: main content layout and the drawer list layout while designing the xml.layout(layout) for the navigation drawer activity. Here I'm answering all your silly questions.

如何让我的应用程序,在主视图中,当打开一个新片段   单击导航抽屉里?

How do I make my app open a new fragment in the main view when clicking in the navigation drawer?

只需添加clicklistener抽屉列表项和根据列表项的位置主要内容替换片段点击。

simply add clicklistener on the drawer list items and replace fragments in the main content depending upon the position of the list item clicked.

样品code:

    // The click listener for ListView in the navigation drawer
    @SuppressWarnings("unused")
    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) {

        Fragment newFragment;
        FragmentTransaction transaction = getFragmentManager().beginTransaction();

        switch (position) {
        case 0:
            newFragment = new f1();
            transaction.replace(R.id.content_frame, newFragment);
            transaction.addToBackStack(null);
            transaction.commit();
            break;

        case 1:
            newFragment = new f2();
            transaction.replace(R.id.content_frame, newFragment);
            transaction.addToBackStack(null);
            transaction.commit();
            break;

        case 2:
            newFragment = new f3();
            transaction.replace(R.id.content_frame, newFragment);
            transaction.addToBackStack(null);
            transaction.commit();
            break;

        case 3:
            newFragment = new f4();
            transaction.replace(R.id.content_frame, newFragment);
            transaction.addToBackStack(null);
            transaction.commit();
            break;  


        }
        //DrawerList.setItemChecked(position, true);
        setTitle(ListTitles[position]);
        DrawerLayout.closeDrawer(DrawerList);   
    }

下面F1,F2。 F3和F4是不同片段每个具有其自己的布局。你必须继承片段类创建单独的Java类他们。

Here f1, f2. f3 and f4 are different fragments each having its own layout. you have to create separate java classes for them by inheriting the fragment class.

可以在导航抽屉时,点击打开多个swipeable片段与标签?

Is it possible to open multiple swipeable fragments with tabs when clicking in the navigation drawer?

为了实现一个片段中的选项卡,你可以使用一个tabhost特定的片段中。 假设你想在片段f_main添加标签。

In order implement tabs within a fragment you can use a tabhost inside that particular fragment. Suppose you want to add tabs in fragment f_main.

有关F_main.xml布局

layout for F_main.xml

<TabHost 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/tabhost"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

   <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >

        <TabWidget
            android:id="@android:id/tabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="0"
            android:orientation="horizontal" />

        <FrameLayout
            android:id="@+id/tabFrameLayout"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1" />
    </LinearLayout>

</TabHost>

然后,让其他片段f_tab1和f_tab2及其相应的布局和Java类。 布局对于两个选项卡片段可以是相同或不同的。在这里我要带他们相同或共同的布局。

Then make other fragments f_tab1 and f_tab2 with their corresponding layouts and java classes. Layouts for the two tab fragments can be same or different. here I'm taking them same or a common layout.

<?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:orientation="vertical" >

        <TextView android:id="@+id/google_map"
            android:layout_height="wrap_content"
            android:layout_width="match_parent"
            android:text="MAP"/>

    </LinearLayout>

$ C $下F_tab1.java片段

Code for F_tab1.java fragment

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class F_tab1 extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        View view=inflater.inflate(R.layout.friends_list, container,false);


        return view;
    }

}

code的另一片段。即F_tab2.java

Code for another fragment. i.e F_tab2.java

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class F_tab2 extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        View view=inflater.inflate(R.layout.friends_list, container,false);


        return view;
    }

}

现在只需使用clicklistener在像前面提到的加载F_main关于项目点击抽屉名单,这将进一步加载的主要内容来看,在F_main fragmnet标签抽屉列表。

Now simply use clicklistener in the drawer list as mentioned earlier to load F_main on item click in the drawer list which will further load the tabs in the F_main fragmnet in the main content view.

我如何做一个头衔展开/折叠的?

APP导航设计,看这篇就够了...

How do I make a "title" expandable/collapsible?

嗯,我不知道wheather的NV抽屉里提供了这个功能,或者不。但它提供了一个功能来切换取决于选择的抽屉项目或加载的主要内容片段的操作栏标题。

Well I don't know wheather the NV drawer provides this feature or not. But it provides a feature to toggle the action bar title depending upon the drawer item selected or the main content fragment loaded.

由于在导航抽屉设计指南讨论,您应该修改操作栏的内容时抽屉是可见的,如更改标题,并删除那些语境的主要内容行动项目。下面code展示了如何通过重写DrawerLayout.DrawerListener回调方法与ActionBarDrawerToggle类的一个实例如下这样做

As discussed in the Navigation Drawer design guide, you should modify the contents of the action bar when the drawer is visible, such as to change the title and remove action items that are contextual to the main content. The following code shows how you can do so by overriding DrawerLayout.DrawerListener callback methods with an instance of the ActionBarDrawerToggle class as follows

 public class MainActivity extends Activity {
    private DrawerLayout mDrawerLayout;
    private ActionBarDrawerToggle mDrawerToggle;
    private CharSequence mDrawerTitle;
    private CharSequence mTitle;
    ...

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

        mTitle = mDrawerTitle = getTitle();
        mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
        mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
                R.drawable.ic_drawer, R.string.drawer_open, R.string.drawer_close) {

            /** Called when a drawer has settled in a completely closed state. */
            public void onDrawerClosed(View view) {
                getActionBar().setTitle(mTitle);
                invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
            }

            /** Called when a drawer has settled in a completely open state. */
            public void onDrawerOpened(View drawerView) {
                getActionBar().setTitle(mDrawerTitle);
                invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
            }
        };

        // Set the drawer toggle as the DrawerListener
        mDrawerLayout.setDrawerListener(mDrawerToggle);
    }

    /* Called whenever we call invalidateOptionsMenu() */
    @Override
    public boolean onPrepareOptionsMenu(Menu menu) {
        // If the nav drawer is open, hide action items related to the content view
        boolean drawerOpen = mDrawerLayout.isDrawerOpen(mDrawerList);
        menu.findItem(R.id.action_websearch).setVisible(!drawerOpen);
        return super.onPrepareOptionsMenu(menu);
    }
    }