java - RecyclerView 在 DrawerLayout 关闭时获得焦点

标签 java android android-recyclerview drawerlayout

这是我发现的一个奇怪的错误。

但是如果我在 fragment 中有一个 RecyclerView,然后打开然后关闭我的 DrawerLayout,我的 RecyclerView 就会成为焦点。这意味着关闭我的 DrawerLayout 将导致 ScrollView 跳转到我的 RecyclerView。显然,我希望 ScrollView 的位置在 DrawerLayout 关闭时不会移动,并且绝对不会停留在我的 RecyclerView 上。


代码

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

<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 的 XML 设计是这样安排的:

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

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

            <include
                android:layout_height="wrap_content"
                android:layout_width="match_parent"
                layout="@layout/toolbar" />
        </LinearLayout>

        <FrameLayout
            android:id="@+id/contentframe"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    </LinearLayout>

    <RelativeLayout
        android:layout_width="300dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:background="@color/background_Secondary">

        <!-- Relative Layout : Back Button -->
        <RelativeLayout
            android:id="@+id/menu_backcontainer"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize">

            <ImageView
                android:id="@+id/menu_back"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginRight="20dp"
                android:src="@drawable/angle"
                android:layout_centerVertical="true"
                android:layout_alignParentEnd="true" />
        </RelativeLayout>

        <!-- List View : Menu Drawer Content -->
        <ListView
            android:id="@+id/menu"
            android:layout_below="@id/menu_backcontainer"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:choiceMode="singleChoice"
            android:background="@color/background_PrimaryDarkColor">
        </ListView>
    </RelativeLayout>
</android.support.v4.widget.DrawerLayout>

我不确定我还需要在此处包含哪些其他 Java 代码,但这是我实例化 RecyclerView 的方式:

    recyclerView = (RecyclerView)view.findViewById(R.id.recyclerview);
    LinearLayoutManager layoutManager = new LinearLayoutManager(getContext().getApplicationContext());
    recyclerView.setLayoutManager(layoutManager);
    recyclerView.setNestedScrollingEnabled(false);

对于我的 DrawerLayout:

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

我想当我在打开 DrawerLayout 后触摸屏幕上的任何地方时,这会使 RecyclerViews 成为 ScrollView 中的焦点元素的愿望无效。也许有更好、更彻底的解释。但我希望这对可能遇到此问题的任何其他人有所帮助。

关于java - RecyclerView 在 DrawerLayout 关闭时获得焦点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40414335/

相关文章:

android - Google Maps Lite Mode 导致 RecyclerView 卡顿

android - 具有多种布局的 RecyclerView

java - 使用递归删除链表中所有出现的元素

java - 在 JSP 中显示 Logo

java - 使用 StringTokenizer 递归解析字符串

Android测试,可视化触摸事件

java.lang.NoClassDefFoundError : org/apache/commons/vfs/FileSystemException

Android Http Post 正在下载错误的 SSL 证书

java - 流 mp3/asx/pls/m3u webradio android

android - FragmentManager replace() 似乎在 RecyclerView 中复制卡片