【问题标题】:is there a way to display an activity within a fragment有没有办法在片段中显示活动
【发布时间】:2015-02-06 21:44:19
【问题描述】:

我在我的地图应用程序中使用导航抽屉,我设法在主页上显示地图。遇到的问题是我无法将地图片段(在布局“xml”中)连接到 MapsActivity(java 文件)。

我尝试使用MapsActivity extends Fragment 而不是MapsActivity extends FragmentActivity,但这当然行不通。

主要的xml布局:

<!-- A DrawerLayout is intended to be used as the top-level content view using match_parent for both width and height to consume the full space available. -->
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<!-- As the main content view, the view below consumes the entire
     space available using match_parent in both dimensions. -->
<FrameLayout android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent">


 <!-- *****The problem I am having is in the following fragment could not link it to the MapsActivity-->
 <fragment
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:id="@+id/map"
        tools:context=".MapsActivity"
        android:name="com.google.android.gms.maps.SupportMapFragment"/>


</FrameLayout>


<!-- android:layout_gravity="start" tells DrawerLayout to treat
     this as a sliding drawer on the left side for left-to-right
     languages and on the right side for right-to-left languages.
     If you're not building against API 17 or higher, use
     android:layout_gravity="left" instead. -->
<!-- The drawer is given a fixed width in dp and extends the full height of
     the container. -->
<fragment android:id="@+id/navigation_drawer"
    android:layout_width="@dimen/navigation_drawer_width"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:name="com.example.famfelimban.mydiet.NavigationDrawerFragment"
    tools:layout="@layout/fragment_navigation_drawer" />

但是,我有一个单独的地图 xml 布局,它工作正常 与 MapsActivity 配合使用的 activity_maps.xml

<fragment
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/map"
tools:context=".MapsActivity"
android:name="com.google.android.gms.maps.SupportMapFragment"/>

地图活动

    public class MapsActivity extends FragmentActivity {

    private GoogleMap mMap; // Might be null if Google Play services APK is not available.

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setUpMapIfNeeded();
    }

    @Override
    protected void onResume() {

    }

    private void setUpMapIfNeeded() {

    }

    private void setUpMap() {

    }
}

【问题讨论】:

    标签: java android xml android-fragments


    【解决方案1】:

    不,Activitys 是 Fragments 的持有者,Fragments 不能持有活动。

    这个问题可能很有趣:How to show different Activities in Fragment implementation?

    【讨论】:

    • 是的,我知道,但还有其他方法吗?浏览了 2 天,一无所获
    【解决方案2】:

    您在这里要做的是将您的Activity 代码移植到Fragment。这并不难,一个片段具有几乎相同的生命周期。唯一的区别是,不是在onCreate 中抓取和修改Views,而是需要在onCreateView 中进行,并且需要实现static newInstance() 方法。

    将 xml 中的 FrameLayout 留空。只有FragmentManager 才能将片段膨胀到其中。对代码的两处更改

    Activity

    @Override
    protected void onStart() {
        super.onStart();
    
        Fragment mapFragment = MapFragment.newInstance(/*args*/);
        getFragmentManager()  //you may need getSupportFragmentMananger()
                .beginTransaction()
                .replace(R.id.container, mapFragment)
                .commit()
        //this inflates the Fragment into the FrameLayout
    }
    

    并创建一个Fragment,其功能类似于您的Activity 版本

    public class MapFragment extends Fragment {
        ...
        public MapFragment() {/*should be empty*/}
        ...
        public static MapFragment newInstance(/*args*/) {
            Fragment fragment = new MapFragment();
    
            Bundle args = new Bundle();
            //put whatever information you need for your activity to run here
            fragment.setArguments(args);
            return fragment;
        }
        ...
        @Overide
        public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState) {
            //this basically takes the place of onCreate in the Activity version
            View root = inflater.inflate(R.layout.fragment_map, parent);
    
            View yourView = root.findViewById(R.id.your_view);
            ...
            return root;
        }
        ...
    }
    

    祝你好运。

    【讨论】:

    • 感谢回复,那你的意思是我的Activity中只有onStart(),然后将Activity中的所有方法都实现到fragment中?
    • onCreate() 仍然存在。 setContentView() 仍然需要被调用,你可以在 onCreate() 中执行 Fragment 事务,但 Activity 的主要工作是启动 Fragment,它完成了所有繁重的工作。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多