我正在尝试获取 AppBarLayout 的布局参数,但我收到此错误。
java.lang.ClassCastException:android.support.design.widget.CoordinatorLayout $ LayoutParams无法强制转换为android.support.design.widget.AppBarLayout $ LayoutParams
activity_scrolling.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context=".ScrollingActivity">
<android.support.design.widget.AppBarLayout
android:id="@+id/app_bar"
android:layout_width="match_parent"
android:layout_height="@dimen/app_bar_height"
android:fitsSystemWindows="false"
android:theme="@style/AppTheme.AppBarOverlay">
<include layout="@layout/toolbar_scrollview"/>
</android.support.design.widget.AppBarLayout>
<include layout="@layout/content_scrolling" />
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="@dimen/fab_margin"
app:layout_anchor="@id/app_bar"
app:layout_anchorGravity="bottom|end"
app:srcCompat="@android:drawable/ic_dialog_email" />
</android.support.design.widget.CoordinatorLayout>
ScrollingActivity.java
public class ScrollingActivity extends AppCompatActivity implements
AppBarLayout.OnOffsetChangedListener{
ConstraintLayout constraintLayout;
AppBarLayout appBarLayout;
static boolean toolbarOpen = true;
int lastPosition = 0;
float toolbarThreshhold = 0.3f;
ConstraintSet constraintSet_openToolbar = new ConstraintSet();
ConstraintSet constraintSet_closeToolbar = new ConstraintSet();
@RequiresApi(api = Build.VERSION_CODES.M)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_scrolling);
/*Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);*/
appBarLayout = findViewById(R.id.app_bar);
constraintLayout = findViewById(R.id.constraint_toolbar_layout);
appBarLayout.addOnOffsetChangedListener(this);
FloatingActionButton fab = (FloatingActionButton)findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action",
Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
}
@Override
public void onAttachedToWindow() {
super.onAttachedToWindow();
constraintSet_openToolbar.clone(this, R.layout.toolbar_scrollview);
constraintSet_closeToolbar.clone(this,
R.layout.toolbar_scrollview_closed);
Log.d(ScrollingActivity.class.getSimpleName()
,"onAttachedToWindow()");
}
@Override
public void onOffsetChanged(AppBarLayout appBarLayout1, int verticalOffset) {
if (lastPosition == verticalOffset){
return;
}
Log.d(ScrollingActivity.class.getSimpleName() ,"onOffset Entry");
lastPosition = verticalOffset;
float progress = Math.abs(verticalOffset / (float)appBarLayout.getHeight());
Log.d(ScrollingActivity.class.getSimpleName() ,String.valueOf(lastPosition));
AppBarLayout.LayoutParams layoutParams = (AppBarLayout.LayoutParams) appBarLayout1.getLayoutParams();
layoutParams.topMargin = - verticalOffset;
appBarLayout.setLayoutParams(layoutParams);
if (toolbarOpen && progress > toolbarThreshhold){
constraintSet_closeToolbar.applyTo(constraintLayout);
toolbarOpen = false;
}
else if (toolbarOpen && progress < toolbarThreshhold){
constraintSet_openToolbar.applyTo(constraintLayout);
toolbarOpen = true;
}
}
}
我收到了这一行的错误(在 onOffsetChanged()内
AppBarLayout.LayoutParams layoutParams = (AppBarLayout.LayoutParams)
appBarLayout1.getLayoutParams();
答案 0 :(得分:2)
您需要使用LayoutParams
的{{1}},因为CoordinatorLayout
是布局xml中AppBarLayout
的孩子。
CoordinatorLayout