我有一个片段,在这个片段下面是一个容器(有时)包含一个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
发布评论