单击一个片段到另一个片段 [英] one Fragment to another Fragment on Button click

查看:258
本文介绍了单击一个片段到另一个片段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

推荐答案

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

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

这篇关于单击一个片段到另一个片段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆