我在工具列中遇到了這種不需要的行為,工具fragment欄背景作業正常,但是當滾動到一個級別然后將方向從縱向模式更改為橫向模式時,背景顏色會丟失。橫向模式的滾動狀態與縱向模式相同。
如果切換回縱向,背景顏色仍然會丟失
當背景顏色丟失時,更改滾動狀態將帶回背景顏色。
問題是如何在更改設備方向后第一次滾動狀態更改時保留背景顏色?
public class VideoInfo extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
mTitleBar = (Toolbar) mRoot.findViewById(R.id.titlebar);
mTitleBar.addOnLayoutChangeListener(new View.OnLayoutChangeListener() {
@Override
public void onLayoutChange(View view, int i, int i1, int i2, int i3, int i4, int i5, int i6, int i7) {
updateHeaderHeight();
}
});
}
private void updateHeaderHeight() {
log.debug("updateHeaderHeight");
mHeaderHeight = mTitleBar.getMeasuredHeight();
if (mHeaderHeight == 0)
log.debug("Warning updateHeaderHeight sets mHeaderHeight to zero!");
if (mIsPortraitMode) {
View scrollView = mRoot.findViewById(R.id.scroll_content);
scrollView.setPadding(scrollView.getPaddingLeft(), mHeaderHeight, scrollView.getPaddingRight(), scrollView.getPaddingBottom());
}
}
@Override
public void onScrollChanged(int i, boolean b, boolean b1) {
updateHeaderBackground(i, true);
}
private void updateHeaderBackground(int scroll) {
mTitleBar.setBackgroundColor(mContext.getResources().getColor(R.color.deep_dark_blue_transparent));
}
}
<RelativeLayout 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"
xmlns:app="http://schemas.android.com/apk/res-auto">
<androidx.appcompat.widget.Toolbar
android:id="@ id/titlebar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:contentInsetLeft="0dp"
app:contentInsetStart="0dp"
android:layout_margin="0dp"
android:background="@android:color/transparent"
android:paddingLeft="0dip">
<!--text size is programmatically changed -->
<TextView
android:id="@ id/toolbar_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fontFamily="@font/lato_regular"
android:singleLine="true"
android:textColor="@color/white_font"
android:textSize="24sp"/>
</androidx.appcompat.widget.Toolbar>
</RelativeLayout>
uj5u.com熱心網友回復:
橫向模式的滾動狀態與縱向模式相同。
當更改配置發生時(在您的情況下是方向),在正常情況下,系統能夠保留一些東西,比如滾動狀態。
但是當滾動到一個級別然后將方向從縱向模式更改為橫向模式時,背景顏色會丟失。
但是無法保留其他內容,例如您在滾動后以編程方式更改的背景顏色ScrollView。
有不同的方法來維護片段中配置更改的資料;最雜草/最強大的使用ViewModels。
但在你的情況下,滾動值已經被保留;您可以在onConfigurationChanged那里覆寫并重置背景:
@Override
public void onConfigurationChanged(@NonNull Configuration newConfig) {
super.onConfigurationChanged(newConfig);
updateHeaderBackground(scrollView.getScrollY()); // Change this to scrollView.getScrollX() if you the scroll is horizontal
}
更新:
它沒有被擊中,改變方向時由于某種原因沒有呼叫該方法
這可能是android:configChanges="orientation"清單檔案活動部分中缺少的原因。
但是,我寧愿推薦另一種方法來解決它:
您可以在回呼onConfigurationChanged()中檢查 bundle 引數是否為空,而不是覆寫;onCreateView()對于配置更改,它不應該為空:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// CODE IS OMITTED
if (savedInstanceState != null) // It's not null
updateHeaderBackground(scrollView.getScrollY()); // Change this to scrollView.getScrollX() if you the scroll is horizontal
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/463368.html
