Error : View android.widget.ListView is not a sliding drawer

后端 未结 3 2075
Happy的楠姐
Happy的楠姐 2020-12-10 18:17

I am using NavigationDrawer in my Android app, I have made my own custom layouts XML file and their adapters also but When I run my program then my application

相关标签:
3条回答
  • 2020-12-10 18:55

    Your ListView must specify the android:layout_gravity attribute in the xml so it can be identified as sliding drawer by the DrawerLayout. Use right, left, start, end as values.

    0 讨论(0)
  • 2020-12-10 19:00

    While using DrawerLayout make sure it is the rootLayout. don't make it a child of LinearLayout or RelativeLayout.

    as closeDrawer come up with two override function first View and second Gravity

    As normally it is close by Gravity. So in order to close drawer you must set

    android:layout_gravity =""
    

    and all android:layout_gravity must be unique in DrawerLayout

     drawer.closeDrawer(GravityCompat.START)//this code close the drawer child with layout_gravity ="start". 
    

    and if you have some other view like linearLayout above listView give layout_gravity to that layout and remove it from listView. drawer close item on the basis of gravity

        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        ///drawer.closeDrawer(anyViewToClose);
        drawer.closeDrawer(GravityCompat.START);//but it is better to do this using gravity.
    

    See closeDrawer code

    public void closeDrawer(@EdgeGravity int gravity, boolean animate) {
            final View drawerView = findDrawerWithGravity(gravity);
            if (drawerView == null) {
                throw new IllegalArgumentException("No drawer view found with gravity "
                        + gravityToString(gravity));
            }
            closeDrawer(drawerView, animate);
        }
    

    also see isDrawerView

    boolean isDrawerView(View child) {
            final int gravity = ((LayoutParams) child.getLayoutParams()).gravity;
            final int absGravity = GravityCompat.getAbsoluteGravity(gravity,
                    ViewCompat.getLayoutDirection(child));
            if ((absGravity & Gravity.LEFT) != 0) {
                // This child is a left-edge drawer
                return true;
            }
            if ((absGravity & Gravity.RIGHT) != 0) {
                // This child is a right-edge drawer
                return true;
            }
            return false;
        }
    
    0 讨论(0)
  • 2020-12-10 19:14

    I had same problem with tutorial of Android Hive. But after I found one magical solution.

    Put android:layout_gravity="start" or android:layout_gravity="left" in your ListView

    <ListView
        android:id="@+id/listSlideMenu"
        android:layout_width="240dp"
        android:layout_height="fill_parent"
        android:layout_gravity="left" 
        android:choiceMode="singleChoice"
        android:dividerHeight="1dp"
        android:background="@color/color_primary"/>
    

    I got the same issue learn from androidhive perfectly working with gravity="start" but on gravity change to end it raise error

    0 讨论(0)
提交回复
热议问题