2013-03-22 84 views
11

我遇到了ScrollView调整大小时非常烦人的问题,而且我将用尽可能的解决方案。ScrollView在儿童更改后不会直接调整大小

我有一个FragmentPager包含几个不同的片段,其中一个有一个ScrollView。 带有ScrollView的片段由一个Spinner和包含LinearLayout的ScrollView组成,其中包含几行其他视图(如SeekBars,Buttons,Edittexts)。 根据在Spinner中选择哪个选项,ScrollView显示不同的视图。要做到这一点,一些视图会将其可见性转换为“已过期”,而其他视图则转向“可见”。 这个效果很好,除了ScrollView在使用微调器选择不同的选项时似乎不能正确调整自身的大小。

当ScrollView充满视图并因此可滚动时,如果用户选择显示少于Viewsthan填充视口的选项,ScrollView仍会滚动。 当用户再次选择旧选项时,ScrollView现在无法滚动,因为它占用了上一个选项所需的大小。 当用户再次选择SAME选项时,ScrollView会突然滚动,因为它现在被调整为所需的实际大小。

这是怎么回事?更好的是,我该如何解决这个烦人的问题?

我的布局:

<?xml version="1.0" encoding="utf-8"?> 
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/control_scroll" 
    android:layout_width="fill_parent" 
    android:layout_height="0dp" 
    android:layout_weight="1" 
    android:background="#99000000" 
    android:fillViewport="true" > 

    <LinearLayout 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_marginBottom="20dp" 
     android:orientation="vertical" > 

     <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:background="#DD000000" 
      android:gravity="bottom" 
      android:orientation="vertical" > 

      <TextView 
       android:id="@+id/lamp_choose_tv" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:layout_marginBottom="2dp" 
       android:paddingLeft="5dp" 
       android:text="@string/choose_lamp_text" 
       android:textColor="#FFFFFF" 
       android:textSize="14sp" /> 
     </LinearLayout> 

     <Spinner 
      android:id="@+id/lamp_select_spinner" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_marginTop="5dp" 
      android:textColor="#FFFFFF" /> 

     <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:background="#DD000000" 
      android:gravity="bottom" 
      android:orientation="vertical" > 

      <TextView 
       android:id="@+id/lamp_settings_tv" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:layout_marginBottom="2dp" 
       android:background="#44000000" 
       android:paddingLeft="5dp" 
       android:text="@string/lamp_settings_text" 
       android:textColor="#FFFFFF" 
       android:textSize="14sp" /> 
     </LinearLayout> 
     <!-- Lamp name --> 

     <LinearLayout 
      android:id="@+id/naam_bar" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:gravity="bottom" 
      android:paddingBottom="3dp" 
      android:paddingLeft="10dp" 
      android:paddingRight="10dp" 
      android:paddingTop="3dp" > 

      <TextView 
       android:id="@+id/bridge_naam" 
       style="@style/ConfigText" 
       android:layout_width="0dp" 
       android:layout_height="wrap_content" 
       android:layout_weight="1" 
       android:text="@string/config_lightname" 
       android:textColor="#FFFFFF" 
       android:textSize="16sp" /> 

      <EditText 
       android:id="@+id/lamp_naam_input" 
       android:layout_width="0dp" 
       android:layout_height="wrap_content" 
       android:layout_weight="2" 
       android:background="@drawable/tasstextfield" 
       android:inputType="textNoSuggestions" 
       android:textColor="#FFFFFF" > 
      </EditText> 
     </LinearLayout> 

     <View 
      android:id="@+id/separator" 
      android:layout_width="fill_parent" 
      android:layout_height="0.5dp" 
      android:background="@android:color/black" 
      android:visibility="visible" /> 
<!-- Rest of the views --> 

事情我已经尝试过:

  • ForceLayout/requestLayout父滚动型
  • 无效滚动型
  • ForceLayout/requestLayout上含有的LinearLayout
  • 使LinearLayout无效
  • 无效的滚动型
  • ForceLayout/requestLayout的所有儿童上的滚动型
+0

您是否试过使用['ListView'](http://developer.android.com/guide/topics/ui/layout/listview.html)而不是构建似乎是您自己的列表视图?忽略该页面上的'Loader'示例 - 您可以简单地使用每段文本创建一个String []或List []并将其提供给['ArrayAdapter'](http://developer.android)。 com/reference/android/widget/ArrayAdapter.html),然后重写'getView'来控制外观。 – user113215 2013-03-26 12:41:26

+0

我明白你为什么这么想。实际上,我停止使用TextViews,因为它基本上只是用于测试目的,现在使用不同的视图,如Buttons,EditTexts和Seekbars。但问题仍然存在。 – 2013-03-26 14:39:03

+0

我相信问题是'ScrollView'需要调整自己的新'Views'。当你设置内部视图设置为'View.VISIBLE'时,在父ScrollView上调用'requestLayout()'或'forceLayout()'来安排布局传递。 – DeeV 2013-03-26 14:42:06

回答

4

这可能不是最愉快的解决方案,但它可能有效。 使用处理程序发布延迟的消息以第二次刷新视图,就好像用户在点击刷新按钮后选择两次相同的选项。

// The code when the user wants to update the views 
myButton.setOnClickListener(new OnClickListener{ 
    @Override 
    public void onClick(View view) { 
    updateView(); 
    Message m = Message.obtain(); 
    m.what = UPDATE_TAG; 
    mHandler.sendMessageDelayed(msg, 200); 
    } 
}) 

... 

final int UPDATE_TAG = 1; 

public void updateView() { 
    // Code where View.GONE and View.VISIBLE + invalidate is used. 
} 

Handler mHandler = new Handler { 
    @Override 
    public void handleMessage(Message input_msg) { 
    if(msg.what == UPDATE_TAG) { 
     updateView(); 
    } 
    } 
} 
+0

愉快吗?不,但它确实有效!非常感谢,希望这个答案也能帮助其他人。 – 2013-04-02 12:25:28

+0

我只需要更新视图中的可见性更改。我不必做任何失效。 – MinceMan 2013-07-01 19:55:49

+0

我仍然认为这个错误必须有更好的解决方案。 – MinceMan 2013-07-01 20:08:37

0

所有的孩子我想这LinearLayout使用某种类型的缓存机制,以避免测量孩子的每一次。或者,也许我错了。

但是,您可以尝试使用RelativeLayout而不是一堆LinearLayout。这将解决您的问题,而且这样会更有效率,因为嵌套的ViewGroup对性能不利。

0

您可以继续设置了滚动高度“WRAP_CONTENT”,这样即使孩子的看法改变了滚动可以调整到新的设置

+1

显然,我尝试过的第一件事。但是这并不能解决问题。 – 2013-04-02 07:43:29

1

试着用这改变

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/control_scroll" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 

android:background="#99000000" 
android:fillViewport="true" > 

退出android:layout_weight="1"

+0

我尝试的第一件事是wrap_content,但是这不能解决问题。 – 2013-04-02 07:44:00

相关问题