按钮上的一个片段到另一个片段点击

时间:2017-06-12 11:16:38

标签: android android-fragments android-fragmentactivity

我已经学会了如何在按钮点击时将片段活动从一个更改为另一个(所有都是片段活动),但现在我遇到了同一片段上的多个按钮的问题。只有第一个按钮ID有效。我有多个按钮,每个按钮都有不同的片段活动。需要帮助

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;
    }
}

3 个答案:

答案 0 :(得分:1)

使用想要移动的片段名称更改片段名称

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

                    }
                });

更改该按钮上的片段名称: -

ABC NAME = new ABC ();

答案 1 :(得分:0)

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

                    }
                });

答案 2 :(得分:0)

@Override 公共视图onCreateView(LayoutInflater充气器,ViewGroup容器, 捆绑保存的实例状态){ 查看v = inflater.inflate(R.layout.fragment_profile,container,false); notification =(Button)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;
}