简体   繁体   English

实施汉堡包菜单后无法在网格视图布局中滚动

[英]Unable to scroll in a grid view layout after implementing hamburger menu

I have a gridview layout in main activity file. 我在主活动文件中有一个gridview布局。 Initially i was able to see all the images by scrolling but later when i added a hamburger menu in the same activity the gridview images didn't show all the images and i am also unable to scroll. 最初,我能够通过滚动查看所有图像,但是后来,当我在同一活动中添加汉堡菜单时,gridview图像并未显示所有图像,因此我也无法滚动。 I added the scrollview in activity.xml file, the app unfortunately stops. 我在activity.xml文件中添加了scrollview,不幸的是该应用程序停止了。 This is logcat: 这是logcat:

08-01 13:14:05.028 6153-6153/com.example.luke.goweb E/AndroidRuntime: FATAL EXCEPTION: main
                                                                      Process: com.example.luke.goweb, PID: 6153
                                                                      java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.luke.goweb/com.example.luke.goweb.MainActivity}: android.view.InflateException: Binary XML file line #88: ScrollView can host only one direct child
                                                                          at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
                                                                          at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
                                                                          at android.app.ActivityThread.-wrap11(ActivityThread.java)
                                                                          at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
                                                                          at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                          at android.os.Looper.loop(Looper.java:148)
                                                                          at android.app.ActivityThread.main(ActivityThread.java:5417)
                                                                          at java.lang.reflect.Method.invoke(Native Method)
                                                                          at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                                                                          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
                                                                       Caused by: android.view.InflateException: Binary XML file line #88: ScrollView can host only one direct child
                                                                          at android.view.LayoutInflater.inflate(LayoutInflater.java:539)
                                                                          at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
                                                                          at android.view.LayoutInflater.inflate(LayoutInflater.java:374)
                                                                          at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:280)
                                                                          at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140)
                                                                          at com.example.luke.goweb.MainActivity.onCreate(MainActivity.java:38)
                                                                          at android.app.Activity.performCreate(Activity.java:6237)
                                                                          at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
                                                                          at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
                                                                          at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
                                                                          at android.app.ActivityThread.-wrap11(ActivityThread.java) 
                                                                          at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
                                                                          at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                          at android.os.Looper.loop(Looper.java:148) 
                                                                          at android.app.ActivityThread.main(ActivityThread.java:5417) 
                                                                          at java.lang.reflect.Method.invoke(Native Method) 
                                                                          at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
                                                                          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
                                                                       Caused by: java.lang.IllegalStateException: ScrollView can host only one direct child
                                                                          at android.widget.ScrollView.addView(ScrollView.java:269)
                                                                          at android.view.LayoutInflater.rInflate(LayoutInflater.java:839)
                                                                          at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:798)
                                                                          at android.view.LayoutInflater.inflate(LayoutInflater.java:515)
                                                                          at android.view.LayoutInflater.inflate(LayoutInflater.java:423) 
                                                                          at android.view.LayoutInflater.inflate(LayoutInflater.java:374) 
                                                                          at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:280) 
                                                                          at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140) 
                                                                          at com.example.luke.goweb.MainActivity.onCreate(MainActivity.java:38) 
                                                                          at android.app.Activity.performCreate(Activity.java:6237) 
                                                                          at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107) 
                                                                          at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369) 
                                                                          at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
                                                                          at android.app.ActivityThread.-wrap11(ActivityThread.java) 
                                                                          at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
                                                                          at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                          at android.os.Looper.loop(Looper.java:148) 
                                                                          at android.app.ActivityThread.main(ActivityThread.java:5417) 
                                                                          at java.lang.reflect.Method.invoke(Native Method) 

It says scrollview can host only one direct child . 它说scrollview只能容纳一个直子 It's working fine in framelayout but i am unable to see all the images and also not able to scroll. 它在framelayout中工作正常,但我看不到所有图像,也无法滚动。 And in scrollview it unfortunately stops. 不幸的是,它在滚动视图中停止了。 But I want the gridview and the hamburger menu on the same activity page. 但我希望在同一活动页面上使用gridview和汉堡菜单。 Here is the activity.xml code: 这是activity.xml代码:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <GridView
        android:id="@+id/gridview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:verticalSpacing="2dp"
        android:horizontalSpacing="2dp"
        android:stretchMode="columnWidth"
        android:numColumns="2"/>
    <android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/drawerLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

<!-- The main content view -->
<RelativeLayout
    android:id="@+id/mainContent"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

<!-- The navigation drawer -->
<RelativeLayout
    android:layout_width="280dp"
    android:layout_height="match_parent"
    android:id="@+id/drawerPane"
    android:layout_gravity="start">

    <!-- Profile Box -->

    <RelativeLayout
        android:id="@+id/profileBox"
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:background="@color/material_blue_grey_800"
        android:padding="8dp" >

        <ImageView
            android:id="@+id/avatar"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:src="@drawable/ic_account_circle_black_24dp"
            android:layout_marginTop="15dp" />

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="42dp"
            android:layout_centerVertical="true"
            android:layout_marginLeft="15dp"
            android:layout_toRightOf="@+id/avatar"
            android:orientation="vertical" >

            <TextView
                android:id="@+id/userName"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Username"
                android:textColor="#fff"
                android:textSize="16sp"
                android:textStyle="bold" />

            <TextView
                android:id="@+id/desc"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="bottom"
                android:layout_marginTop="4dp"
                android:text="View Profile"
                android:textColor="#fff"
                android:textSize="12sp" />
        </LinearLayout>
    </RelativeLayout>

    <!-- List of Actions (pages) -->
    <ListView
        android:id="@+id/navList"
        android:layout_width="280dp"
        android:layout_height="match_parent"
        android:layout_below="@+id/profileBox"
        android:choiceMode="singleChoice"
        android:background="#ffffffff" />

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

Any help ? 有什么帮助吗? Or any suggestions ? 或有什么建议吗? Thank you in advance. 先感谢您。

Hmm, that shouldn't happen. 嗯,那不应该发生。 Try wrapping your GridView in a LinearLayout and then placing that LinearLayout inside the ScrollView. 尝试将GridView封装在LinearLayout中,然后将LinearLayout放入ScrollView中。 I've always had good luck with LinearLayouts inside ScrollViews... 我一直在ScrollViews中使用LinearLayouts感到好运...

EDIT: maybe your hamburger menu is partly to blame as well. 编辑:也许您的汉堡菜单也应归咎于部分。 Try Google's approach to Navigation Drawers (documentation here ), which is to place them in a separate fragment entirely. 尝试使用Google的导航抽屉方法( 此处的文档),将其完全放在单独的片段中。 This has the benefit of allowing you to use that menu in multiple Activities without having to copy that XML everywhere. 这样的好处是,您可以在多个活动中使用该菜单,而不必到处复制该XML。

EDIT 2: I just noticed you're including the DrawerLayout from the approach above inside your FrameLayout. 编辑2:我只是注意到您在FrameLayout中包含了上述方法中的DrawerLayout。 Inside a ScrollView, it is guaranteed to cause a crash. 在ScrollView内部,可以保证会导致崩溃。 If you move it outside the ScrollView, your problems should go away. 如果将其移到ScrollView之外,您的问题将消失。

As you mentioned you were able to view and scroll all the images in grid view. 如前所述,您可以在网格视图中查看和滚动所有图像。 But the scroll didn't work after implementing the hamburger menu or the navigation drawer. 但是在实现汉堡菜单或导航抽屉后,滚动无法正常工作。 What i will say that don't mix up the layout code of navigation drawer and grid layout you better separate these two files and call it whenever you need. 我要说的是,不要混淆导航抽屉和网格布局的布局代码,最好将这两个文件分开,并在需要时调用它。 Here is the complete code to implement navigation drawer. 这是实现导航抽屉的完整代码。 Hope this helps you. 希望这对您有所帮助。 First create an xml file and add these codes: 首先创建一个xml文件并添加以下代码:

<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 -->
    <FrameLayout
        android:id="@+id/content_frame"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
    <!-- The navigation drawer -->
    <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>

Later create a DrawerItemClickListener class and add these code: 稍后创建DrawerItemClickListener类并添加以下代码:

private class DrawerItemClickListener implements ListView.OnItemClickListener {
    @Override
    public void onItemClick(AdapterView parent, View view, int position, long id) {
        selectItem(position);
    }
}

/** Swaps fragments in the main content view */
private void selectItem(int position) {
    // Create a new fragment and specify the planet to show based on position
    Fragment fragment = new PlanetFragment();
    Bundle args = new Bundle();
    args.putInt(PlanetFragment.ARG_PLANET_NUMBER, position);
    fragment.setArguments(args);

    // Insert the fragment by replacing any existing fragment
    FragmentManager fragmentManager = getFragmentManager();
    fragmentManager.beginTransaction()
                   .replace(R.id.content_frame, fragment)
                   .commit();

    // Highlight the selected item, update the title, and close the drawer
    mDrawerList.setItemChecked(position, true);
    setTitle(mPlanetTitles[position]);
    mDrawerLayout.closeDrawer(mDrawerList);
}

@Override
public void setTitle(CharSequence title) {
    mTitle = title;
    getActionBar().setTitle(mTitle);
}

Now it's time to write something in your MainActivity source file 现在是时候在MainActivity源文件中写一些东西了

public class MainActivity extends Activity {
    private DrawerLayout mDrawerLayout;
    private ActionBarDrawerToggle mDrawerToggle;
    private CharSequence mDrawerTitle;
    private CharSequence mTitle;
    ...

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

        mTitle = mDrawerTitle = getTitle();
        mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
        mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
                R.drawable.ic_drawer, R.string.drawer_open, R.string.drawer_close) {

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

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

        // Set the drawer toggle as the DrawerListener
        mDrawerLayout.setDrawerListener(mDrawerToggle);
    }

    /* Called whenever we call invalidateOptionsMenu() */
    @Override
    public boolean onPrepareOptionsMenu(Menu menu) {
        // If the nav drawer is open, hide action items related to the content view
        boolean drawerOpen = mDrawerLayout.isDrawerOpen(mDrawerList);
        menu.findItem(R.id.action_websearch).setVisible(!drawerOpen);
        return super.onPrepareOptionsMenu(menu);
    }
}

After this you will be able to view your full grid layout as well as navigation drawer. 之后,您将能够查看完整的网格布局以及导航抽屉。 Swipe the screen from left to right you will see the navigation drawer. 从左向右滑动屏幕,您将看到导航抽屉。 To add the action with an App icon you can go through the link by @Shobhik . 要添加带有App图标的动作,您可以通过@Shobhik链接。 You can also go for RecyclerView with a GridLayout as suggested by @cricket_007 . 您也可以按照@ cricket_007的建议使用带有GridLayout的RecyclerView Follow any of these advice or this code will get your job done !! 遵循任何这些建议,否则此代码将完成您的工作!

FrameLayout can contain a lot of child but ScrollView can not. FrameLayout可以包含很多子项,但ScrollView不能。 If you want to still use ScrollView add a LinearLayout or something else as parent of other childs. 如果仍要使用ScrollView添加LinearLayout或其他内容作为其他子项的父项。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM