Android 从活动中打开片段
我正在开发一个小的谷歌地图应用程序,它可以让用户找到离他们很近的地方。我想添加一个功能,让用户在收藏夹列表中添加一个地方。到目前为止,我已经创建了一些类来实现这个功能 我的主要活动是打开其他活动的主页,代码如下:Android 从活动中打开片段,android,android-fragments,Android,Android Fragments,我正在开发一个小的谷歌地图应用程序,它可以让用户找到离他们很近的地方。我想添加一个功能,让用户在收藏夹列表中添加一个地方。到目前为止,我已经创建了一些类来实现这个功能 我的主要活动是打开其他活动的主页,代码如下: import android.app.FragmentManager; import android.app.FragmentTransaction; import android.content.Intent; import android.os.Bundle; import an
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();
}
});
}
}
我创建了一个按钮,可以打开保存收藏列表的片段,我的片段声明如下:
公共类FavoriteListFragment扩展了片段{…}
单击按钮时,我有点不确定如何打开MainActivity中的片段。
有什么想法吗?
谢谢 因为您使用的是
android.support.v4.app.Fragment
,所以在导入正确的版本时出现了很多混乱。试着这样做:
android.support.v4.app.FragmentManager fragmentManager = MainActivity.this.getSupportFragmentManager();
android.support.v4.app.FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
FavouriteListFragment fragment = new FavouriteListFragment();
fragmentTransaction.add(R.id.fragment_container, fragment);
fragmentTransaction.commit();
其中fragment\u container
是FrameLayout
内部activity\u main
<FrameLayout
android:id="@+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
显示片段有两种方式: 1-首先,您需要在代码中定义一个片段容器,如下所示:
<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>
2-您可以在活动xml文件中定义片段,如下所示:
<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" />
第一个称为动态片段创建,第二个称为静态片段创建。您可以自由使用第一个片段,但是如果您的片段在整个活动中没有变化,那么使用第二个片段就更简单了。首先,您需要在xml文件中使用片段容器,然后您可以使用@Rohit answer进行REST。它会在我的MainActivity xml文件中吗?我没有任何与片段容器id相关的内容。我应该在哪里创建它呢?在
activity\u main
中,它应该是FrameLayout
。它表示无法解析碎片事务的方法add
。add(R.id.fragment\u容器,碎片)代码>行您使用的是哪个FragmentManager
?试着换一下。@T91,看看我更新的答案。我特别添加了要使用的fragmentManager。谢谢您的回答。片段是否会替换主活动,因为我不希望在打开列表时看到它。@T91片段永远不会替换您的活动,而是参与活动内部的视图。如果希望片段占据的布局是父视图,则是,它占据整个视图。但是,如果您的布局有例如4个视图,那么您可以将其中一个视图用于片段,其余视图用于活动主视图layout@T91如果你想更好地理解这种行为,我建议你在不同的布局上运行一些测试,看看它是如何工作的好吧,所以在这种情况下,片段将发生在主活动中?再次感谢你的回复,但我最初的帖子是,当单击按钮时,我将如何启动片段
<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" />