1

MainActivity a 2 boutons et lorsque je clique sur les boutons, je veux que 2 fragments apparaissent dans la même page. Mais quand je clique sur son boutonMontContact, il apparaît, mais quand je clique sur le bouton PhoneContact, je écraser l'autre.Des fragments Android semblent transparents

MainActivity.java

public class MainActivity extends AppCompatActivity { 

Button itsMeContact,phoneContact; 

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



    itsMeContact= (Button) findViewById(R.id.its_me_contact); 
    phoneContact= (Button) findViewById(R.id.phone_contact); 

    itsMeContact.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 

      FragmentManager manager = getFragmentManager(); 
      UserFragment userFragment = new UserFragment(); 
      FragmentTransaction transaction = manager.beginTransaction(); 
      transaction.add(R.id.frame_layout, userFragment, "userFragment"); 

      transaction.commit(); 
     } 
    }); 

    phoneContact.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      FragmentManager manager = getFragmentManager(); 
      FragmentB fragmentB = new FragmentB(); 
      FragmentTransaction transaction = manager.beginTransaction(); 
      transaction.add(R.id.frame_layout, fragmentB, "fragg"); 

      transaction.commit(); 
     } 
    }); 

} 

} 

UserFragment.java

public class UserFragment extends Fragment { 
RecyclerView recyclerView ; 
UserAdapter userAdapter ; 
ArrayList<Person> arrayList ; 
private LinearLayoutManager layoutManager; 
Button itsMeContact,phoneContact; 

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


    recyclerView = view.findViewById(R.id.recycler_view); 
    itsMeContact= view.findViewById(R.id.its_me_contact); 


    layoutManager = new LinearLayoutManager(getActivity()); 
    layoutManager.scrollToPosition(0); 
    layoutManager.setOrientation(LinearLayoutManager.VERTICAL); 
    recyclerView.setLayoutManager(layoutManager); 
    arrayList=new ArrayList<Person>(); 

    initial(); 

    return view; 
} 

private void initial() { 
    Factory.getInstance().user().enqueue(new Callback<List<User>>() { 
     @Override 
     public void onResponse(Call<List<User>> call, Response<List<User>> response) { 
      // textView.setText(response.body().get(0).name); 
      userAdapter= new UserAdapter(response.body(), getActivity()); 
      recyclerView.setAdapter(userAdapter); 

     } 

     @Override 
     public void onFailure(Call<List<User>> call, Throwable t) { 

     } 
    }); 

} 

} 

FragmentB.java

public class FragmentB extends Fragment { 
TextView textView; 

@Nullable 
@Override 
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) { 
    View view=inflater.inflate(R.layout.fragment_fragment_b,container,false); 
    textView=view.findViewById(R.id.textView); 
    textView.setText("naber"); 

    return view; 
} 

}

Votre conseil important pour moi!

Merci beaucoup

Mon application: enter image description here

Répondre

0

Chaque transaction Fragment vous avez utilisé le même framelayout: "R.id.frame_layout". C'est donc normal si "ça a l'air". Si vous voulez 2 fragments affichés dans une même mise en page, vous devez utiliser 2 différents framelayout.