我有以下自定义寻呼机适配器,我从我的活动中调用并传递一些参数。一切都很好。我得到了纬度和经度的字符串。
class CustomPagerAdapter extends PagerAdapter {
Context context;
private final ArrayList<String> latitude;
private final ArrayList<String> longitude;
public CustomPagerAdapter(Activity context,ArrayList<String> latitude,ArrayList<String> longitude) {
this.context = context;
this.latitude= latitude;
this.longitude = longitude;
}
@Override
public int getCount() {
return latitude.size();
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == (object);
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View itemView = layoutInflater.inflate(R.layout.pager_item, container, false);
ImageView imageView = (ImageView) itemView.findViewById(R.id.listpager_imageView);
TextView latitude = (TextView) itemView.findViewById(R.id.listpager_text);
TextView longitude = (TextView) itemView.findViewById(R.id.listpager_title);
latitude.setText(this.latitude.get(position));
longitude.setText(this.longitude.get(position));
container.addView(itemView);
return itemView;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((RelativeLayout) object);
}
}
我也有这个地图片段,如果我从这样的活动中调用它,它工作正常:
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
// Replace the contents of the container with the new fragment
ft.replace(R.id.your_placeholder, New MapFragment());
// Complete the changes added above
ft.commit();
这是片段:
public class MapFragment extends Fragment {
MapView mapView;
GoogleMap map;
private double latitude,longitude;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.listviewtopager, container, false);
// Gets the MapView from the XML layout and creates it
mapView = (MapView) v.findViewById(R.id.mapview);
mapView.onCreate(savedInstanceState);
// Gets to GoogleMap from the MapView and does initialization stuff
map = mapView.getMap();
map.getUiSettings().setMyLocationButtonEnabled(false);
map.setMyLocationEnabled(true);
// Needs to call MapsInitializer before doing any CameraUpdateFactory calls
MapsInitializer.initialize(this.getActivity());
// Updates the location and zoom of the MapView
CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(new LatLng(1.56, 43.433), 10);
map.animateCamera(cameraUpdate);
return v;
}
@Override
public void onResume() {
mapView.onResume();
super.onResume();
}
@Override
public void onDestroy() {
super.onDestroy();
mapView.onDestroy();
}
@Override
public void onLowMemory() {
super.onLowMemory();
mapView.onLowMemory();
}
}
我想要做的是在CustomPagerAdapter中引入片段,所以当我每次刷卡时从活动中调用CustomPagerAdapter时,将加载一个新片段。 your_placeholder
id就是这个
<FrameLayout
android:id="@+id/map_fragment"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
有可能这样做吗?如果我没有正确解释问题,我很抱歉。如果您有任何疑问,我会尝试编辑它。谢谢。
编辑:我有的另一个问题 - &gt;当我按下后退按钮时,pagerActivity再次启动,它不会关闭它。我必须按两次才能关闭它。
答案 0 :(得分:0)
确定可以用PagerAdapter替换片段;我用它。由于有人提出它,FragmentPagerAdapter类可以使处理片段更容易,因此同名。我没有用它。无论哪种方式,代码都应该类似。
关于按后退按钮。如果你想有这种效果。以下是您的示例代码:
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
// Replace the contents of the container with the new fragment
ft.replace(R.id.your_placeholder, New MapFragment());
ft.addToBackStack(null); // support the Back key
// Complete the changes added above
ft.commit();
注意:添加方法 addToBackStack