2016-03-02 5 views
1

В моей деятельности, я хочу, чтобы поместить слайдшоу выше мою страницу, а затем положить recycleview после этого, чтобы сделать так, I'v написал этот код в моем макете:андроид-recycleview внутри Scrollview

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="#fff" 
    android:orientation="vertical" 
    android:padding="6dp"> 


    <android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android" 
     xmlns:card_view="http://schemas.android.com/apk/res-auto" 
     xmlns:tools="http://schemas.android.com/tools" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:orientation="vertical" 
     card_view:cardBackgroundColor="#fff" 
     card_view:cardCornerRadius="3dp" 
     card_view:cardElevation="0dp"> 

     <cn.trinea.android.view.autoscrollviewpager.AutoScrollViewPager 
      android:id="@+id/pager" 
      android:layout_width="match_parent" 
      android:layout_height="250dp" /> 
    </android.support.v7.widget.CardView> 

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

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

в проблема с этим кодом заключается в том, что когда я запускаю операцию, только прокрутки recycleview, я не хочу этого, я хочу, если я прокручу вниз, вся страница прокручивается не только на переработку.

Как я могу сделать прокрутку всей страницы?

+0

Вложенные прокручиваемые виды не рекомендуется в андроиде. Почему вы не включаете свой 'AutoScrollViewPager' как часть вашего элемента' RecyclerView'? –

ответ

3

Три шага, чтобы сделать это:

  1. Заменить Scrollview с NestedScrollView поведением макета, установленным в app:layout_behavior="@string/appbar_scrolling_view_behavior"
  2. Храните все свои виджеты под линейной/относительную компоновкой (потому что вложенный вид прокрутки принимает только один ребенок) 3.Create CustomLinearLayoutManager.java с этим кодом:

    public class CustomLinearLayoutManager extends LinearLayoutManager { 
    
    public CustomLinearLayoutManager(Context context, int orientation, boolean reverseLayout) { 
        super(context, orientation, reverseLayout); 
    } 
    
    private int[] mMeasuredDimension = new int[2]; 
    
    @Override 
    public void onMeasure(RecyclerView.Recycler recycler, RecyclerView.State state, 
             int widthSpec, int heightSpec) { 
        final int widthMode = View.MeasureSpec.getMode(widthSpec); 
        final int heightMode = View.MeasureSpec.getMode(heightSpec); 
        final int widthSize = View.MeasureSpec.getSize(widthSpec); 
        final int heightSize = View.MeasureSpec.getSize(heightSpec); 
        int width = 0; 
        int height = 0; 
        for (int i = 0; i < getItemCount(); i++) { 
         measureScrapChild(recycler, i, 
           View.MeasureSpec.makeMeasureSpec(i, View.MeasureSpec.UNSPECIFIED), 
           View.MeasureSpec.makeMeasureSpec(i, View.MeasureSpec.UNSPECIFIED), 
           mMeasuredDimension); 
    
         if (getOrientation() == HORIZONTAL) { 
          width = width + mMeasuredDimension[0]; 
          if (i == 0) { 
           height = mMeasuredDimension[1]; 
          } 
         } else { 
          height = height + mMeasuredDimension[1]; 
          if (i == 0) { 
           width = mMeasuredDimension[0]; 
          } 
         } 
        } 
        switch (widthMode) { 
         case View.MeasureSpec.EXACTLY: 
          width = widthSize; 
         case View.MeasureSpec.AT_MOST: 
         case View.MeasureSpec.UNSPECIFIED: 
        } 
    
        switch (heightMode) { 
         case View.MeasureSpec.EXACTLY: 
          height = heightSize; 
         case View.MeasureSpec.AT_MOST: 
         case View.MeasureSpec.UNSPECIFIED: 
        } 
    
        setMeasuredDimension(width, height); 
    } 
    
    private void measureScrapChild(RecyclerView.Recycler recycler, int position, int widthSpec, 
               int heightSpec, int[] measuredDimension) { 
        View view = recycler.getViewForPosition(position); 
        if (view != null) { 
         RecyclerView.LayoutParams p = (RecyclerView.LayoutParams) view.getLayoutParams(); 
         int childWidthSpec = ViewGroup.getChildMeasureSpec(widthSpec, 
           getPaddingLeft() + getPaddingRight(), p.width); 
         int childHeightSpec = ViewGroup.getChildMeasureSpec(heightSpec, 
           getPaddingTop() + getPaddingBottom(), p.height); 
         view.measure(childWidthSpec, childHeightSpec); 
         measuredDimension[0] = view.getMeasuredWidth() + p.leftMargin + p.rightMargin; 
         measuredDimension[1] = view.getMeasuredHeight() + p.bottomMargin + p.topMargin; 
         recycler.recycleView(view); 
        } 
    } 
    

    }

комплект адаптер к вашему recyclerview так:

CustomLinearLayoutManager customLinearLayoutManager = new CustomLinearLayoutManager(this, LinearLayoutManager.VERTICAL, false); 
     updatesRecyclerView.setHasFixedSize(true); 
     updatesRecyclerView.setLayoutManager(customLinearLayoutManager); 
     updatesRecyclerView.setAdapter(yourAdapter); 
     updatesRecyclerView.setNestedScrollingEnabled(false); 

Вот именно, это работает.

+0

спасибо большое за ответ, еще одна вещь, я использую GridLayoutManager для моего recycleview, как я могу использовать gridlayout? –

+0

работал над этим кодом? – Manikanta

+0

GridLayoutManager расширяет LinearLayoutManager, я действительно не пробовал его с помощью gridlayoutmanager. в идеале незначительные изменения в коде выше должны работать и для gridlayoutmanager ... – Manikanta

Смежные вопросы