简体   繁体   English

如何从 Android 中的片段单击按钮时打开片段

[英]How to open a Fragment on button click from a fragment in Android

I have some button on my HomeFragment i want to to open a Fragment on button click from a fragment .我的 HomeFragment 上有一些按钮,我想在单击一个片段的按钮时打开一个片段。 Something like Navigation.类似导航的东西。 I am trying with bellow code but did not worked.我正在尝试使用波纹管代码,但没有奏效。 Please help !请帮忙 ! I am very new to android and trying to learn new things.我对android很陌生,正在尝试学习新事物。

Here is My code这是我的代码

import android.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

public class HomeFragment extends Fragment {

    public HomeFragment(){}



    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        View rootView = inflater.inflate(R.layout.fragment_home, container, false);

        Button aboutBtn = (Button) rootView.findViewById(R.id.aboutusButton);
        Button phonebookBtn = (Button) rootView.findViewById(R.id.phbookButton);
        Button schemeBtn = (Button) rootView.findViewById(R.id.schemeButton);
        Button loanBtn = (Button) rootView.findViewById(R.id.loanButton);
        Button serviceBtn = (Button) rootView.findViewById(R.id.serviceButton);
        Button devBtn = (Button) rootView.findViewById(R.id.devButton);

        aboutBtn.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v)
            {
                // Launching new Activity on selecting single List Item
                Intent i = new Intent(getActivity(), AboutFragment.class);
                startActivity(i);
            }
        });

        phonebookBtn.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v)
            {
                // Launching new Activity on selecting single List Item
                Intent j = new Intent(getActivity(), PhoneBookFragment.class);
                startActivity(j);
            }
        });

        schemeBtn.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v)
            {
                // Launching new Activity on selecting single List Item
                Intent k = new Intent(getActivity(), HomeFragment.class);
                startActivity(k);
            }
        });

        loanBtn.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v)
            {
                // Launching new Activity on selecting single List Item
                Intent l = new Intent(getActivity(), RemittanceFragment.class);
                startActivity(l);
            }
        });

        serviceBtn.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v)
            {
                // Launching new Activity on selecting single List Item
                Intent m = new Intent(getActivity(), ServiceFragment.class);
                startActivity(m);
            }
        });

        devBtn.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v)
            {
                // Launching new Activity on selecting single List Item
                Intent n = new Intent(getActivity(), AboutDeveloper.class);
                startActivity(n);
            }
        });


        return rootView;
    }
}

I am looking for something like bellow code but don't know how to make the code works with my code.我正在寻找类似波纹管代码的东西,但不知道如何使代码与我的代码一起使用。

Button aboutBtn = (Button) v.findViewById(R.id.aboutButton);
aboutBtn.setOnClickListener(this);

Button homeBtn = (Button) v.findViewById(R.id.homeButton);
homeButton.setOnClickListener(this);

Button serviceBtn = (Button) v.findViewById(R.id.serviceButton);
serviceBtn.setOnClickListener(this);

@Override
public void onClick(View view) {
    Fragment fragment = null;
    switch (view.getId()) {
        case aboutButton:
            fragment = new AboutFragment();
            break;

        case homeBtn:
            fragment = new PhonebookFragment();
            break;

        case serviceBtn:
            fragment = new ServiceFragment();
            break;

        default:
            fragment = new HomeFragment();
            break;

    }
}

You can replace the fragment using FragmentTransaction on button click.您可以在单击按钮时使用 FragmentTransaction 替换片段。 Something like this:像这样的东西:

  Fragment someFragment = new SomeFragment(); 
    FragmentTransaction transaction = getFragmentManager().beginTransaction();
    transaction.replace(R.id.fragment_container, someFragment ); // give your fragment container id in first parameter
    transaction.addToBackStack(null);  // if written, this transaction will be added to backstack
    transaction.commit(); 

Learn about performing fragment transactions here. 在此处了解如何执行片段事务。

code:代码:

  package com.rupomkhondaker.sonalibank;

import android.app.Fragment;
import android.app.FragmentTransaction;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

public class HomeFragment extends Fragment implements View.OnClickListener {

    public HomeFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View rootView = inflater.inflate(R.layout.fragment_home, container, false);

        Button aboutBtn = (Button) rootView.findViewById(R.id.aboutusButton);
        Button phonebookBtn = (Button) rootView.findViewById(R.id.phbookButton);

        aboutBtn.setOnClickListener(this);
        phonebookBtn.setOnClickListener(this);


        return rootView;
    }

    @Override
    public void onClick(View view) {
        Fragment fragment = null;
        switch (view.getId()) {
            case R.id.aboutusButton:
                fragment = new AboutFragment();
                replaceFragment(fragment);
                break;

            case R.id.phbookButton:
                fragment = new PhoneBookFragment();
                replaceFragment(fragment);
                break;
        }
    }

    public void replaceFragment(Fragment someFragment) {
        FragmentTransaction transaction = getFragmentManager().beginTransaction();
        transaction.replace(R.id.frame_container, someFragment);
        transaction.addToBackStack(null);
        transaction.commit();
    }


}

I have a simple trick to it!我有一个简单的技巧! This is the code in MainActivity:这是 MainActivity 中的代码:

Intent intent=new Intent(getApplicationContext(),Main2Activity.class);
        intent.putExtra("pos",1);
        startActivity(intent);

Intent intent1=new Intent(getApplicationContext(),Main2Activity.class);
        intent1.putExtra("pos",2);
        startActivity(intent1);

In Main2Activity which is a NavigationDrawer with fragments在 Main2Activity 中,这是一个带有片段的 NavigationDrawer

Bundle extras;
  extras=getIntent().getExtras();
        if(extras!=null)
        {
            position=extras.getInt("pos");
            if(position==1)
            {
                fragment=new FragmentOne();

                if(fragment !=null)
                {
                    android.support.v4.app.FragmentManager fragmentManager=getSupportFragmentManager();
                    FragmentTransaction fragmentTransaction=fragmentManager.beginTransaction();
                    fragmentTransaction.replace(R.id.screen_area,fragment);
                    fragmentTransaction.commit();
                }
            }
            if(position==2)
            {
                fragment=new FragmentTwo();

                if(fragment !=null)
                {
                    android.support.v4.app.FragmentManager fragmentManager=getSupportFragmentManager();
                    FragmentTransaction fragmentTransaction=fragmentManager.beginTransaction();
                    fragmentTransaction.replace(R.id.screen_area,fragment);
                    fragmentTransaction.commit();
                }
            }
        }

Kotlin example:科特林示例:

fun FragmentActivity.replaceFragment(fragment: Fragment, frameId: Int = R.id.fragment_container, addToStack: Boolean) {
val doesFragmentAlreadyExists = supportFragmentManager.findFragmentByTag(fragment.javaClass.simpleName) != null
if (!doesFragmentAlreadyExists) {
    supportFragmentManager.inTransaction {
        if (addToStack) replace(frameId, fragment, fragment.javaClass.simpleName)
            .addToBackStack(fragment.javaClass.simpleName)
        else
            replace(frameId, fragment, fragment.javaClass.simpleName)
    }
}}

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

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