左抽屉中有多个视图

时间:2016-02-24 09:07:08

标签: android drawerlayout navigationview

您好我正在尝试制作工具导航抽屉,就像这些截图中的任何想法如何实现这一点? Before Clicking After Clicking

点击女性时,左侧抽屉中的视图已完全更改。我需要帮助。

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"

tools:context="com.example.satya.navigationdrawer.MainActivity">

<FrameLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

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

        <android.support.v7.widget.Toolbar
            android:id="@+id/my_toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            android:elevation="4dp"
            android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />


        <LinearLayout
            android:id="@+id/main_fragment_container"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">

        </LinearLayout>
    </LinearLayout>

    <FrameLayout
        android:id="@+id/drawer_fragment_container"
        android:layout_width="240dp"
        android:layout_height="match_parent"
        android:layout_gravity="start">

    </FrameLayout>
   </FrameLayout>
   </android.support.v4.widget.DrawerLayout>

MainActivity

public class MainActivity extends AppCompatActivity {

private DrawerLayout drawerLayout;

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

    Toolbar toolbar = (Toolbar) findViewById(R.id.my_toolbar);
    setSupportActionBar(toolbar);

    drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
    ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawerLayout, toolbar, R.string.open, R.string.close);
    drawerLayout.setDrawerListener(toggle);
    toggle.syncState();

//        if (findViewById(R.id.drawer_fragment_container) != null) {
//            if (savedInstanceState != null) {
//                return;
//            }       **//If i use this fragment gets inflated before even toggle button is   pressed**
//            DrawerFragment drawerFragment = new DrawerFragment();
//            getSupportFragmentManager().beginTransaction().add(R.id.drawer_fragment_container, drawerFragment).commit();
 //            }

         }

@Override
public void onBackPressed() {
    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    if (drawer.isDrawerOpen(GravityCompat.START)) {
        drawer.closeDrawer(GravityCompat.START);
    } else {
        super.onBackPressed();
    }
  }
  }

片段

        public class DrawerFragment extends Fragment {

        String[] drawerList = {"A", "B", "C", "D"};

        public DrawerFragment() {
            // Required empty public constructor
        }


        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            // Inflate the layout for this fragment


            List<String> listItems = new ArrayList<>(Arrays.asList(drawerList));
            ArrayAdapter<String> listItemsAdapter = new ArrayAdapter<String>(getActivity(), R.layout.drawer_list_item, R.id.drawer_list_item, listItems);

            View view = inflater.inflate(R.layout.fragment_drawer, container, false);
            ListView listView = (ListView) view.findViewById(R.id.drawer_list);
            listView.setAdapter(listItemsAdapter);

            return view;
        }

    }

2 个答案:

答案 0 :(得分:0)

尝试在ExpandableListview布局

中使用ListView代替NavigationDrawer

答案 1 :(得分:0)

导航抽屉可以将视图替换为另一个视图,就像Android中的任何其他区域一样。

如果您的应用使用片段,您可以执行以下操作:

        // Create a new Fragment to be placed in the activity layout
        ExampleFragment firstFragment = new ExampleFragment ();

        // In case this activity was started with special instructions from an
        // Intent, pass the Intent's extras to the fragment as arguments
        firstFragment.setArguments(getIntent().getExtras());

        // Add the fragment to the 'fragment_container' FrameLayout
        getSupportFragmentManager().beginTransaction()
                .add(R.id.fragment_container, firstFragment).commit();