android - 抽屉导航不滑动

标签 android user-interface navigation-drawer slidingdrawer

我已经以编程方式实现了一个Navigation Drawer。虽然当我点击左上角的图标时其他一切正常,但屏幕变暗(抽屉确实滑入时的动画)但抽屉根本不滑动。

这是我的代码。

public class MainActivity extends Activity {
    private static final String TAG = "MAIN_ACTIVITY";
    private DrawerLayout mDrawerLayout;
    public static ArrayList<String> mOptions=new ArrayList<String>();
    // ListView represents Navigation Drawer
    private ListView mDrawerList;
    private ActionBarDrawerToggle mDrawerToggle;
    private CharSequence mTitle;
    private MainActivityLayout mainActivityLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mainActivityLayout=new MainActivityLayout(this,null);

        setContentView(mainActivityLayout);//changed this


        mOptions.add("Search");
        mOptions.add("Inbox");
        mOptions.add("Shout-A-Loud");
        mOptions.add("Saved Profiles");
        mOptions.add("Profile viewers");// premium feature. Will display profile views but not viewers.
        mOptions.add("Settings");
        mOptions.add("App Info");


        mDrawerLayout=(DrawerLayout) mainActivityLayout;//this changed
        mDrawerList=(ListView) new ListViewCustom(this,null);


        ArrayAdapter<String> adapter = new ArrayAdapter<String>(getBaseContext(), 
                R.layout.drawer_list_item, mOptions);


        mDrawerList.setAdapter(adapter);
        mDrawerList.setOnItemClickListener(new DrawerItemClickListener());
        getActionBar().setDisplayHomeAsUpEnabled(true);
        getActionBar().setHomeButtonEnabled(true);


        mDrawerToggle = new ActionBarDrawerToggle(
                this,                    
                mDrawerLayout,           
                R.drawable.ic_drawer,   
                R.string.drawer_open,    
                R.string.drawer_close   
                ) {
            public void onDrawerClosed(View view) {
                super.onDrawerClosed(view);
                mTitle="Social Geek";
                getActionBar().setTitle(mTitle);
                invalidateOptionsMenu(); 
            }

            public void onDrawerOpened(View view) {
                super.onDrawerOpened(view);
                mTitle="Select";
                getActionBar().setTitle(mTitle);
                invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
            }
        };
        mDrawerLayout.setDrawerListener(mDrawerToggle);
        getActionBar().setDisplayHomeAsUpEnabled(true);
        getActionBar().setHomeButtonEnabled(true);




        if (savedInstanceState == null) {
            selectItem(0);
        }

    }

二等

public class MainActivityLayout extends DrawerLayout{

static Context context;
static DrawerLayout DrawerLayoutCustom;
static ListViewCustom ListViewCustom;

    public MainActivityLayout(Context context,AttributeSet attr) {
        super(context,attr);
        MainActivityLayout.context=context;
        ListViewCustom=new ListViewCustom(context,null);

        LayoutParams layoutParams=new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT);
        setLayoutParams(layoutParams);
        addView(ListViewCustom);
        initializeEverything(context, null);

    }

第三类

public class ListViewCustom extends ListView{
    static final int LISTVIEW_CUSTOM=0;
    public ListViewCustom(Context context,AttributeSet attrs) {
        super(context,attrs);



        DrawerLayout.LayoutParams layoutParams=
                new DrawerLayout.LayoutParams(240,LayoutParams.MATCH_PARENT);
        layoutParams.gravity=Gravity.START;
        setBackgroundColor(111);
        setDivider(getResources().getDrawable(android.R.color.transparent));
        setChoiceMode(CHOICE_MODE_SINGLE);
        setLayoutParams(layoutParams);

    }
}

最佳答案

我强烈建议在 .xml 中创建自定义布局。 您做错的是,您在 ListView 中创建了 DrawerLayout,甚至没有将其添加到任何布局中。意思是,您的 ListView 是父级,而 DrawerLayout 是子级,这是不正确的。

正确的做法是 DrawerLayout 是父级ListView子级。 (意思是,ListView 需要在 DrawerLayout 内)。

xml 示例:

<!--- drawerlayout needed to make it all work -->
<android.support.v4.widget.DrawerLayout

    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <!-- The main content view this is where you display your stuff -->
    <FrameLayout
        android:id="@+id/content_frame"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <!-- The navigation drawer, this will slide in and out -->
    <ListView android:id="@+id/left_drawer"
        android:layout_width="240dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:choiceMode="singleChoice"
        android:divider="@android:color/transparent"
        android:dividerHeight="0dp"
        android:background="#111"/>

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

也请看这里: How to create a NavigationDrawer

关于android - 抽屉导航不滑动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24300960/

相关文章:

android - 进度对话框在 Activity 恢复时永远旋转

android - 如何在 Osmand Android 中更改 primaryRoadColor?

user-interface - 具有所有 HTTP 方法的浏览器的 REST 开发插件

Java StackTrace 编辑器或 GUI

Android - 导航 View 项目菜单背景颜色

安卓工作室 : custom keymap file location on OSX

javascript - 使用 Jquery Mobile 多页模板将页面重定向到带有 Javascript 的 div

java - 我怎样才能将 Borderlayout 常量用于不是不同布局管理器的面板?

java - 项目上的抽屉导航点击仍保留在主页 fragment 上

java - 在抽屉导航中跨多个组实现多项选择