translationY不会改变界限

时间:2015-03-05 22:55:12

标签: android android-layout android-linearlayout android-toolbar android-recyclerview

我尝试使用ObservableScroll library创建灵活的工具栏  遵循他wrote的代码,只是我想使用RecyclerView而不是ScrollView。

这里是布局:

<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=".MainActivity">

<FrameLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:elevation="4dp">

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

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="?attr/colorPrimary"
            android:minHeight="?attr/actionBarSize"/>

        <View
            android:id="@+id/vFlexible"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:background="?attr/colorPrimary" />
        </LinearLayout>

    <TextView
        android:id="@+id/title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingTop="16dp"
        android:paddingLeft="72dp"
        android:ellipsize="end"
        android:maxLines="1"
        android:layout_gravity="start"
        android:minHeight="?attr/actionBarSize"
        android:textColor="@android:color/white"
        android:textSize="20sp"
        android:text="test"/>
</FrameLayout>


<com.github.ksoichiro.android.observablescrollview.ObservableRecyclerView
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1"
    android:id="@+id/rv" />

以及代码:

package test.itayrabin.toolbartest;

import android.content.res.TypedArray;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.util.TypedValue;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;

import com.github.ksoichiro.android.observablescrollview.ObservableRecyclerView;
import com.github.ksoichiro.android.observablescrollview.ObservableScrollViewCallbacks;
import com.github.ksoichiro.android.observablescrollview.ScrollState;

 public class MainActivity extends ActionBarActivity implements ObservableScrollViewCallbacks {

private View vFlexibleSize;
private Toolbar mToolbar;
private TextView tvTitle;
private int flexibleSize;
public static final String TAG = "FlexibleTest";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mToolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(mToolbar);

    tvTitle = (TextView) findViewById(R.id.title);
    tvTitle.setText(getTitle());
    setTitle(null);

    final ObservableRecyclerView recyclerView = (ObservableRecyclerView) findViewById(R.id.rv);
    recyclerView.setLayoutManager(new LinearLayoutManager(this));

    recyclerView.setAdapter(new Adapter(this));
    recyclerView.setScrollViewCallbacks(this);

    flexibleSize = getResources().getDimensionPixelSize(R.dimen.flexible_space_height);
    int flexibleAndToolbar = flexibleSize + getActionBarSize();

    vFlexibleSize = findViewById(R.id.vFlexible);
    vFlexibleSize.getLayoutParams().height = flexibleAndToolbar;

    //recyclerView.setPadding(0,flexibleAndToolbar,0,0);

}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

@Override
public void onScrollChanged(int scrollY, boolean firstScroll, boolean dragging) {
    update(scrollY);
}

@Override
public void onDownMotionEvent() {}

@Override
public void onUpOrCancelMotionEvent(ScrollState scrollState) {}

protected int getActionBarSize() {
    TypedValue typedValue = new TypedValue();
    int[] textSizeAttr = new int[]{R.attr.actionBarSize};
    int indexOfAttrTextSize = 0;
    TypedArray a = obtainStyledAttributes(typedValue.data, textSizeAttr);
    int actionBarSize = a.getDimensionPixelSize(indexOfAttrTextSize, -1);
    a.recycle();
    return actionBarSize;
}

private void update(final int scrollY){
    vFlexibleSize.setTranslationY(-scrollY);

    int adjustedY = scrollY;
    if (scrollY<0){
        adjustedY = 0;
        Log.i(TAG,"Scroll Y <0 - " + scrollY);
    } else if (flexibleSize < scrollY){
        adjustedY = flexibleSize;
        Log.i(TAG,"scrollY is bigger than flexibleSize - " +scrollY);
    }

    float maxScale = (float) (flexibleSize - mToolbar.getHeight()) / mToolbar.getHeight();
    Log.i(TAG,"maxScale is " + maxScale);

    float scale = maxScale * ((float) flexibleSize - adjustedY) / flexibleSize;
    Log.i(TAG,"scale is " + scale);

    tvTitle.setPivotX(0);
    tvTitle.setPivotY(0);
    tvTitle.setScaleX(scale + 1);
    tvTitle.setScaleY(scale + 1);

}

这里基本上发生的是屏幕顶部有一个Frame布局,包括一个LinearLayout,它顶部有工具栏,下面有一个视图,当我们滚动时它会展开和折叠。下面我有recyclerView。

当更新方法中存在滚动时,我通过设置它的TranslationY来折叠flexibleView。

问题在于,当我滚动并且FlexibleView折叠了回收器的填充停留,或者LinearLayout没有改变它的界限时,或者某些事情使得recylcerview留在其中&#39 ; s位置,而不是坚持工具栏边框FRLayout。

有没有人有任何想法如何解决这个问题?

0 个答案:

没有答案