我想从操作栏选项卡中的另一个片段调用一个片段?我该怎么做?
显然:
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);
}
}
我的Maps 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>
发件人:
我在这个课程中有项目列表项目点击我试图打开地图片段。
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
让我知道!
谢谢!
答案 0 :(得分:2)
将您的代码放入**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;
};
在您的活动中创建一个方法,如下所示。
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();
}
答案 1 :(得分:1)
您可以使用FragmentTrasactions
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.add(R.id.content_frame, yourFragmentInstance, yourFragmentTag);
ft.commit();
答案 2 :(得分:1)
您使用 android.support.v4.app.Fragment 的片段
并使用getFragmentManager()
;它是返回android.app.FragmentManager实例。
但你使用getSupportFragmentManager()
它的作品很好。
替换此代码
Fragment fragment = null;
fragment = new Maps();
if (fragment != null)
{
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.map, fragment).commit();
}