简体   繁体   English

从活动中调用片段(来自OnClickListener)

[英]Call a Fragment from an Activity (from OnClickListener)

I have a button inside my Activity and when I click on this button I want to call a Fragment. 我的活动中有一个按钮,当我单击该按钮时,我想调用一个片段。

For example if I want to call an Activity I can use the intent but if I want to call a Fragment, how can I do that? 例如,如果我想调用一个活动,我可以使用意图,但如果我想调用一个片段,我该怎么做?

I have checked other questions but I have not found an answer to what I'm asking. 我检查了其他问题,但未找到所要解答的答案。

btnHome.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

        }
    });

What am I going to put inside this? 我要放在里面什么呢?

You can add your fragment dynamically.You want to create a fragment. 您可以动态添加片段。要创建片段。

To programmatically add or remove a Fragment, you will need the FragmentManager and FragmentTransaction 要以编程方式添加或删除Fragment,您将需要FragmentManagerFragmentTransaction

XML Layout XML布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

 <FrameLayout
    android:id="@+id/myFrame" <!-- Id which you're gonna use in Java -->
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

 <Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Click me" />

 </LinearLayout>

Java 爪哇

btnHome.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {

  FragmentManager fragmentManager = getFragmentManager ();
  FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction ();

   MyFragment myfragment = new MyFragment();  //your fragment 

 // work here to add, remove, etc
  fragmentTransaction.add (R.id.myFrame, myfragment);
  fragmentTransaction.commit ();

    }
});

See this doc 看到这个文件

You cannot open new fragments. 您无法打开新片段。 Fragments need to be always hosted by an activity. 片段需要始终由活动托管。 If the fragment is in the same activity (eg tabs) then the back key navigation is going to be tricky I am assuming that you want to open a new screen with that fragment. 如果该片段处于同一活动中(例如选项卡),则返回键导航将非常棘手,我假设您要使用该片段打开一个新屏幕。 So you would simply create a new activity and put the new fragment in there. 因此,您只需创建一个新活动并将新片段放在其中。 That activity would then react to the intent either explicitly via the activity class or implicitly via intent filters. 然后,该活动将通过活动类显式地或通过意图过滤器隐式地对意图做出反应。

The answer to your problem is easy: replace the current Fragment with the new Fragment and push transaction onto the backstack. 您的问题的答案很容易:用新的Fragment替换当前的Fragment并将事务推送到Backstack。 This preserves back button behaviour... 这样可以保留后退按钮的行为...

Creating a new Activity really defeats the whole purpose to use fragments anyway...very counter productive. 创建一个新的Activity确实打败了使用碎片的整个目的……非常适得其反。

@Override
public void onClick(View v) {
    // Create new fragment and transaction
    Fragment newFragment = new chartsFragment(); 
    // consider using Java coding conventions (upper first char class names!!!)
    FragmentTransaction transaction = getFragmentManager().beginTransaction();

    // Replace whatever is in the fragment_container view with this fragment,
    // and add the transaction to the back stack
    transaction.replace(R.id.fragment_container, newFragment);
    transaction.addToBackStack(null);

    // Commit the transaction
    transaction.commit(); 
}

https://developer.android.com/guide/components/fragments.html#Transactions https://developer.android.com/guide/components/fragments.html#Transactions

Quotation 报价单

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

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