滚动不在父布局中工作,仅适用于嵌套的RecyclerView?

时间:2016-09-23 07:08:33

标签: android android-fragments android-recyclerview scrollview android-coordinatorlayout

我遇到的问题是滚动仅适用于我的Fragment所在的RecyclerView。此Fragment位于TabLayout。问题是我在View之上有一个标题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"

2 个答案:

答案 0 :(得分:0)

RecyclerView是一个列表,因此无需任何操作即可滚动,如果您想在父级中滚动,则必须添加

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

....
</ScrollView>

答案 1 :(得分:0)

由于您使用下面的viewpager,如果viewpager中的所有观看次数都使用recyclverview,则会正常滚动。但是您在recyclerview上方添加了视图,因此视图不会滚动。我建议您使用带有视图的Recyclerview(recyclerview上方)作为recyclerview的标题。 我自定义recyclerview可以添加这样的标题,你只需要为headerview充气并将其添加到recyclerview(listview有headerview但是recyclelerview没有):

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;
        }
    }
}