2017-04-04 101 views
0

我有一個活動,只有2個按鈕和一個FrameLayout裏:Android的動態片段

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal" 
    android:id="@+id/linearLayout"> 
    <Button 
     android:id="@+id/btPrincipal1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Ver Fragment 1" 
     android:layout_gravity="center_horizontal" 
     android:layout_weight="1" 
     /> 
    <Button 
     android:id="@+id/btPrincipal2" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Ver Fragment 2" 
     android:layout_gravity="center_horizontal" 
     android:layout_weight="1"/> 

</LinearLayout> 

<FrameLayout 
    android:layout_width="250dp" 
    android:layout_height="match_parent" 

    android:id="@+id/contenedor" 
    android:layout_alignParentBottom="true" 
    android:layout_alignParentEnd="true" 
    android:layout_below="@+id/linearLayout"> 


</FrameLayout> 

當我點擊按鈕1,我所說的片段,當我點擊按鈕2,我想打電話給同一個片段但具有不同的屬性。例如:

我點擊按鈕1,調用片段A,背景顏色爲綠色。

我點擊按鈕2,調用片段A,背景顏色爲紅色。

有可能嗎?我該怎麼做?謝謝。

+0

請參閱從答案下面的鏈接:http://stackoverflow.com/questions/ 17436298/how-to-pass-a-variable-from-activity-to-fragment-and-pass-it-back http://stackoverflow.com/questions/28829509/how-to-pass-arguments-to-fragment -from-activity –

+0

[如何將活動傳遞給Fragment並將其傳遞回來?](http://stackoverflow.com/questions/17436298/how-to-pass-a-variable-from-活動到片段,並通過它回來) – Gary99

回答

0

在你MainActivity,做到:

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    Button button1 = (Button) findViewById(R.id.btPrincipal1); 
    Button button2 = (Button) findViewById(R.id.btPrincipal2); 

    button1.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 
      FragmentManager supportFragmentManager = getSupportFragmentManager(); 
      Bundle bundle = new Bundle(); 
      bundle.putString(COLOR_PARAM, "green"); 
      supportFragmentManager.beginTransaction() 
        .replace(R.id.contenedor, YourFragment.newInstance("Button1")) 
        .commit(); 
     } 
    }); 

    button2.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 
      FragmentManager supportFragmentManager = getSupportFragmentManager(); 
      Bundle bundle = new Bundle(); 
      bundle.putString(COLOR_PARAM, "red"); 
      supportFragmentManager.beginTransaction() 
        .replace(R.id.contenedor, YourFragment.newInstance("Button2")) 
        .commit(); 
     } 
    }); 
} 

然後,在YourFragment.java,做到:

public class YourFragment extends Fragment { 

public static final String COLOR_PARAM = "param1"; 

private String currentColor; 

public YourFragment() {} 


public static Fragment newInstance(String param) { 
    Fragment fragment = new Fragment(); 
    Bundle args = new Bundle(); 
    args.putString(COLOR_PARAM, param); 
    fragment.setArguments(args); 
    return fragment; 
} 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    if (getArguments() != null) { 
     currentColor = getArguments().getString(COLOR_PARAM); 
    } 

} 

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
         Bundle savedInstanceState) { 
    View view = inflater.inflate(R.layout.your_fragment_layout, container, false); 
    FrameLayout frameLayout = (FrameLayout) view.findViewById(R.id.fragment_layout); 
    if (currentColor.equals("green")) { 
     frameLayout.setBackgroundColor(getActivity().getResources().getColor(android.R.color.holo_green_dark)); 
    } 
    else if (currentColor.equals("red")) { 
     frameLayout.setBackgroundColor(getActivity().getResources().getColor(android.R.color.holo_red_dark)); 
    } 

    return view; 
} 
} 
+0

謝謝你的回答!但現在工作,在MainActivity中,COLOR_PARAM是一個字符串? –

+0

在MainActivity中,COLOR_PARAM與片段相同。如果它們不在同一個包中,則必須執行YourFragment.COLOR_PARAM – Roger

0

如果你想改變背景只是你的片段的創建和對象,點擊按鈕後調用一個方法來改變片段的背景。但如果您想更改碎片佈局或其他內容,請創建代碼可讀性的新片段原因。

試試這個:

public class FragmentA extends Fragment { 
    public static FragmentA newInstance(int colorResId) { 
     FragmentA fragment = new FragmentA(); 
     Bundle bundle = new Bundle(); 
     bundle.putInt("colorId", colorResId); 
     fragment.setArguments(bundle); 
     return new FragmentA(); 
    } 

    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { 
     int resId = getArguments().getInt("colorId", 0); 
     if (resId != 0) { 
      //set background 
     } 
    } 

    public void changeBackground(int colorId){ 
     //set background 
    } 
}