我希望我的应用的主页有不同的部分,其中包含每个部分中的项目列表。为此,我使用了ScrollView
。在scrollview中,我放置了一个搜索区域和一个ExpandableHeightGridView
(这是一个自定义类,可以帮助GridView
根据项目数扩展ScrollView
内的空间。
但无论如何,scrollView都不起作用,搜索框出现在屏幕顶部,同时GridView
滚动就好像它不在ScrollView中。
以下是我的代码:
activity_main.xml中
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/njoftime_background">
<ScrollView
android:id="@+id/sv"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/white"
android:layout_marginTop="?android:attr/actionBarSize"
android:orientation="vertical">
<RelativeLayout
android:id="@+id/sr"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:background="@drawable/event_background_block"
android:padding="5dp">
<RelativeLayout
android:id="@+id/search_area"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:focusableInTouchMode="true"
android:background="@drawable/njoftime_item_background">
<ImageButton
android:id="@+id/btn_search"
android:layout_width="65dp"
android:layout_height="35dp"
android:layout_alignParentRight="true"
android:layout_marginLeft="1dp"
android:background="@color/njoftime_main_color"
android:onClick="searchPressed"
android:scaleType="fitCenter"
android:src="@drawable/ic_search_white" />
<EditText
android:id="@+id/search_text"
android:layout_width="fill_parent"
android:layout_height="35dp"
android:layout_alignParentLeft="true"
android:layout_toLeftOf="@+id/btn_search"
android:background="@null"
android:ems="20"
android:imeOptions="actionSearch"
android:inputType="text"
android:maxLines="1"
android:paddingLeft="7dp"
android:textColor="@color/njoftime_desc"
android:textCursorDrawable="@null"
android:textSize="17sp" />
</RelativeLayout>
<RelativeLayout
android:id="@+id/search_categories_area"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/search_area"
android:paddingBottom="7dp"
android:paddingTop="13dp">
<GridView
android:id="@+id/search_grid"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:drawSelectorOnTop="false"
android:horizontalSpacing="2dp"
android:numColumns="4" />
</RelativeLayout>
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<com.example.crs.tempus.ExpandableHeightGridView
android:id="@+id/njoftime_list"
android:layout_width="match_parent"
android:horizontalSpacing="5dp"
android:verticalSpacing="5dp"
android:layout_marginTop="5dp"
android:layout_height="wrap_content"
android:background="@color/white"
android:drawSelectorOnTop="true"
android:numColumns="1" />
</RelativeLayout>
</LinearLayout>
</ScrollView>
<RelativeLayout
android:id="@+id/left_drawer"
android:layout_width="230dp"
android:layout_height="fill_parent"
android:layout_gravity="start"
android:layout_marginTop="?android:attr/actionBarSize"
android:background="@color/white"
android:orientation="vertical">
<ExpandableListView
android:id="@+id/list_slidermenu"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@color/white"
android:choiceMode="singleChoice"
android:dividerHeight="1dp"
android:drawSelectorOnTop="true"
android:groupIndicator="@null"
android:paddingBottom="13dp"
android:paddingLeft="7dp"
android:paddingRight="7dp"
android:paddingTop="13dp"></ExpandableListView>
</RelativeLayout>
</android.support.v4.widget.DrawerLayout>
ExpandableHeightGridView
public class ExpandableHeightGridView extends GridView
{
boolean expanded = false;
public ExpandableHeightGridView(Context context)
{
super(context);
}
public ExpandableHeightGridView(Context context, AttributeSet attrs)
{
super(context, attrs);
}
public ExpandableHeightGridView(Context context, AttributeSet attrs,
int defStyle)
{
super(context, attrs, defStyle);
}
public boolean isExpanded()
{
return expanded;
}
@Override
public void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
{
// HACK! TAKE THAT ANDROID!
if (isExpanded())
{
// 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();
}
else
{
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
}
public void setExpanded(boolean expanded)
{
this.expanded = expanded;
}
}
ExpandableHeightGridView也用于另一个活动,它可以工作。
有什么想法吗?
答案 0 :(得分:1)
1)在ExpandableHeightGridView
班级中使用onMeasure
覆盖方法中的以下代码。它适用于我的情况。
@Override
public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2,
MeasureSpec.AT_MOST);
super.onMeasure(widthMeasureSpec, expandSpec);
}
或
2)在您的代码中,您可能需要调用setExpanded
类的ExpandableHeightGridView
方法并将其设置为true
。
希望这个解决方案之一适合你。
答案 1 :(得分:0)
修订---------
尝试使用customScollview,像这样创建一个CustomScrollview并使用此
<CustomScrollView
android:fillviewport="true"
android:id="@+id/sv"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white">
机器人:fillviewport =&#34;真&#34;
在你的ScrollView中添加它,就像这样
t.Name != "BarController"