如何在没有碎片的情况下使用导航抽屉?

时间:2013-05-22 21:56:54

标签: android navigation drawer

我正在尝试按照tutorial关于如何创建导航抽屉,但我不想在用户从抽屉列表中选择项目后使用片段来显示新内容。解决这个问题的最佳方法是什么? 我正在使用不实现Fragments的API 10.

4 个答案:

答案 0 :(得分:17)

首先,API 10可以通过包含DrawerLayout的相同Android支持包访问片段。这已经存在了两年多了,如果您不熟悉Android过去两年的所有内容,那么您不应该尝试使用DrawerLayout这样的新内容。

其次,DrawerLayout与片段无关。引用the Web page that you linked to

  

当用户选择抽屉列表中的项目时,系统会在给予setOnItemClickListener()的OnItemClickListener上调用onItemClick()。您在onItemClick()方法中执行的操作取决于您实现应用程序结构的方式。

如果你仔细阅读这两个句子,你会发现“片段”这两个词都没有出现。那是因为DrawerLayout与片段无关。他们展示的示例代码使用片段,但这只是示例代码。

因此,欢迎您随意更新您的用户界面:

  • 使用Android Support包的片段backport执行FragmentTransaction,或
  • 开始活动,或
  • 再次就您现有的活动致电setContentView(),或
  • 否则修改现有活动的UI(例如,隐藏/显示一些小部件)

答案 1 :(得分:1)

使用此代码

private void selectItem(int position) {


    // Locate Position
    switch (position) {
    case 0:
            startActivity(new Intent(this,TEST.class));
        break;

答案 2 :(得分:1)

您也可以使用LayoutInflater类。

  1. 创建xml布局文件。
  2. 使用findViewById找到要更改的视图。
  3. 使用.removeAllViews()方法从找到的View中删除所有子项。
  4. 使用.inflate()方法将xml布局内容扩展到找到的视图。
  5. 这是一个例子:

    LinearLayout layoutToChange = (LinearLayout)findViewById(R.id.layout_to_change);
    layoutToChange.removeAllViews();
    
    LayoutInflater inflater = LayoutInflater.from(this);
    LinearLayout newLayout = (LinearLayout)inflater.inflate(R.layout.new_layout, null);
    
    layoutToChange.addView(newLayout);
    

答案 3 :(得分:0)

没有片段的基本抽屉

package xxxxxx;


import android.content.res.Configuration;
import android.os.Bundle;
import android.support.v4.widget.DrawerLayout;

import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;




public class loginhome extends AppCompatActivity {

    private ListView mDrawerList;
    private DrawerLayout mDrawerLayout;
    private ArrayAdapter<String> mAdapter;
    private ActionBarDrawerToggle mDrawerToggle;
    private String mActivityTitle;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.loginhome);
        Toolbar topToolBar = (Toolbar)findViewById(R.id.toolbar);
        setSupportActionBar(topToolBar);
        mDrawerList = (ListView)findViewById(R.id.navList);
        mDrawerLayout = (DrawerLayout)findViewById(R.id.drawer_layout);
        mActivityTitle = getTitle().toString();

        addDrawerItems();
        setupDrawer();


        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setHomeButtonEnabled(true);

    }

    private void addDrawerItems() {
        String[] osArray = { "Android", "iOS", "Windows", "OS X", "Linux" };
        mAdapter = new ArrayAdapter<String>(this, R.layout.drawer_items,R.id.label ,osArray);
        mDrawerList.setAdapter(mAdapter);

        mDrawerList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Toast.makeText(loginhome.this, "Time for an upgrade!", Toast.LENGTH_SHORT).show();
            }
        });
    }

    private void setupDrawer() {
        mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, R.string.drawer_open, R.string.drawer_close) {

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

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

        mDrawerToggle.setDrawerIndicatorEnabled(true);
        mDrawerLayout.setDrawerListener(mDrawerToggle);
    }

    @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);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_help) {
            Toast.makeText(loginhome.this, "setting", Toast.LENGTH_LONG).show();
        }
        if(id == R.id.action_place){
            Toast.makeText(loginhome.this, "Refresh App", Toast.LENGTH_LONG).show();
        }
        if(id == R.id.action_search){
            Toast.makeText(loginhome.this, "Create Text", Toast.LENGTH_LONG).show();
        }
        // Activate the navigation drawer toggle
        if (mDrawerToggle.onOptionsItemSelected(item)) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

draweritems.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">


        <TextView
            android:id="@+id/label"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="#000"
            android:textSize="30sp"
            android:background="#D3D3D3">
        </TextView>
    </LinearLayout>

toolbar.xml

<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/colorPrimary"
        android:elevation="4dp"
        android:id="@+id/toolbar"
        android:theme="@style/ThemeOverlay.AppCompat.Dark"


        >

    </android.support.v7.widget.Toolbar>