LinearLayout布局参数不刷新

时间:2014-09-11 20:45:21

标签: android android-layout android-linearlayout parallax redraw

我遇到了罕见的行为,我无法弄清楚原因。

我实现了一个具有视差效果和自定义“on-pull-update”的渐弱动作栏。所有这些效果都与图像标题的高度和位置一起发挥,产生了视差和bouncinnes的感觉。

一切正常但是在几次播放之后,上下移动,图像标题的线性布局不会刷新o反映高度变化,即使我从事件中记录监听器和大小/位置/ alpha计算也能正常工作。 / p>

是否有一些东西丢失,阻止视图多次刷新?

自从昨天以来我就一直在努力争取这个问题,而且我真的会指出任何帮助或方向或任何指示。

提前致谢!

在我的主要课程下面,我从一个自定义的“ObservableActionBar”中继承,它只会覆盖onScroll函数,通过一个简单的界面添加一个监听器,它需要5个参数。

public class ActivityTest extends Activity{

private Drawable mActionBarBackgroundDrawable;

//PARALLAX
private float scale;

private boolean isGoingDownOnCero = false;

private float onDownY = 0;

private float distanceY = 0;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_activity_test);

    mActionBarBackgroundDrawable = getResources().getDrawable(R.drawable.ab_background);
    mActionBarBackgroundDrawable.setAlpha(0);

    getActionBar().setBackgroundDrawable(mActionBarBackgroundDrawable);

    scale = getBaseContext().getResources().getDisplayMetrics().density;

    ((ObservableScrollView) findViewById(R.id.scroll_test))
        .setOnTouchListener(new OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                isGoingDownOnCero = true;
                onDownY = event.getY();
                break;
            case MotionEvent.ACTION_UP:
                isGoingDownOnCero = false;
                ((View) findViewById(R.id.anchor)).setLayoutParams(
                    new RelativeLayout.LayoutParams(
                        LayoutParams.MATCH_PARENT,
                        (int)(255 * scale)
                    )
                );
                break;
            case MotionEvent.ACTION_MOVE :
                distanceY = (event.getY() - onDownY) / 2;
            default:
                break;
            }
            return false;
        }
    });

    ((ObservableScrollView) findViewById(R.id.scroll_test))
        .setScrollViewListener(new ScrollViewListener() {

            @Override
            public void onScrollChanged(ObservableScrollView scrollView, int x, int y,
                    int oldx, int oldy) {


                    int headerHeight = findViewById(R.id.image_header).getHeight() - getActionBar().getHeight();
                    float ratio = (float) Math.min(Math.max(y, 0), headerHeight) / headerHeight;
                    int newAlpha = (int) (ratio * 255);
                    mActionBarBackgroundDrawable.setAlpha(newAlpha);
                    getActionBar().setBackgroundDrawable(mActionBarBackgroundDrawable);

                    //findViewById(R.id.image_header).offsetTopAndBottom( - offset);

                    int cantRedrawSize = (int)(
                            ((int)(y * scale) / 2) < ( (int)(255 * scale) / 2) ? 
                                    (int)(y * scale) / 2 : 
                                    ((int)(255 * scale) / 2) );

                    Log.v("REDRAW SIZE", cantRedrawSize + "");
                    Log.v("Linear SIZE", ((View) findViewById(R.id.anchor)).getHeight() + "");

                    ((View) findViewById(R.id.anchor)).setLayoutParams(
                        new RelativeLayout.LayoutParams(
                            LayoutParams.MATCH_PARENT,
                            (255 * scale) - cantRedrawSize > 0 ? (int)((255 * scale) - cantRedrawSize) : 0 
                        )
                    );


                    if(isGoingDownOnCero && y == oldy && y == 0){
                        ((View) findViewById(R.id.anchor)).setLayoutParams(
                            new RelativeLayout.LayoutParams(
                                LayoutParams.MATCH_PARENT,
                                (int)(255 * scale) + ( distanceY > 0 ? distanceY < 200 ? (int)distanceY : 200 : 0)
                            )
                        );                  
                    }

                    ((View) findViewById(R.id.anchor)).requestLayout();


            }

        });     
} 

}

我的布局

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.noche.quepasoanoche.ActivityTest" >

<com.example.CLASS.ObservableScrollView
    android:id="@+id/scroll_test"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_weight="1" >

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >



           <LinearLayout
               android:background="#000"
                android:orientation="vertical"
                android:id="@+id/anchor"
                android:layout_width="match_parent"
                android:layout_height="255dp" >

               <ImageView
                android:id="@+id/image_header"
                android:layout_width="wrap_content"
                android:layout_height="400dp"
                android:adjustViewBounds="true"
                android:scaleType="centerCrop"
                android:src="@drawable/bg_home" />

            </LinearLayout>

            <LinearLayout
                android:id="@+id/item_lista"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:layout_below="@id/anchor"
                android:background="#000"
                android:orientation="vertical" >

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="50dp"
                    android:layout_margin="5dp"
                    android:background="#e4e4e4" >
                </LinearLayout>

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="50dp"
                    android:layout_margin="5dp"
                    android:background="#e4e4e4" >
                </LinearLayout>

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="50dp"
                    android:layout_margin="5dp"
                    android:background="#e4e4e4" >
                </LinearLayout>

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="50dp"
                    android:layout_margin="5dp"
                    android:background="#e4e4e4" >
                </LinearLayout>

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="50dp"
                    android:layout_margin="5dp"
                    android:background="#e4e4e4" >
                </LinearLayout>

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="50dp"
                    android:layout_margin="5dp"
                    android:background="#e4e4e4" >
                </LinearLayout>

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="50dp"
                    android:layout_margin="5dp"
                    android:background="#e4e4e4" >
                </LinearLayout>

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="50dp"
                    android:layout_margin="5dp"
                    android:background="#e4e4e4" >
                </LinearLayout>

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="50dp"
                    android:layout_margin="5dp"
                    android:background="#e4e4e4" >
                </LinearLayout>

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="50dp"
                    android:layout_margin="5dp"
                    android:background="#e4e4e4" >
                </LinearLayout>

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="50dp"
                    android:layout_margin="5dp"
                    android:background="#e4e4e4" >
                </LinearLayout>

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="50dp"
                    android:layout_margin="5dp"
                    android:background="#e4e4e4" >
                </LinearLayout>

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="50dp"
                    android:layout_margin="5dp"
                    android:background="#e4e4e4" >
                </LinearLayout>

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="50dp"
                    android:layout_margin="5dp"
                    android:background="#e4e4e4" >
                </LinearLayout>

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="50dp"
                    android:layout_margin="5dp"
                    android:background="#e4e4e4" >
                </LinearLayout>

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="50dp"
                    android:layout_margin="5dp"
                    android:background="#e4e4e4" >
                </LinearLayout>

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="50dp"
                    android:layout_margin="5dp"
                    android:background="#e4e4e4" >
                </LinearLayout>

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="50dp"
                    android:layout_margin="5dp"
                    android:background="#e4e4e4" >
                </LinearLayout>

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="50dp"
                    android:layout_margin="5dp"
                    android:background="#e4e4e4" >
                </LinearLayout>

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="50dp"
                    android:layout_margin="5dp"
                    android:background="#e4e4e4" >
                </LinearLayout>

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="50dp"
                    android:layout_margin="5dp"
                    android:background="#e4e4e4" >
                </LinearLayout>
            </LinearLayout>

        </RelativeLayout>

    </FrameLayout>
</com.example.CLASS.ObservableScrollView>

它看起来如何。

http://i.stack.imgur.com/o06eq.png

1 个答案:

答案 0 :(得分:0)

设置视图高度后,

((View) findViewById(R.id.anchor)).requestLayout();函数中调用此onTouch