简体   繁体   English

单击按钮时一个片段到另一个片段

[英]one Fragment to another Fragment on Button click

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.只有第一个按钮 id 有效。 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;
    }
}

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 ();
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();

                    }
                });

@Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View v = inflater.inflate(R.layout.fragment_profile, container, false); @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View v = inflater.inflate(R.layout.fragment_profile, container, false); notification = (Button)v.findViewById(R.id.notification);通知 = (按钮)v.findViewById(R.id.notification);

    notification.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            FragmentTransaction fr = getFragmentManager().beginTransaction();
            fr.replace(R.id.container(this is the main activity container layout),new NotificationFragment(this is the fragment you want to go)());
            fr.commit();
        }
    });

    return v;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM