首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >RecycleView inside NestedScrollView with match_parent

RecycleView inside NestedScrollView with match_parent
EN

Stack Overflow用户
提问于 2016-03-18 20:35:43
回答 1查看 687关注 0票数 0

我有一个自定义ScrollView的布局,可以关闭。有一些片段加载到此ScrollView中。

今天,我将RecycleView添加到新的片段中,并注意到一个奇怪的行为。当RecycleView拥有android:height="match_parent"时,它会在ScrollView中扩展到完全高度。

有没有办法禁用这个功能(我想让RecycleView在内部滚动,并设置为screen_height大小)?

主要内容xml (位于CoordinatorLayout内部)

代码语言:javascript
复制
<xxx.SwitchableScrollView
    android:id="@+id/main_scroll_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fillViewport="true">

    <LinearLayout
        android:id="@+id/fragment_holder"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:ignore="ScrollViewSize"/>

</xxx.SwitchableScrollView>

片段布局:

代码语言:javascript
复制
<FrameLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/appeals_list"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:visibility="visible"/>

    <LinearLayout
        android:id="@+id/loading_progress"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center">

        <ProgressBar
            android:layout_width="@dimen/big_progress_diameter"
            android:layout_height="@dimen/big_progress_diameter"
            android:indeterminate="true"
            android:indeterminateDrawable="@drawable/custom_progress_bar_primary"/>
    </LinearLayout>
</FrameLayout>

扩展ScrollView:

代码语言:javascript
复制
public class SwitchableScrollView extends ScrollView {
    boolean allowScroll = true;

    public boolean isAllowScroll() {
        return allowScroll;
    }

    public void setAllowScroll(boolean allowScroll) {
        this.allowScroll = allowScroll;
    }

    // .... constructors skipped

    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        return allowScroll && super.onTouchEvent(ev);
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        return allowScroll && super.onInterceptTouchEvent(ev);
    }
}

RecycleView初始化:

代码语言:javascript
复制
listView = (NestedRecyclerView) v.findViewById(R.id.appeals_list);
listView.setHorizontalScrollBarEnabled(false);
listView.setVerticalScrollBarEnabled(true);
listView.setNestedScrollingEnabled(true);
adapter = new AppealListAdapter(appealList);
LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
listView.setLayoutManager(layoutManager);
listView.setAdapter(adapter);

new GetAppeals().execute();

我使用的是支持库23.2.1 (也尝试了23.2.0)。ListView和GridView在另一个片段中工作得很好。

EN

回答 1

Stack Overflow用户

发布于 2016-03-18 21:46:53

我终于找到了答案。我花了6个小时(!)要将这一行添加到代码中:

代码语言:javascript
复制
layoutManager.setAutoMeasureEnabled(false);

他们应该是疯了才会默认启用这样的东西...

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36084565

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档