How to remove focus from RecyclerView inside ScrollView?

前端 未结 5 1302
傲寒
傲寒 2020-12-05 07:37

I have a Scrollview which contains an ImageView and RecyclerView. if navigation drawer opened then closed the RecyclerView auto scrolling to top, How to stop this?



        
相关标签:
5条回答
  • 2020-12-05 08:17

    That is because RecyclerView ALWAYS set:

    setFocusableInTouchMode(true);
    

    this is hard coded in its constructor.

    so if you apply those attribute in your XML for a RecyclerView

    android:focusable="false"
    android:focudeableInTouchMode="false"
    

    that would be useless, you end up with recycleView.isFocusable() == true...

    so the most elegant solution is to disable foucusable for RecyclerView's Parent.

    <LinearLayout
        android:focusable="false"
        android:focusableInTouchMode="false"
        android:descendantFocusability="blocksDescendants"
        ...
        >
        <android.support.v7.widget.RecyclerView
            ...
        />
    />
    

    or you can just simply setFocusable(false)

    0 讨论(0)
  • 2020-12-05 08:20

    Adding android:descendantFocusability="blocksDescendants" can restrict scroll to recylerview but if you have edittext inside your layout, this property can block the focus of that particular edittext too. So if you are using this property please make sure you are removing this property in your kotlin/java class once the layout loaded.

    parentLayout?.descendantFocusability = FOCUS_BEFORE_DESCENDANTS (view as ViewGroup).descendantFocusability = FOCUS_BEFORE_DESCENDANTS

    0 讨论(0)
  • 2020-12-05 08:29

    This problem because of recyclerView has default focus.

    Solution

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

    Add android:descendantFocusability="blocksDescendants" to your immediate layout of scrollView

    0 讨论(0)
  • 2020-12-05 08:29

    Just add the following code in your linear layout, works 100% ` android:descendantFocusability="blocksDescendants"

    <android.support.v4.widget.NestedScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>
    
        <LinearLayout
            android:descendantFocusability="blocksDescendants"
            android:layout_width="match_parent"
            android:layout_height="match_parent">
    
    0 讨论(0)
  • 2020-12-05 08:33

    Check out clearFocus() from here Android Dev Doc.

    You can set a DrawerListener to your navigation drawer and use the onDrawerStateChanged() or some of the other options from here to call clearFocus() on your RecyclerView.

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