前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Android之 ScrollView和RecyclerView滑动冲突问题, GridView在NestedScrollView失去高度显示不全,

Android之 ScrollView和RecyclerView滑动冲突问题, GridView在NestedScrollView失去高度显示不全,

原创
作者头像
用户10521372
发布2023-05-26 11:36:23
1.4K0
发布2023-05-26 11:36:23
举报
文章被收录于专栏:android自用android自用

ScrollView和RecyclerView滑动冲突问题

方法1:我们可以把scrollview换成androidx.core.widget.NestedScrollView

代码语言:html
复制
<com.scwang.smartrefresh.layout.SmartRefreshLayout
    android:id="@+id/refreshLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:srlEnableFooterFollowWhenLoadFinished="true"
    app:srlEnableScrollContentWhenLoaded="true">
    
    <!-- 传统scrollview和RecyclerView滚动冲突 -->
    <androidx.core.widget.NestedScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/record_item_card"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:divider="@null"
            android:paddingHorizontal="5dp"
            android:visibility="visible" />
    
    </androidx.core.widget.NestedScrollView>
    
    <com.scwang.smartrefresh.layout.footer.ClassicsFooter
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:srlClassicsSpinnerStyle="Translate" />

</com.scwang.smartrefresh.layout.SmartRefreshLayout>

方法2:

代码语言:java
复制
kesanRv.setLayoutManager(new LinearLayoutManager(getActivity()) {
    @Override
    public boolean canScrollVertically() {
        return false;
    }
});

如果无法解决,在布局文件中的RecycleView的外部套一个RelativeLayout

GridView在NestedScrollView失去高度显示不全

在使用Android的ScrollView里面嵌套GridView时,设置android:layout_height="wrap_content"属性,运行界面的效果不会出现全部数据,即GridView会显示不全。 

建议:新建一个类继承GridView

代码语言:java
复制
public class MyGridView extends GridView {
    public MyGridView(Context context) {
        super(context);
    }

    public MyGridView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public MyGridView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public MyGridView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        //重写此方法
        int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST);
        super.onMeasure(widthMeasureSpec, expandSpec);
    }

}

xml文件:

代码语言:html
复制
<!-- 原生Gridview自带滚动条,继承此类重写方法去掉滚动 -->
<com.example.view.MyGridView
    android:id="@+id/menu_view"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:horizontalSpacing="@dimen/dp_10"
    android:verticalSpacing="@dimen/dp_10"
    android:numColumns="3"/>

之后就是适配器的事

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • ScrollView和RecyclerView滑动冲突问题
  • GridView在NestedScrollView失去高度显示不全
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档