2017-08-24 32 views
1

我的recyclerview滾動是laggy。它在我開始通過觸摸Recyclerview進行滾動時滯後,但是從上面的視圖開始時,它不會滯後。 我也禁用了recyclerview上的nestedscrolling。Laggy RecyclerView

這裏是我的佈局:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_height="match_parent" 
    android:layout_width="match_parent" 
    xmlns:app="http://schemas.android.com/apk/res-auto"> 

    <android.support.v4.widget.NestedScrollView 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:scrollbars="vertical" 
     android:overScrollMode="never"> 

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

      <TextView 
       android:layout_width="match_parent" 
       android:layout_height="200dp" 
       android:text="HAHHAHA"/> 

      <android.support.v7.widget.RecyclerView 
       android:id="@+id/mRecyclerViewMain" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       app:layout_behavior="@string/appbar_scrolling_view_behavior" 
       android:background="@android:color/transparent"> 

      </android.support.v7.widget.RecyclerView> 


     </LinearLayout> 

    </android.support.v4.widget.NestedScrollView> 


</FrameLayout> 

這裏是一個video的滯後。這就像滾動跳過一些佈局。

+1

''setNestedScrollView = false' recyclerview –

+0

@PriteshVadhiya我在我的OnCreate()方法中將nestedscrolling設置爲false。 'mRecyclerViewMain.setNestedScrollingEnabled(false);' – Charlie

+1

您嘗試過[此解決方案](https://stackoverflow.com/a/40147626/8164071) –

回答

2

修改您這樣的XML:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
      xmlns:tools="http://schemas.android.com/tools" 
      android:layout_height="match_parent" 
      android:layout_width="match_parent" 
      xmlns:app="http://schemas.android.com/apk/res-auto"> 


        <android.support.v7.widget.RecyclerView 
         android:id="@+id/mRecyclerViewMain" 
         android:layout_width="match_parent" 
         android:layout_height="wrap_content" 
         app:layout_behavior="@string/appbar_scrolling_view_behavior" 
         android:background="@android:color/transparent"> 

        </android.support.v7.widget.RecyclerView> 

     </FrameLayout> 

,並設置在recyclerView的頭你的TextView,它將在設置viewType的標頭和項目清單

適配器類解決您的問題

<TextView 
       android:layout_width="match_parent" 
       android:layout_height="200dp" 
       android:text="HAHHAHA"/> 

@Override 
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 
     if (layoutInflater == null) 
      layoutInflater = LayoutInflater.from(parent.getContext()); 

     if (viewType == 0) { 
      // bind your headerview 
     } 
     if (viewType == 1) { 
      //bind your recyclerview 
     } 
    } 

    @Override 
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) { 
     if (holder instanceof HeaderHolder) 
      // viewHolder for header view 
     if (holder instanceof ListHolder) { 
      // viewholder for list 
     } 
    } 

使用這個你可以通過viewType

@Override 
    public int getItemViewType(int position) { 

     if (position == 0) 
      return 0; 
     else if (ArrayList.size() == 0) 
      return 1; 

    } 
+0

性能稍好一點通過設置.setHasFixedSize(true)在回收站 感謝 – Charlie

+1

這將給最佳性能 –