2016-10-02 115 views
0

Recyclerview不滾動滾動視圖。如果我刪除滾動視圖比它的光滑。我應該做些什麼來順利滾動recyclerview?RecyclerView不滾動滾動

<?xml version="1.0" encoding="utf-8"?> 
    <ScrollView 
     xmlns:android="http://schemas.android.com/apk/res/android" 
     xmlns:app="http://schemas.android.com/apk/res-auto" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"> 

     <RelativeLayout 
      android:id="@+id/content_activity_main" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      > 

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

      <LinearLayout 
       android:id="@+id/ll" 
       android:layout_width="match_parent" 
       android:layout_height="90dp" 
       android:layout_below="@+id/rv" 
       android:orientation="vertical"> 

       <ImageView 
        android:layout_width="50dp" 
        android:layout_height="50dp" 
        android:scaleType="fitXY" 
        /> 

       <TextView 
        android:id="@+id/textView35" 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:layout_marginTop="8dp" 
        /> 

      </LinearLayout> 
     </RelativeLayout> 
    </ScrollView> 

回答

1

你不應該把你的RecyclerView放在一個ScrollView中。如果您需要在RecyclerView的末尾顯示頁腳(即在您的RecyclerView的最後一項之後顯示一個視圖),那麼這也應該是RecyclerView的一部分。爲此,您只需在適配器中指定不同的項目類型並返回相應的ViewHolder。適配器的

private class ViewType { 
     public static final int NORMAL = 0; 
     public static final int FOOTER = 1; 
} 

然後,覆蓋getCount將(),並增加一個項目:

首先添加這在適配器

@Override 
public int getCount() { 
    return yourListsSize + 1; 
} 

接下來,你需要指定是哪個類型的是當前項目。爲了實現這一目標,覆蓋getItemViewType()適配器的:

@Override 
public int getItemViewType(int position) { 
    if(position == getCount() - 1) 
     return ViewType.FOOTER; 
    else 
     return ViewType.NORMAL; 
} 

最後,在onCreateViewHolder()檢查當前項目的類型和膨脹適當的視圖:

@Override 
public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) { 

    View rowView; 

    switch (viewType) { 
     case ViewType.NORMAL: 
      rowView=LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.normal, viewGroup, false); 
      break; 
     case ViewType.FOOTER: 
      rowView=LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.footer, viewGroup, false); 
      break; 
     default: 
      rowView=LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.normal, viewGroup, false); 
      break; 
    } 
    return new ViewHolder(rowView); 
} 

當然,您還需要將您的頁腳佈局移動到單獨的xml文件中,以便在此處使其膨脹。通過「頁腳佈局」,我指的是LinearLayoutandroid:id="@+id/ll"及其子視圖。

希望這會有所幫助。