2017-03-07 8 views
0

enter image description here新しいフラグメント完全

古い断片を置換これは私が私のイメージボタンをクリックした後、新しいフラグメントは、完全に古いフラグメントを交換しないでレイアウトされていない...

これは私の古いフラグメントメニューのJavaですコード

public class CMenu extends Fragment implements View.OnClickListener{ 
@Nullable 
@Override 
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup 
container, @Nullable Bundle savedInstanceState){ 

return inflater.inflate(R.layout.fragment_cmenu,container,false); 
} 

@Override 
public void onViewCreated(View view,@Nullable Bundle savedInstanceState){ 
    super.onViewCreated(view,savedInstanceState); 
    initView(view); 

} 
private void initView(View view){ 
    ImageButton prebtn = (ImageButton) view.findViewById(R.id.premium_cake); 
    prebtn.setOnClickListener(this); 
    ImageButton homemade = (ImageButton) view.findViewById(R.id.homemade); 
    homemade.setOnClickListener(this); 
    ImageButton sc_cake = (ImageButton) view.findViewById(R.id.sc_cake); 
    sc_cake.setOnClickListener(this); 
    ImageButton special_food = (ImageButton) 
    view.findViewById(R.id.special_food); 
    special_food.setOnClickListener(this); 
    ImageButton delicious_drink = (ImageButton) 
    view.findViewById(R.id.delicious_drink); 
    delicious_drink.setOnClickListener(this); 
    ImageButton other = (ImageButton) view.findViewById(R.id.other); 
    other.setOnClickListener(this); 

} 

@Override 
public void onClick(View view){ 
    switch (view.getId()){ 
     case R.id.premium_cake: 

getFragmentManager().beginTransaction().replace(R.id.MenuFragment,new 
PremiumCake()).addToBackStack(null).commit(); 
      break; 
     case R.id.homemade: 

getFragmentManager().beginTransaction().replace(R.id.MenuFragment,new 
HomeMade()).addToBackStack(null).commit(); 
      break; 
     case R.id.sc_cake: 

getFragmentManager().beginTransaction().replace(R.id.MenuFragment,new 
SpecialCake()).addToBackStack(null).commit(); 
      break; 
     case R.id.delicious_drink: 

getFragmentManager().beginTransaction().replace(R.id.MenuFragment,new 
DeliciousDrink()).addToBackStack(null).commit(); 
      break; 
     case R.id.special_food: 

getFragmentManager().beginTransaction().replace(R.id.MenuFragment,new 
SignatureFood()).addToBackStack(null).commit(); 
      break; 
     case R.id.other: 

getFragmentManager().beginTransaction().replace(R.id.MenuFragment,new 
Other()).addToBackStack(null).commit(); 
      break; 
    } 
} 

this is my new fragment java code 

public class Other extends Fragment { 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
} 

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
         Bundle savedInstanceState) { 
    // Inflate the layout for this fragment 
    return inflater.inflate(R.layout.other, container, false); 
} 

古いフラグメントのxml

<ScrollView 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent"> 

<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:background="?android:windowBackground" 
    android:layout_height="wrap_content" 
    tools:context=".MenuFragment" 
    tools:ignore="ContentDescription" 
    android:id="@+id/MenuFragment"> 

    <ImageButton 
     android:layout_width="match_parent" 
     android:id="@+id/sc_cake" 
     android:background="@drawable/sc_bg" 
     android:layout_height="120dp" 
     android:layout_below="@+id/homemade" 
     android:layout_alignParentStart="true" 
     /> 

    <ImageButton 
     android:layout_width="match_parent" 
     android:id="@+id/premium_cake" 
     android:background="@drawable/pc_bg" 
     android:layout_height="120dp" 
     android:layout_alignParentTop="true" 
     android:layout_alignParentEnd="true" 
     android:layout_marginTop="50dp" /> 

    <ImageButton 
     android:layout_width="match_parent" 
     android:id="@+id/homemade" 
     android:background="@drawable/hmc_bg" 
     android:layout_height="120dp" 
     android:layout_below="@+id/premium_cake" 
     android:layout_alignParentStart="true" /> 

    <ImageButton 
     android:layout_width="match_parent" 
     android:id="@+id/delicious_drink" 
     android:background="@drawable/dd_bg" 
     android:layout_height="120dp" 
     android:layout_below="@+id/special_food" 
     android:layout_alignParentStart="true" /> 

    <ImageButton 
     android:layout_width="match_parent" 
     android:id="@+id/other" 
     android:background="@drawable/other_bg" 
     android:layout_height="120dp" 
     android:layout_below="@+id/delicious_drink" 
     android:layout_alignParentStart="true" 
     android:layout_marginBottom="50dp" /> 

    <ImageButton 
     android:layout_width="match_parent" 
     android:id="@+id/special_food" 
     android:background="@drawable/sf_bg" 
     android:layout_height="120dp" 
     android:layout_below="@+id/sc_cake" 
     android:layout_alignParentStart="true" /> 

    </RelativeLayout> 
</ScrollView> 
(私はこの上のスクロールビューを使用しています)

新しいフラグメントのXML

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:app="http://schemas.android.com/apk/res-auto" 
android:layout_width="match_parent" 
android:layout_height="wrap_content" 
android:layout_alignParentTop="true"> 

<GridLayout 
    android:orientation="vertical" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:weightSum="1" 
    android:background="#2e3192"> 
    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_marginTop="50dp"> 

     <Button 
      android:id="@+id/cb_big" 
      android:layout_width="0dp" 
      android:layout_weight="1" 
      android:layout_height="180dp" 
      android:background="@drawable/coolerbag_big" /> 

     <Button 
      android:id="@+id/cb_small" 
      android:layout_width="0dp" 
      android:layout_weight="1" 
      android:layout_height="180dp" 
      android:background="@drawable/coolerbag_small" /> 


    </LinearLayout> 
</GridLayout> 
</ScrollView> 
+0

フラグメント –

+0

を置き換えるために書き込んだコードを入力してください。これは私がstrackoverflowの初めての投稿であるためご迷惑をおかけして申し訳ありません。コードは私のポストで更新されました – JsLaw

+0

フラグメント自体をコンテナとして渡した場合、そのフラグメントを含むレイアウトのIDが必要です。以下のコードをアップロードします。 –

答えて

0

方法を置き換える

<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/activity_main" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" 
    android:background="@android:color/black" 
    tools:context=".MainActivity"> 
<FrameLayout 
      android:id="@+id/container" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent"/> 
    </LinearLayout> 

が コンテナにあったし、追加以前のフラグメントを削除し、古いフラグメントが含まれている活動のXML新しいもの

OldFragment fragment = new OldFragment(); 
     FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction(); 
     fragmentTransaction.replace(R.id.container, fragment); 
     fragmentTransaction.commit(); 

をだから今、画像がクリックされたときに新しいものと古いフラグメントを置き換えるために、代わりに:だから、あなたが最初に古いフラグメントをロードしているあなたのActivityクラスでは、あなたが行っている必要があります

getFragmentManager().beginTransaction().replace(R.id.MenuFragment,new 
PremiumCake()).addToBackStack(null).commit(); 

だけで行います:の

getFragmentManager().beginTransaction().replace(R.id.container,new 
    PremiumCake()).addToBackStack(null).commit(); 

コンテナはアクティビティのXML内のフレームレイアウトで、古いフラグメントを で保持していて、それを新しいフラグメントで置き換えます。

+0

それは仕事です!どうもありがとうございました!!! – JsLaw

+0

お手伝いします:) –

関連する問題