导航抽屉不滑动

时间:2014-06-19 07:24:20

标签: 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);

    }
}

1 个答案:

答案 0 :(得分:2)

我强烈建议在.xml中创建自定义布局。 您做错了的是,您在DrawerLayout内创建了ListView,甚至没有将其添加到任何布局中。意思是,您的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