从活动中打开片段 [英] Open Fragment From Activity

查看:81
本文介绍了从活动中打开片段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究一个小型的Google Maps应用程序,该应用程序使用户可以找到靠近它们的地方,我想添加一些功能,以使用户可以将其添加到收藏夹列表中.到目前为止,我创建了可以实现该功能的类.

Im working on a small google maps app that lets users find places close to them, I want to add functionality that lets the user add a place to a list of favourites, So far ive created classes that may do the functionality.

我的主要活动是我的主页,该主页打开其他活动,代码如下:

My main activity is my home page which opens other activities, code below:

 import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageButton;

public class MainActivity extends AppCompatActivity {

    ImageButton btnNearBy;
    ImageButton btnFavourites;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        btnNearBy = (ImageButton) findViewById(R.id.btnNearby);
        btnNearBy.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent mapIntent = new Intent(getBaseContext(), MapsActivity.class);
                startActivity(mapIntent);
            }
        });

        btnFavourites = (ImageButton) findViewById(R.id.btnFavourites);
        btnFavourites.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                FragmentManager fragmentManager = MainActivity.this.getFragmentManager();
                FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
                FavouriteListFragment fragment = new FavouriteListFragment();
                fragmentTransaction.add(R.id.fragment_container, fragment); //ERROR ON THIS LINE
                fragmentTransaction.commit();
            }
        });

    }
}

我创建了一个按钮,该按钮应该打开保存收藏夹列表的片段,我的片段声明如下: public class FavouriteListFragment extends Fragment { ... }

Ive created a button that should open up the fragment that holds the list of favourites ,My fragment is declared like this: public class FavouriteListFragment extends Fragment { ... }

有点不确定如何在单击按钮时从MainActivity中打开片段. 有任何想法吗? 谢谢!

Im a little unsure how to open the fragment from the MainActivity when clicking a button. Any ideas? Thanks!

推荐答案

有两种显示片段的方法:

There are two ways of displaying fragments:

1-首先,您需要在代码中定义一个片段容器,如下所示:

1- First you need to define a fragment container in your code like the following:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/main_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">

     <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="do something" 
        android:onClick="openFragment" />
     <FrameLayout
         android:id="@+id/fragment_container"
         android:layout_height="wrap_content"
         android:layout_width="match_content" />
</LinearLayout>

,然后您需要在活动中创建一个名为openFragment的函数,并在openFragment中使用以下代码:

and then you need to create a function called openFragment in your activity and use the following code in openFragment:

getSupportFragmentManager().beginTransaction().add(R.id_fragment_container,new FavouriteListFragment()).commit();

2-您可以在活动xml文件中定义片段,例如:

2- You can define the fragment in your activity xml file like:

<fragment xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/my_fragment"
    android:name="com.example.android.something.FavouriteListFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.android.something.FavouriteListFragment"
    tools:layout="@android:layout/fragment_layout" />

第一个称为动态片段创建,第二个称为静态.您可以在第一个活动中拥有更大的自由度,但是如果您的片段在整个活动中都没有变化,那么使用第二个活动会更简单

The first one is called dynamic fragment creation and the second one is called static. You have more freedom with the first one but if your fragment doesn't change throughout the activity it is simpler to use the second one

这篇关于从活动中打开片段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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