簡體   English   中英

LinearLayout 布局參數不刷新

[英]LinearLayout Layout Params not Refreshing

我遇到了一種罕見的行為,我不知道為什么。

我正在實現一個帶有視差效果和自定義“on-pull-update”的淡入淡出操作欄。 所有這些效果都與圖像標題的高度和位置有關,從而產生視差和彈跳的感覺。

一切正常,但經過幾次播放后,上下移動,圖像標題的線性布局不會刷新或反映高度變化,即使我從事件偵聽器中記錄並且大小/位置/alpha 計算工作正常。

是否有什么我缺少的東西可以防止視圖多次刷新?

在我的主類下面,這是一個自定義的“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>

以及它的外觀。

在此處輸入圖片說明

調用這個((View) findViewById(R.id.anchor)).requestLayout(); 在設置視圖高度后的onTouch函數中:

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM