1. 程式人生 > >NetstedScrollView RecyclerView滑動中出現問題記錄

NetstedScrollView RecyclerView滑動中出現問題記錄

1. RecyclerView 不能fling,滑動不順暢

解決辦法:

        ViewCompat.setNestedScrollingEnabled(recyclerView, false);  
2.RecyclerView 在notifyDataSetChanged()後自動滾動的問題
       <android.support.v4.widget.NestedScrollView
                android:id="@+id/scrollView"
                android:layout_width="match_parent"
                android:layout_height="match_parent">

                <LinearLayout
                    android:id="@+id/ll_scroll_container"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:descendantFocusability="blocksDescendants"
                    android:orientation="vertical">

                    <TextView
                        android:id="@+id/detail_tip"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:paddingLeft="15dp"
                        android:paddingTop="20dp"
                        android:paddingRight="15dp"
                        android:paddingBottom="20dp"
                        android:textSize="13sp" />

                    <android.support.v7.widget.RecyclerView
                        android:id="@+id/recyclerView"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:divider="#00000000"
                        android:dividerHeight="0dip"
                        android:overScrollMode="never"
                        android:listSelector="@android:color/transparent"
                        android:scrollbars="none" />
                </LinearLayout>
            </android.support.v4.widget.NestedScrollView>

如上程式碼,會出現在noifyDataSetChanged()之後,自動定位到RecyclerView的頂部,上方的detail_tip TextView 沒有在螢幕中展示。 上圖中的程式碼問題已經解決。
解決辦法是添加了:

			//新增到RecyclerView的父佈局中
                   android:descendantFocusability="blocksDescendants"
                   //新增到RecyclerView中
                   android:overScrollMode="never"

解釋:

android:descendantFocusability :Defines the relationship between the ViewGroup and its descendants when looking for a View to take focus.
翻譯:定義ViewGroup和其子View的關係來確定需要一個View獲取焦點時,哪個View來獲取當前的焦點。