android - 从操作栏选项卡开始 fragment

标签 android android-fragments

我想从操作栏选项卡内的另一个 fragment 调用一个 fragment ?我该怎么做?

显然:

startActivity(new Intent(getActivity(), Maps.class));

将不起作用。

我想打电话

public class Maps extends Fragment implements GoogleMap.OnMyLocationChangeListener
{
   @Override
   public View onCreateView(LayoutInflater inflater, ViewGroup container,   Bundle savedInstanceState) 
   {

    if (rootView != null) 
    {
        ViewGroup parent = (ViewGroup) rootView.getParent();
        if (parent != null)
            parent.removeView(rootView);
    }
    try 
    {
        rootView = inflater.inflate(R.layout.activity_maps, container, false);
    } 
    catch (InflateException e) 
    {
        /* map is already there, just return view as it is  */
    }

    fa = getActivity();


    if (initMap())
    {

            gps = new GPSTracker(fa);
            curlat = gps.getLatitude();
            curlong = gps.getLongitude();
            gps.stopUsingGPS();
            //Get the ZOOM working for given location.
    }
    else
    {
        Toast.makeText(getActivity().getApplicationContext(),"Sorry! Map not available!", Toast.LENGTH_SHORT).show();
    }


    return rootView;
}

private boolean initMap() 
{
    if (googleMap == null)
    {
        SupportMapFragment mapFrag = ((SupportMapFragment) getFragmentManager().findFragmentById(R.id.map));
        googleMap = mapFrag.getMap();
        googleMap.setTrafficEnabled(true);
        googleMap.setMyLocationEnabled(true);
        googleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
        googleMap.setOnMyLocationChangeListener(this);
    }

    return (googleMap != null);
 }

}

我的 map XML 如下所示:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/relativeLayoutforMap"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<fragment
    android:id="@+id/map"
    android:name="com.google.android.gms.maps.MapFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

</RelativeLayout>

来自:

我在 Item Click 上有这个类中的项目列表我正在尝试打开 map fragment 。

public class Friends extends Fragment 
{

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

list = (ListView) rootView.findViewById(R.id.friends_list);


   list.setOnItemClickListener(new OnItemClickListener()
    {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) 
        {
            android.support.v4.app.FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
            android.support.v4.app.FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
            Maps fragment = new Maps();
            fragmentTransaction.replace(R.id.map, fragment);
            fragmentTransaction.commit();
        }
    }); 
  return rootView;
 }

更新 1:

我尝试了以下方法:

            Fragment fragment = null;
            fragment = new Maps();

            if (fragment != null) 
            {
                FragmentManager fragmentManager = getFragmentManager();
                fragmentManager.beginTransaction()
                .replace(R.id.map, fragment).commit();
            }

我在 getFragmentManger() 中遇到错误;

Type mismatch: cannot convert from android.support.v4.app.FragmentManager to android.app.FragmentManager

告诉我!

谢谢!

最佳答案

将您的代码放入您的** friend fragment **

public android.view.View onCreateView(android.view.LayoutInflater inflater,
            android.view.ViewGroup container,
            android.os.Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.yourlayout, null);

        list=(ListView)view.findViewById(R.id.listId);
        list.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {
                Map fragment=new Map();
               ((yourActivity)getActivity()).replacefragment(fragment);


            }
        });

        return view;

    };

在您的 Activity 中创建一个方法,如下所示。

private void replacefragment(Fragment fragment){

android.support.v4.app.FragmentManager fragmentManager = getActivity()
                            .getSupportFragmentManager();
                    android.support.v4.app.FragmentTransaction fragmentTransaction = fragmentManager
                            .beginTransaction();

                    fragmentTransaction.replace(R.id.map, fragment);
                    fragmentTransaction.commit();
}

关于android - 从操作栏选项卡开始 fragment ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25840395/

相关文章:

android - 如何使用 Android 导航组件将值传回之前的 fragment 目标?

java - Android什么时候序列化对象?

java - 如何在 Java Android 中分配动态名称?

android - 线程 "main"java.lang.UnsatisfiedLinkError : no swt-win32-3550 中的异常

java - 使用 onClick 监听器事件显示图像

java - 未聚焦时更新 ListView fragment

android - fragment 中的 getLayoutInflater()

android - Windows Media Player 无法播放在 Android 中创建的 MPEG-TS 文件。

java - 如何在 RxJava 中将两个对象合并到其中一个对象中?

android - fragment 中的 MapView(蜂窝)