0

我遇到问题,滚动只适用于我的Fragment,其中RecyclerView是。这Fragment被放置在TabLayout。问题是我的标题View高于RecyclerView,但是当我尝试在标题视图区域中向上或向下滚动时,布局不会滚动。但是,如果我滚动RecyclerView部分,则布局滚动。任何想法,为什么这可能发生?我的实现如下。任何帮助,将不胜感激。谢谢!滚动不能在父布局中工作,只适用于嵌套的RecyclerView?

activity_main.xml中:

<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:id="@+id/activity_main" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 
    <android.support.design.widget.AppBarLayout 
     android:id="@+id/app_bar" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content"> 
     <android.support.v7.widget.Toolbar 
      android:id="@+id/toolbar" 
      android:layout_width="match_parent" 
      android:layout_height="?attr/actionBarSize" 
      android:background="?attr/colorPrimary" 
      app:popupTheme="@style/AppTheme.PopupOverlay" 
      app:theme="@style/AppTheme.AppBarOverlay" 
      app:layout_scrollFlags="scroll|enterAlways" /> 
     <android.support.design.widget.TabLayout 
      android:id="@+id/tab_layout" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" 
      app:tabTextColor="@android:color/white" 
      app:tabSelectedTextColor="@android:color/white" 
      app:tabIndicatorColor="@android:color/white" 
      app:tabIndicatorHeight="3dp"/> 
    </android.support.design.widget.AppBarLayout> 
    <android.support.v4.view.ViewPager 
     android:id="@+id/pager" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     app:layout_behavior="@string/appbar_scrolling_view_behavior"/> 
    <android.support.design.widget.FloatingActionButton 
     android:id="@+id/fab" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="end|bottom" 
     android:layout_margin="@dimen/fab_margin" 
     android:src="@drawable/ic_place_white_24dp" 
     app:borderWidth="0dp" 
     app:layout_behavior="com.tabs.activity.ScrollingFabBehavior"/> 
</android.support.design.widget.CoordinatorLayout> 

fragment_three.xml:

<RelativeLayout 
    android:orientation="vertical" 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:fresco="http://schemas.android.com/tools" 
    android:background="@color/white" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content"> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:orientation="vertical" 
     android:gravity="center_horizontal" 
     android:id="@+id/profile_header"> 

     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:textSize="24sp" 
      android:textColor="@color/black" 
      android:layout_marginLeft="16dp" 
      android:layout_marginRight="16dp" 
      android:layout_marginTop="16dp" 
      android:id="@+id/profile_name"/> 

     <com.facebook.drawee.view.SimpleDraweeView 
      android:layout_marginTop="16dp" 
      android:id="@+id/profile_picture" 
      android:layout_width="100dp" 
      android:layout_height="100dp" 
      fresco:roundingBorderColor="@color/white" 
      fresco:roundingBorderWidth="10dp" 
      fresco:placeholderImageScaleType="centerCrop" 
      fresco:placeholderImage="@mipmap/blank_prof_pic" 
      fresco:roundAsCircle="true" 
      /> 

    </LinearLayout> 

    <LinearLayout 
     android:paddingTop="16dp" 
     android:id="@+id/button_row" 
     android:orientation="horizontal" 
     android:layout_width="match_parent" 
     android:weightSum="4" 
     android:layout_below="@id/profile_header" 
     android:layout_marginTop="16dp" 
     android:layout_height="wrap_content"> 
    ... 
    </LinearLayout> 

    <android.support.v7.widget.RecyclerView 
     android:layout_width="match_parent" 
     android:layout_below="@id/button_row" 
     android:layout_height="wrap_content" 
     android:id="@+id/rv_posts_feed" 
     android:visibility="gone" 
+0

包括''中activity_main.xml中fragment_three.xml' 'out of'' – Piyush

+0

使用NestedScrollView。参考:[http://stackoverflow.com/questions/30679476/how-do-i-use-both-scroll-view-and-recyclerview-in-one-xml-layout-android] –

回答

0

RecyclerView是一个列表,以便滚动可能没有什么也不做,如果你想在你的父母滚动,你必须添加

<ScrollView 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

.... 
</ScrollView> 
+0

包裹整个布局在一个'ScrollView'不起作用。事实上,它可以防止我的屏幕滚动。任何其他想法? – user1871869

+0

@ user1871869显示图像您想如何实现? – Piyush

+0

@Piyush我的意思是我只想让整个视图可以滚动。我认为在'ViewPager'中放置'app:layout_behavior =“@ string/appbar_scrolling_view_behavior”'已经可以让我把任何东西放到'ViewPager'中,在我的情况下,在里面放入'fragment_three.xml',应该使整个片段可滚动 – user1871869

0

因为您在下面使用viewpager,如果所有视图都在viewpager使用recyclverview,它会正常滚动。但你添加上面的视图recyclerview,所以视图不滚动。我建议您使用带有视图的回收站(recyclerview以上)作为recyclerview的标题。 我定制recyclerview可以这样添加标题,你只需要充气你headerview并把它添加到recyclerview(列表视图具有headerview但recyclerview没有):

public class Adapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> { 

    List<View> headers = new ArrayList<>(); 
    List<View> footers = new ArrayList<>(); 
    public static final int TYPE_HEADER = 111; 
    public static final int TYPE_FOOTER = 222; 
    public static final int TYPE_ITEM = 333; 

    private List<String> items; 
    private Activity context; 
    private LayoutInflater mInflater; 

    public FakeAdapter(List<String> items, Activity context) { 
     this.context = context; 
     mInflater = LayoutInflater.from(context); 
     if (items == null) { 
      throw new NullPointerException(
        "items must not be null"); 
     } 
     this.items = items; 
    } 



    @Override 
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 
     if (viewType == TYPE_ITEM) { 
      View itemView = LayoutInflater. 
        from(parent.getContext()). 
        inflate(R.layout.view_item_fake, 
          parent, 
          false); 
      return new QuestionHolder(itemView); 
     } else { 
      //create a new framelayout, or inflate from a resource 
      FrameLayout frameLayout = new FrameLayout(parent.getContext()); 
      //make sure it fills the space 
      frameLayout.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)); 
      return new HeaderFooterViewHolder(frameLayout); 
     } 
    } 

    @Override 
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) { 
     //check what type of view our position is 
     if (position < headers.size()) { 
      View v = headers.get(position); 
      //add our view to a header view and display it 
      bindHeaderFooter((HeaderFooterViewHolder) holder, v); 
     } else if (position >= headers.size() + items.size()) { 
      View v = footers.get(position - items.size() - headers.size()); 
      //add oru view to a footer view and display it 
      bindHeaderFooter((HeaderFooterViewHolder) holder, v); 
     } else { 
      //it's one of our items, display as required 
      bindHolder((QuestionHolder) holder, position - headers.size()); 
     } 
    } 

    private void bindHeaderFooter(HeaderFooterViewHolder vh, View view) { 
     //empty out our FrameLayout and replace with our header/footer 
     vh.base.removeAllViews(); 
     vh.base.addView(view); 
    } 

    private void bindHolder(final QuestionHolder holder, final int position) { 
     final String item = getItem(position); 
     if (item != null) { 
      holder.header_text.setText(item); 
     } 

    } 

    @Override 
    public int getItemCount() { 
     return headers.size() + items.size() + footers.size(); 
    } 

    public String getItem(int position) { 
     if (position < 0 || position >= items.size()) { 
      return null; 
     } 
     return items.get(position); 
    } 

    public final static class QuestionHolder extends RecyclerView.ViewHolder { 
     @Bind(R.id.header_text) 
     TextView header_text; 

     public QuestionHolder(View itemView) { 
      super(itemView); 
      ButterKnife.bind(this, itemView); 
     } 
    } 

    final static class HeaderHolder extends RecyclerView.ViewHolder { 
     //@Bind(R.id.header_merchant) 
     public TextView header; 

     public HeaderHolder(View itemView) { 
      super(itemView); 
      //ButterKnife.bind(this, itemView); 
      header = (TextView) itemView; 
     } 
    } 

    @Override 
    public int getItemViewType(int position) { 
     //check what type our position is, based on the assumption that the order is headers > items > footers 
     if (position < headers.size()) { 
      return TYPE_HEADER; 
     } else if (position >= headers.size() + items.size()) { 
      return TYPE_FOOTER; 
     } 
     return TYPE_ITEM; 
    } 

    //add a header to the adapter 
    public void addHeader(View header) { 
     if (header != null && !headers.contains(header)) { 
      headers.add(header); 
      //animate 
      notifyItemInserted(headers.size() - 1); 
     } 
    } 

    //remove a header from the adapter 
    public void removeHeader(View header) { 
     if (header != null && headers.contains(header)) { 
      //animate 
      notifyItemRemoved(headers.indexOf(header)); 
      headers.remove(header); 
      if (header.getParent() != null) { 
       ((ViewGroup) header.getParent()).removeView(header); 
      } 
     } 
    } 

    //add a footer to the adapter 
    public void addFooter(View footer) { 
     if (footer != null && !footers.contains(footer)) { 
      footers.add(footer); 
      //animate 
      notifyItemInserted(headers.size() + items.size() + footers.size() - 1); 
     } 
    } 

    //remove a footer from the adapter 
    public void removeFooter(View footer) { 
     if (footer != null && footers.contains(footer)) { 
      //animate 
      notifyItemRemoved(headers.size() + items.size() + footers.indexOf(footer)); 
      footers.remove(footer); 
      if (footer.getParent() != null) { 
       ((ViewGroup) footer.getParent()).removeView(footer); 
      } 
     } 
    } 

    //our header/footer RecyclerView.ViewHolder is just a FrameLayout 
    public static class HeaderFooterViewHolder extends RecyclerView.ViewHolder { 
     FrameLayout base; 

     public HeaderFooterViewHolder(View itemView) { 
      super(itemView); 
      this.base = (FrameLayout) itemView; 
     } 
    } 
}