Android 翻译不';不要改变界限
我正在尝试使用ObservableScroll创建一个灵活的工具栏 按照他所说的代码,我只想使用RecyclerView而不是ScrollView 布局如下:Android 翻译不';不要改变界限,android,android-layout,android-linearlayout,android-toolbar,android-recyclerview,Android,Android Layout,Android Linearlayout,Android Toolbar,Android Recyclerview,我正在尝试使用ObservableScroll创建一个灵活的工具栏 按照他所说的代码,我只想使用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:lay
<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);
}
package test.itayrabin.test;
导入android.content.res.TypedArray;
导入android.support.v7.app.ActionBarActivity;
导入android.os.Bundle;
导入android.support.v7.widget.LinearLayoutManager;
导入android.support.v7.widget.Toolbar;
导入android.util.Log;
导入android.util.TypedValue;
导入android.view.Menu;
导入android.view.MenuItem;
导入android.view.view;
导入android.widget.TextView;
导入com.github.ksoichiro.android.observeScrollView.observeRecyclerView;
导入com.github.ksoichiro.android.observatescrollview.observatescrollview回调;
导入com.github.ksoichiro.android.observeScrollView.ScrollState;
公共类MainActivity扩展了ActionBarActivity实现了ObservableScrollViewCallbacks{
私有视图vFlexibleSize;
私有工具栏mToolbar;
私有文本视图tvTitle;
私人内部灵活尺寸;
公共静态最终字符串标记=“FlexibleTest”;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mToolbar=(工具栏)findviewbyd(R.id.Toolbar);
设置支持操作栏(mToolbar);
tvTitle=(TextView)findViewById(R.id.title);
setText(getTitle());
setTitle(空);
最终ObservereCyclerView再循环视图=(ObservereCyclerView)findViewById(R.id.rv);
recyclerView.setLayoutManager(新的LinearLayoutManager(本));
setAdapter(新适配器(this));
recyclerView.setScrollViewCallbacks(此);
flexibleSize=getResources().getDimensionPixelSize(R.dimen.flexible\u space\u height);
int flexibleAndToolbar=flexibleSize+getActionBarSize();
vFlexibleSize=findViewById(R.id.vFlexible);
vFlexibleSize.getLayoutParams().height=flexibleAndToolbar;
//设置填充(0,FlexableAndToolbar,0,0);
}
@凌驾
公共布尔onCreateOptions菜单(菜单){
//为菜单充气;这会将项目添加到操作栏(如果存在)。
getMenuInflater().充气(右菜单菜单菜单主菜单);
返回true;
}
@凌驾
公共布尔值onOptionsItemSelected(菜单项项){
//处理操作栏项目单击此处。操作栏将
//自动处理Home/Up按钮上的点击,只要
//在AndroidManifest.xml中指定父活动时。
int id=item.getItemId();
//noinspection SimplifiableIf语句
if(id==R.id.action\u设置){
返回true;
}
返回super.onOptionsItemSelected(项目);
}
@凌驾
public void onScrollChanged(整型滚动、布尔型第一滚动、布尔型拖动){
更新(滚动);
}
@凌驾
public void onDownMotionEvent(){}
@凌驾
public void onProcencelMotionEvent(ScrollState ScrollState){}
受保护的int getActionBarSize(){
TypedValue TypedValue=新的TypedValue();
int[]textSizeAttr=newint[]{R.attr.actionBarSize};
int indexofattertextsize=0;
TypedArray a=获得的StyledAttributes(typedValue.data,textSizeAttr);
int actionBarSize=a.getDimensionPixelSize(indexOfAttrTextSize,-1);
a、 回收();
返回操作条大小;
}
私有无效更新(最终整数滚动){
vFlexibleSize.setTranslationY(-scrollY);
int adjustedY=滚动;
如果(滚动