Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/342.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 当抽屉布局关闭时,RecyclerView将成为焦点_Java_Android_Android Recyclerview_Drawerlayout - Fatal编程技术网

Java 当抽屉布局关闭时,RecyclerView将成为焦点

Java 当抽屉布局关闭时,RecyclerView将成为焦点,java,android,android-recyclerview,drawerlayout,Java,Android,Android Recyclerview,Drawerlayout,这是我发现的一种奇怪的虫子 但如果我有一个片段中的RecyclerView,我打开然后关闭我的抽屉布局,我的RecyclerView就会聚焦。这意味着关闭抽屉布局将导致ScrollView跳转到我的RecyclerView。显然,我希望当抽屉布局关闭时,ScrollView的位置不要移动,并且绝对不要停留在我的RecyclerView上 代码 在整个应用程序中,我在ScrollView中包含了RecyclerViews,类似于此设置: <ScrollView xmlns:android=

这是我发现的一种奇怪的虫子

但如果我有一个片段中的RecyclerView,我打开然后关闭我的抽屉布局,我的RecyclerView就会聚焦。这意味着关闭抽屉布局将导致ScrollView跳转到我的RecyclerView。显然,我希望当抽屉布局关闭时,ScrollView的位置不要移动,并且绝对不要停留在我的RecyclerView上


代码

在整个应用程序中,我在ScrollView中包含了RecyclerViews,类似于此设置:

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <RelativeLayout
            android:layout_alignParentTop="true"
            android:layout_width="match_parent"
            android:layout_height="180dp">

            <android.support.v7.widget.AppCompatImageView
                android:id="@+id/header_photo"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:adjustViewBounds="true"
                android:tint="#80000000"
                android:scaleType="centerCrop"/>

            <LinearLayout
                android:orientation="vertical"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerVertical="true"
                android:layout_centerHorizontal="true">

                <TextView android:id="@+id/header_text"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:gravity="center"
                    android:paddingTop="2dp"
                    android:paddingBottom="2dp"
                    android:textColor="@color/text_SecondaryColor"
                    android:textSize="@dimen/header_xxlarge"/>

                <View android:id="@+id/divider"
                    android:layout_width="0dp"
                    android:layout_height="@dimen/hr_thick"
                    android:background="@color/accent_PrimaryColor"/>
            </LinearLayout>
        </RelativeLayout>

        <LinearLayout
            android:orientation="vertical"
            android:layout_margin="20dp"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <android.support.v7.widget.RecyclerView
                android:id="@+id/recyclerview"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"/>

            <Button android:id="@+id/button_more"
                android:layout_marginTop="6dp"
                android:layout_marginBottom="6dp"
                android:layout_width="400px"
                android:layout_height="100px"
                android:layout_gravity="center"
                android:gravity="center"
                android:background="@drawable/button_dark"
                android:textColor="@drawable/button_dark_textcolor"/>
        </LinearLayout>
    </LinearLayout>
</ScrollView>
对于我的抽屉布局:

drawerlayout = (DrawerLayout)findViewById(R.id.drawer);
menu = (ListView)findViewById(R.id.menu);
MenuAdapter adapter = new MenuAdapter(
        this,
        R.layout.listview_menuitem,
        menuItemArray);
menu.setAdapter(adapter);
menu.setOnItemClickListener(new menuItemClickListener());

menuToggle =  new ActionBarDrawerToggle(
        this,
        drawerlayout,
        toolbar,
        R.string.app_name,
        R.string.app_name);
   drawerlayout.setDrawerListener(menuToggle);
   menuToggle.syncState();

哎呀,离开电脑和一杯咖啡的片刻之旅真是太神奇了

我只需将FrameLayout的
focusableInTouchMode
属性设置为
true

我想,当我在抽屉布局打开后触摸屏幕上的任何地方时,这就消除了RecyclerViews希望成为ScrollView中焦点元素的愿望。也许有更好、更彻底的解释。但我希望这能帮助其他可能遇到这个问题的人

    recyclerView = (RecyclerView)view.findViewById(R.id.recyclerview);
    LinearLayoutManager layoutManager = new LinearLayoutManager(getContext().getApplicationContext());
    recyclerView.setLayoutManager(layoutManager);
    recyclerView.setNestedScrollingEnabled(false);
drawerlayout = (DrawerLayout)findViewById(R.id.drawer);
menu = (ListView)findViewById(R.id.menu);
MenuAdapter adapter = new MenuAdapter(
        this,
        R.layout.listview_menuitem,
        menuItemArray);
menu.setAdapter(adapter);
menu.setOnItemClickListener(new menuItemClickListener());

menuToggle =  new ActionBarDrawerToggle(
        this,
        drawerlayout,
        toolbar,
        R.string.app_name,
        R.string.app_name);
   drawerlayout.setDrawerListener(menuToggle);
   menuToggle.syncState();