简体   繁体   English

使用片段上的按钮更改片段

[英]Change fragment using button on fragment

I have created a simple application that has a Navigation Drawer with Items that when selected changes the fragment that is displayed.我创建了一个简单的应用程序,它有一个带有项目的导航抽屉,当被选中时,它会更改显示的片段。 This works with no issues, every item on the navigator loads the fragment into the content frame.这没有问题,导航器上的每个项目都将片段加载到内容框架中。

I have added some buttons one of the fragments (I will be adding some to the other fragments too at a later date) but I cant work out where or how to listen to the button press and then how to handle it to change the existing fragment.我已经在其中一个片段中添加了一些按钮(稍后我也会在其他片段中添加一些按钮),但我无法弄清楚在哪里或如何听按钮按下,然后如何处理它以更改现有片段.

I had tried to add code to the MainaActivity and Fragment however it doesnt work.我曾尝试向 MainaActivity 和 Fragment 添加代码,但它不起作用。

Example fragment class示例片段 class

public class info extends Fragment {



    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        //returning our layout file
        //change R.layout.fragment for each of your fragments
        return inflater.inflate(R.layout.fragment_info, container, false);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        //you can set the title for your toolbar here for different fragments different titles
        getActivity().setTitle("Info");
    }



}

here is my code that process the original fragments这是我处理原始片段的代码

    private void displaySelectedScreen(int itemId) {

        //creating fragment object
        Fragment fragment = null;

        //initializing the fragment object which is selected
        switch (itemId) {
            case R.id.nav_atoz:
                fragment = new atoz();
                break;
            case R.id.nav_colour:
                fragment = new colour();
                break;
            case R.id.nav_type:
                fragment = new type();
                break;
            case R.id.nav_info:
                fragment = new info();
                break;



        }

        //replacing the fragment
        if (fragment != null) {

            FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
            ft.replace(R.id.content_frame, fragment).addToBackStack("tag");
            ft.commit();
                    }


        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        drawer.closeDrawer(GravityCompat.START);
    }

what am I doing wrong?我究竟做错了什么? any suggestions?有什么建议么? can I use Items on the fragment decorated like buttons and call these from the original MainActivity Case statement?我可以在装饰成按钮的片段上使用 Items 并从原始 MainActivity Case 语句中调用它们吗?

You should create a Click Listener within the Fragment that contains the button:您应该在包含按钮的 Fragment 中创建一个 Click Listener:

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    // Store the inflated layout
    View root = inflater.inflate(R.layout.my_fragment_layout, container, false);

    // Get a reference to the button using the layout
    Button myButton = root.findViewById(R.id.my_button);

    // Set the click listener
    myButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            // Perform the fragment transaction
            FragmentManager fragmentManager = getFragmentManager();
            fragmentManager.beginTransaction()
                .replace(R.id.content_container, new MyFragment(), "My_Fragment_Tag")
                .commit();
        }
    });

    return root; // Return the inflated layout
}

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

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