one Fragment to another Fragment on Button click

筅森魡賤 提交于 2019-12-11 02:29:30

问题


I have learned how to change the fragment activity from one to another (all are fragment activities) on button click but now I am having issue with multiple button on same fragment. only first button id works. I have more than one button and each button has different fragment activity. need help

package com.test.fragmentation;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

public class List extends Fragment {


    public List() {
        // Required empty public constructor
    }


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


        Button ID = (Button) rootView.findViewById(R.id.btnHello);
        ID.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    FragmentManager fragmentManager = getFragmentManager();
                    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
                    HelloFragment NAME = new HelloFragment();
                    fragmentTransaction.replace(R.id.fragment_container, NAME);
                    fragmentTransaction.commit();

                }
            });
        return rootView;
    }
}

回答1:


Change the fragment name with that in which want to move

Button ID = (Button) rootView.findViewById(R.id.btnHello);
            ID.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        FragmentManager fragmentManager = getFragmentManager();
                        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
                        HelloFragment NAME = new HelloFragment();
                        fragmentTransaction.replace(R.id.fragment_container, NAME);
                        fragmentTransaction.commit();

                    }
                });

Change Fragment name on that button click:-

ABC NAME = new ABC ();



回答2:


Button ID = (Button) rootView.findViewById(R.id.btnHello);
            ID.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                       FragmentName Name = new FragmentName ();//Name of fragment where you want to go. 

FragmentTransaction fragmentTransaction= 

getActivity().getSupportFragmentManager().beginTransaction();  

fragmentTransaction.replace(R.id.frame, Name);              

fragmentTransaction.commitAllowingStateLoss();

                    }
                });


来源:https://stackoverflow.com/questions/44498104/one-fragment-to-another-fragment-on-button-click

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!