2015-12-03 8 views
-1

Я пытаюсь создать Swappable fragements на моем андроид приложенияЗамена фрагментов по щелчку кнопки андроида

только половина части фрагмента кажется, заменив
скриншот:

screenshot

xml файл для основного является

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:app="http://schemas.android.com/apk/res-auto" 
xmlns:tools="http://schemas.android.com/tools" 
android:orientation="vertical" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
app:layout_behavior="@string/appbar_scrolling_view_behavior" 
tools:showIn="@layout/app_bar_main" 
tools:context=".MainActivity" 
android:id="@+id/contentlayout"> 
<fragment 
    android:layout_width="match_parent" 
    android:layout_height="60dp" 
    android:id="@+id/header_menufragment" 
    class="com.example.www.newapp.header_menu" /> 
<fragment 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:id="@+id/dynamicfragment" 
    class="com.example.www.newapp.mainfragment"/> 

</LinearLayout> 

всего dynamicfragment предполагается заменить на projectfragment нажатием кнопки «Проект».

project.setOnClickListener(new View.OnClickListener() { 
      @Override 
     public void onClick(View v) { 
       getFragmentManager() 
         .beginTransaction() 
         .replace(R.id.dynamicfragment, new projectfragment()) 
         .addToBackStack(null) 
         .commit(); 
     } 
    }); 

projectfragment класс имеет

public class projectfragment extends Fragment { 
@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) { 
return inflater.inflate(R.layout.fragment_project, container,false); 
    } 
    } 

файл макета

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:orientation="vertical" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:paddingLeft="15dp" 
android:paddingRight="15dp" 
android:paddingBottom="10dp" 
android:paddingTop="10dp" 
android:background="@color/colorDarkRed"> 

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="horizontal" 
    android:layout_weight=".5"> 
    <ImageButton 
     android:id="@+id/img1" 
     android:layout_width="wrap_content" 
     android:layout_height="match_parent" 
     android:layout_weight="1" /> 
    <ImageButton 
     android:id="@+id/img2" 
     android:layout_width="wrap_content" 
     android:layout_height="match_parent" 
     android:layout_weight="1" /> 
</LinearLayout> 

<ImageButton 
    android:id="@+id/img3" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:layout_weight="1" 
    android:src="@drawable/imgfile"/> 


</LinearLayout> 

Пожалуйста, помогите мне решить эту проблему.

+0

Это не совсем ясно, что он заменяет половину фрагмента, вы можете поделиться макеты двух фрагментов? – himanshu1496

+0

yes Я обновил файл, который вы задали –

+0

Насколько мне известно, нельзя заменить фрагмент, определенный статически в файле макета. удивлен тем, что видел этот результат .. !!!! –

ответ

0

Вы не можете заменить фрагмент, определенный статически в файле макета. Вы можете заменять только фрагменты, добавленные динамически через FragmentTransaction.

Использовать FrameLayout в качестве контейнера и заменить Fragment внутри этого.

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

    <FrameLayout android:id="@+id/fragment_container" android:layout_weight="1" 
      android:layout_width="match_parent" android:layout_height="match_parent" /> 

</LinearLayout> 

На операцию добавления фрагмента же, как и вы:

getFragmentManager() 
    .beginTransaction() 
    .replace(R.id.fragment_container, new projectfragment()) 
    .addToBackStack(null) 
    .commit(); 

Для получения дополнительной информации см ниже ссылке:
link1
link2

+0

благодарит за ответ, я постараюсь как можно скорее и дам вам знать –

+0

Спасибо, это сработало как шарм :) –

0

, что работал для меня попробовать этот способ

WishaFriend ff = new WishaFriend(); 

           android.app.FragmentManager fm = getActivity().getFragmentManager(); 
           android.app.FragmentTransaction ft = fm.beginTransaction(); 
           ft.replace(((ViewGroup) getView().getParent()).getId(), ff); 
           ft.commit(); 
           dialog.dismiss(); 

попробуйте добавить этот

ft.replace(((ViewGroup) getView().getParent()).getId(), ff); 
+0

спасибо за ответ, другой связанный помог мне –

Смежные вопросы