2017-10-05 99 views
0

我有描述一個TextView,另一個用於當前步驟「應用爲 食譜」當朝向改變

當我改變了設備的方向爲橫向,並返回到肖像模式再此問題發生(文字TextView的文本重疊在TextView來像另一個TextView不一樣,雖然有單個TextView和這種重疊發生)看到附加的圖像。

Normal view before changing orientation

This when i go landscape and returned to portrait again

xml文檔

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
xmlns:app="http://schemas.android.com/apk/res-auto" 
xmlns:tools="http://schemas.android.com/tools"> 

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

    <com.google.android.exoplayer2.ui.SimpleExoPlayerView 
     android:id="@+id/playerView" 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_weight="1" 
    /> 

    <ImageView 
     android:id="@+id/thumbImage" 
     android:layout_gravity="left" 
     android:paddingRight="15dp" 
     android:elevation="1dp" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" /> 

    <TextView 
     android:id="@+id/description" 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_weight="1" 
     android:padding="16dp" 
     android:textSize="17sp" 
     android:text="intro" 
     android:freezesText="true" 
     android:gravity="center_horizontal" 

     /> 


    <RelativeLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     > 

     <android.support.design.widget.FloatingActionButton 
      android:id="@+id/next_button" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_margin="@dimen/fab_margin" 
      android:src="@drawable/ic_next" 
      android:layout_alignParentRight="true" 
      app:fabSize="auto" /> 


     <android.support.design.widget.FloatingActionButton 
      android:id="@+id/back_button" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_margin="@dimen/fab_margin" 
      android:src="@drawable/ic_back" 
      app:fabSize="auto" /> 

     <TextView 
      android:id="@+id/currentStep" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_centerInParent="true" 
      android:text="1/10" 
      android:textSize="20sp"/> 


    </RelativeLayout> 

</LinearLayout> 

Java代碼:

public class StepDetailsFragment extends android.app.Fragment implements View.OnClickListener { 

ArrayList<Step> steps; 

StepsAdapter adapter; 

@BindView(R.id.description) 
TextView description; 
@BindView(R.id.currentStep) 
TextView current; 
@BindView(R.id.next_button) 
FloatingActionButton next; 
@BindView(R.id.back_button) 
FloatingActionButton back; 

FragmentOneListener listener; 

public int currentIndex; 
@BindView(R.id.playerView) 
SimpleExoPlayerView playerView; 
SimpleExoPlayer player; 

private boolean playWhenReady=true; 
private long playbackPosition; 
private int currentWindow; 


@Nullable 
@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
    View root = inflater.inflate(R.layout.details_fragment, container, false); 

    ButterKnife.bind(this, root); 

    Bundle bundle = getArguments(); 
    if (!bundle.containsKey("steps")) { 
     return root; 
    } 
    steps = bundle.getParcelableArrayList("steps"); 
    currentIndex = bundle.getInt("current"); 

    show(); 

    back.setOnClickListener(this); 
    next.setOnClickListener(this); 

    return root; 
} 

@Override 
public void onClick(View v) { 
    int id = v.getId(); 

    if (id == R.id.back_button) { 
     currentIndex--; 
     show(); 
    } else if (id == R.id.next_button) { 
     currentIndex++; 
     show(); 
    } 

} 


public void show() { 
    if (currentIndex <= 0) { 
     back.setVisibility(GONE); 
    } else { 
     back.setVisibility(View.VISIBLE); 
    } 
    if (listener != null) { 
     listener.setCurrent(currentIndex); 
    } 
    if (currentIndex >= steps.size() - 1) { 
     next.setVisibility(GONE); 
    } else { 
     next.setVisibility(View.VISIBLE); 
    } 

    description.setText(steps.get(currentIndex).getDescription()); 
    current.setText((currentIndex + 1) + "/" + steps.size()); 

} 
+0

您的活動在輪換時重新創建。片段管理器將原始片段存儲在實例狀態中,但我假設您正在活動的onCreate()中添加一個新片段。所以你有恢復和新的片段在彼此之上。如果savedInstanceState爲空,可能只需添加新片段。 –

+0

謝謝!這是行得通的,問題出在活動狀態。 –

+0

我將其添加爲答案 –

回答

0

ÿ我們的活動在輪換時重新創建。 片段管理器將實例狀態中的原始片段存儲起來,但我假設您在活動的onCreate()中添加了新的片段。 所以你有恢復和新的片段在彼此之上。

如果savedInstanceStatenull,也許只能添加一個新的片段。