2014-04-15 4 views
0

나는 다음 코드를 사용하여이 fragment_settings.xml에게이미 일부보기가있는 조각을 바꾸는 방법은 무엇입니까?

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/linearlayout_settings_fragment" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" > 

    <TextView 
     android:id="@+id/textview_my_account" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:text="My Account" /> 

    <TextView 
     android:id="@+id/textview_my_account_1" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:text="List" /> 

</LinearLayout> 

있습니다

public class SettingsFragment extends Fragment implements Constants, OnClickListener{ 

    public static SettingsFragment newInstance() { 
     SettingsFragment f = new SettingsFragment(); 
     return f; 
    } 

    @Override 
    public View onCreateView(LayoutInflater inflater, final ViewGroup container, 
      Bundle savedInstanceState) { 
     container.setPadding(0, 0, 0, 0); 
     final View view = inflater.inflate(R.layout.fragment_settings, container, 
       false); 
     initViews(view); 
     return view; 
    } 

    private void initViews(View view) { 
     // TODO Auto-generated method stub 
     TextView textView = (TextView)view.findViewById(R.id.textview_my_account); 
     textView.setOnClickListener(this); 
     TextView textView1 = (TextView)view.findViewById(R.id.textview_my_account_1); 
     textView1.setOnClickListener(this); 
    } 

    @Override 
    public void onClick(View v) { 
     // TODO Auto-generated method stub 
     FragmentManager fragmentManager = getChildFragmentManager(); 
     FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); 

     switch (v.getId()) { 
     case R.id.textview_my_account: 
      SettingsContactDetailFragment settingsContactDetailFragment = SettingsContactDetailFragment.newInstance(); 
      settingsContactDetailFragment.setRetainInstance(true); 
      fragmentTransaction.replace(R.id.linearlayout_settings_fragment, settingsContactDetailFragment); 
      fragmentTransaction.addToBackStack(FragmentEnum.SETTINGS_CONTACT_DETAIL_FRAGMENT); 
      fragmentTransaction.commit(); 
      break; 

     case R.id.textview_my_account_1: 
      SettingsContactListFragment settingsContactListFragment = SettingsContactListFragment.newInstance(); 
      settingsContactListFragment.setRetainInstance(true); 
      fragmentTransaction.replace(R.id.linearlayout_settings_fragment, settingsContactListFragment); 
      fragmentTransaction.addToBackStack(FragmentEnum.SETTINGS_CONTACT_LIST_FRAGMENT); 
      fragmentTransaction.commit(); 
      break; 
     } 
    } 
} 

내가이 작업을 시도했다. 내가 fragmentTransaction.replace를 사용할 때는 조각 fragment_settings.xml

LinearLayout linearLayout = (LinearLayout)view.findViewById(R.id.linearlayout_settings_fragment); 
linearLayout.removeAllViews(); 

이미하지만 왜 작동하지 이하로 전망 추가?

fragmentTransaction.replace(R.id.linearlayout_settings_fragment, settingsContactListFragment); 

답변

2

FragmentTransaction.replace 방법은 용기에서 단지 이전에 추가의 조각의 뷰를 제거한다. 컨테이너에 속한 모든보기를 제거하지는 않습니다. 그것이 다른 2 TextViews에 추가되는 이유입니다. 당신이 TextView을 바꾸려면

, 그들은 처음과 나중 R.id.linearlayout_settings_fragment에 추가 SettingsContactDetailFragment 또는 SettingsContactListFragment 조각으로 대체 할 수있는 다른 Fragment의 일부이어야한다.

+0

감사합니다. 따라서이 두 textview는 ** replace ** 메소드에 의해 대체/삭제되어서는 안되는 다른 조각의 일부로 간주됩니다. 조각에이 두 개의보기를 추가하고 다른 조각으로 바꿔야합니다. –

관련 문제