在GridView的滚动上滚动整个屏幕(Scrolling the whole screen on GridView's scroll)

编程入门 行业动态 更新时间:2024-10-25 06:25:12
在GridView的滚动上滚动整个屏幕(Scrolling the whole screen on GridView's scroll)

我有一个片段,在这个片段下面是一个容器(有时)包含一个GridWiew。 如何在GridView的滚动条上滚动整个屏幕?

I have a fragment and below this fragment a container which (sometimes) contains a GridWiew. How can I scroll the whole screen on GridView’s scroll?

最满意答案

首先,您必须添加scrollView作为父级,它将是片段和gridView的父级。 在那种情况下,基于gridview的滚动内容不是理想的解决方案,因为它将导致2滚动。 相反,您将不得不禁用gridview的滚动并向gridview添加高度。这将删除回收功能。

固定高度的GridView

package com.mytestapp import android.util.AttributeSet; import android.view.ViewGroup; import android.widget.GridView; import android.content.Context; public class ExpandableHeightGridView extends GridView { ExpandableHeightGridView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } ExpandableHeightGridView(Context context) { super(context); } public ExpandableHeightGridView(Context context, AttributeSet attrs) { super(context, attrs); } @Override public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { // Calculate entire height by providing a very large height hint. // View.MEASURED_SIZE_MASK represents the largest height possible. int expandSpec = MeasureSpec.makeMeasureSpec(MEASURED_SIZE_MASK, MeasureSpec.AT_MOST); super.onMeasure(widthMeasureSpec, expandSpec); ViewGroup.LayoutParams params = getLayoutParams(); params.height = getMeasuredHeight(); } }

Firstly you will have to add scrollView as a parent which will be parent to both the fragment and gridView. In that scenario scrolling content based on gridview is not a desirable solution as it will result in 2 scroll . Instead you will have to disable scrolling of gridview and add height to gridview.This would though remove the recycling feature.

GridView of fixed height

package com.mytestapp import android.util.AttributeSet; import android.view.ViewGroup; import android.widget.GridView; import android.content.Context; public class ExpandableHeightGridView extends GridView { ExpandableHeightGridView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } ExpandableHeightGridView(Context context) { super(context); } public ExpandableHeightGridView(Context context, AttributeSet attrs) { super(context, attrs); } @Override public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { // Calculate entire height by providing a very large height hint. // View.MEASURED_SIZE_MASK represents the largest height possible. int expandSpec = MeasureSpec.makeMeasureSpec(MEASURED_SIZE_MASK, MeasureSpec.AT_MOST); super.onMeasure(widthMeasureSpec, expandSpec); ViewGroup.LayoutParams params = getLayoutParams(); params.height = getMeasuredHeight(); } }

更多推荐

GridWiew,GridView,滚动,电脑培训,计算机培训,IT培训"/> <meta name="descript

本文发布于:2023-04-29 06:35:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1335248.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:屏幕   GridView   Scrolling   scroll   screen

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!