- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在我的应用程序中使用导航栏,每次选择一个项目时都会加载一个 fragment 。
我能够使用相同的文本字段和按钮保存 fragment 的状态,但是有一个 fragment 可以加载 map 、添加标记、集群并执行 AsyncTask
.
每次我转到另一个菜单项并返回时,它都会重新加载所有内容,例如 AsyncTask、Cluster、Markers。如何停止此 fragment 以不再重新创建 map 并在返回时保存状态:
Udate 1:我更新了代码,但问题仍然存在
主要 Activity :
public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
NavigationView navigationView = null;
Toolbar toolbar = null;
GoogleMap nMap;
SparseArray<Fragment> myFragments;
JSONArray jArray;
private static final int ERROR_DIALOG_REQUEST = 9001;
public static final int ID_OF_BEACH = 5;
//initBeach test = new initBeach();
//JSONArray jsonArray = test.ExcuteLoad();
/**
* ATTENTION: This was auto-generated to implement the App Indexing API.
* See https://g.co/AppIndexing/AndroidStudio for more information.
*/
private GoogleApiClient client;
Fragment fragment;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
myFragments = new SparseArray<>();
//setTitle("");
setContentView(R.layout.activity_main);
if (servicesOK()) {
try {
jArray = new LoadJson().execute().get();
} catch (Exception e) {
e.printStackTrace();
}
} else {
setContentView(R.layout.activity_main);
}
//Set the fragment initially
FragmentTransaction fragmentTransaction =
getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.fragment_container, new MainFragment(jArray));
fragmentTransaction.addToBackStack("1");
fragmentTransaction.commit();
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
//noinspection ConstantConditions
getSupportActionBar().setDisplayShowTitleEnabled(false);
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.setDrawerListener(toggle);
toggle.syncState();
navigationView = (NavigationView) findViewById(R.id.nav_view);
//How to change elements in the header programatically
navigationView.setNavigationItemSelectedListener(this);
// ATTENTION: This was auto-generated to implement the App Indexing API.
// See https://g.co/AppIndexing/AndroidStudio for more information.
client = new GoogleApiClient.Builder(this).addApi(AppIndex.API).build();
navigationView.setItemIconTintList(null);
}
@Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
//noinspection SimplifiableIfStatement
return super.onOptionsItemSelected(item);
}
@Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
if (id == R.id.nav_camara) {
fragment = myFragments.get(1);
// if fragment doesn't exist in myFragments, create one and add to it
if (fragment == null) {
fragment = new MainFragment(jArray);
myFragments.put(1, fragment);
}
FragmentTransaction fragmentTransaction =
getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.fragment_container, fragment);
fragmentTransaction.commit();
// Handle the camera actionn
} else if (id == R.id.nav_gallery) {
//Set the fragment initially
fragment = myFragments.get(2);
// if fragment doesn't exist in myFragments, create one and add to it
if (fragment == null) {
fragment = new GalleryFragment();
myFragments.put(2, fragment);
}
FragmentTransaction fragmentTransaction =
getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.fragment_container, fragment);
fragmentTransaction.commit();
} else if (id == R.id.nav_search) {
//Set the fragment initially
// Handle the camera action
} else if (id == R.id.nav_manage) {
// Handle the camera action
} else if (id == R.id.nav_share) {
} else if (id == R.id.nav_send) {
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
public boolean servicesOK() {
int isAvailable = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);
if (isAvailable == ConnectionResult.SUCCESS) {
return true;
} else if (GooglePlayServicesUtil.isUserRecoverableError(isAvailable)) {
Dialog dialog =
GooglePlayServicesUtil.getErrorDialog(isAvailable, this, ERROR_DIALOG_REQUEST);
dialog.show();
} else {
Toast.makeText(this, "Can't connect to mapping service", Toast.LENGTH_SHORT).show();
}
return false;
}
@Override
public void onStart() {
super.onStart();
// ATTENTION: This was auto-generated to implement the App Indexing API.
// See https://g.co/AppIndexing/AndroidStudio for more information.
client.connect();
Action viewAction = Action.newAction(
Action.TYPE_VIEW, // TODO: choose an action type.
"Main Page", // TODO: Define a title for the content shown.
// TODO: If you have web page content that matches this app activity's content,
// make sure this auto-generated web page URL is correct.
// Otherwise, set the URL to null.
Uri.parse("http://host/path"),
// TODO: Make sure this auto-generated app URL is correct.
Uri.parse("android-app://com.compscitutorials.basigarcia.navigationdrawervideotutorial/http/host/path")
);
AppIndex.AppIndexApi.start(client, viewAction);
}
@Override
public void onStop() {
super.onStop();
// ATTENTION: This was auto-generated to implement the App Indexing API.
// See https://g.co/AppIndexing/AndroidStudio for more information.
Action viewAction = Action.newAction(
Action.TYPE_VIEW, // TODO: choose an action type.
"Main Page", // TODO: Define a title for the content shown.
// TODO: If you have web page content that matches this app activity's content,
// make sure this auto-generated web page URL is correct.
// Otherwise, set the URL to null.
Uri.parse("http://host/path"),
// TODO: Make sure this auto-generated app URL is correct.
Uri.parse("android-app://com.compscitutorials.basigarcia.navigationdrawervideotutorial/http/host/path")
);
AppIndex.AppIndexApi.end(client, viewAction);
client.disconnect();
}
//AsyincTask to load JSON
public class LoadJson extends AsyncTask<String, Void, JSONArray> {
String URLu = "SOME_JSON_URL";
@Override
protected JSONArray doInBackground(String... params) {
try {
URL url = new URL(URLu);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.connect();
InputStream stream = connection.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(stream));
StringBuffer buffer = new StringBuffer();
String line = "";
while ((line = reader.readLine()) != null) {
buffer.append(line);
}
String finalJson = buffer.toString();
JSONObject jsonObject = new JSONObject(finalJson);
JSONArray jsonArray = jsonObject.getJSONArray("marcadores");
return jsonArray;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
@Override
protected void onPostExecute(JSONArray result) {
super.onPostExecute(result);
}
@Override
protected void onPreExecute() {
super.onPreExecute();
}
}
}
public class MainFragment extends Fragment {
GoogleMap nMap;
MapView mapView;
CameraUpdate update;
JSONArray array;
private ClusterManager<ItemCluster> mClusterManager;
private void parseJSON(JSONArray array) {
if (mapView != null) {
nMap = mapView.getMap();
nMap.getUiSettings().setMyLocationButtonEnabled(false);
nMap.setMyLocationEnabled(true);
nMap.getUiSettings().setZoomControlsEnabled(true);
mClusterManager = new ClusterManager<>(this.getActivity(), nMap);
nMap.setOnCameraChangeListener(mClusterManager);
nMap.setInfoWindowAdapter(mClusterManager.getMarkerManager());
nMap.setOnMarkerClickListener(mClusterManager);
}
try {
//Loading LAT LOng of markers on map
for (int i = 0; i < array.length(); i++) {
JSONObject jsonObject = array.getJSONObject(i);
String locality = jsonObject.getString("nombre");
String vientos = jsonObject.getString("viento");
int temp = jsonObject.getInt("temperatura");
double lat = jsonObject.getDouble("lat");
double lon = jsonObject.getDouble("lon");
nMap = mapView.getMap();
mClusterManager.setRenderer(new OwnIconRendered(getActivity().getApplicationContext(), nMap, mClusterManager));
ItemCluster offsetItem = new ItemCluster(lat, lon, locality, vientos, temp);
mClusterManager.addItem(offsetItem);
}
LatLng latLon = new LatLng(39.4666667, -0.3666667);
update = CameraUpdateFactory.newLatLngZoom(latLon, 10);
nMap.moveCamera(update);
} catch (Exception e) {
e.printStackTrace();
}
}
@SuppressLint("ValidFragment")
public MainFragment(JSONArray input) {
array = input;
}
public MainFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
mapView = (MapView) rootView.findViewById(R.id.map);
mapView.onCreate(savedInstanceState);
parseJSON(array);
// Inflate the layout for this fragment
setRetainInstance(true);
return rootView;
}
class OwnIconRendered extends DefaultClusterRenderer<ItemCluster> {
private final IconGenerator mClusterIconGenerator = new IconGenerator(getActivity().getApplicationContext());
private final TextView mClusterImageView;
private final ImageView mClusterViento;
private int tempAvg = 0;
private final Drawable TRANSPARENT_DRAWABLE = new ColorDrawable(Color.TRANSPARENT);
public OwnIconRendered(Context context, GoogleMap map,
ClusterManager<ItemCluster> clusterManager) {
super(context, map, clusterManager);
View clusterIcon = getActivity().getLayoutInflater().inflate(R.layout.info_windows, null);
mClusterIconGenerator.setContentView(clusterIcon);
mClusterImageView = (TextView) clusterIcon.findViewById(R.id.maxTmp);
mClusterViento = (ImageView) clusterIcon.findViewById(R.id.viento);
}
@Override
protected void onBeforeClusterItemRendered(ItemCluster item, MarkerOptions markerOptions) {
markerOptions.icon(BitmapDescriptorFactory.fromResource(R.drawable.marcador_playa));
markerOptions.title(item.getTitle());
super.onBeforeClusterItemRendered(item, markerOptions);
}
@Override
protected void onBeforeClusterRendered(Cluster<ItemCluster> cluster, MarkerOptions markerOptions) {
tempAvg = 0;
String vientoStr = null;
for (ItemCluster item : cluster.getItems()) {
tempAvg = (item.getTemp()+tempAvg);
}
mClusterImageView.setText(String.valueOf(tempAvg/cluster.getSize()));
if ((tempAvg/cluster.getSize()) > 32) {
mClusterImageView.setBackgroundResource(R.drawable.temp_32_o_mas);
} else if ((tempAvg/cluster.getSize()) <= 31 && (tempAvg/cluster.getSize()) >= 25) {
mClusterImageView.setBackgroundResource(R.drawable.temp_25_a_31);
} else if ((tempAvg/cluster.getSize()) <= 24 && (tempAvg/cluster.getSize()) >= 20) {
mClusterImageView.setBackgroundResource(R.drawable.temp_20_a_24);
} else if ((tempAvg/cluster.getSize()) <= 19 && (tempAvg/cluster.getSize()) >= 15) {
mClusterImageView.setBackgroundResource(R.drawable.temp_15_a_19);
} else if ((tempAvg/cluster.getSize()) <= 14 && (tempAvg/cluster.getSize()) >= 10) {
mClusterImageView.setBackgroundResource(R.drawable.temp_10_a_14);
} else if ((tempAvg/cluster.getSize()) <= 9 && (tempAvg/cluster.getSize()) >= 5) {
mClusterImageView.setBackgroundResource(R.drawable.temp_5_a_9);
} else if ((tempAvg/cluster.getSize()) <= 4) {
mClusterImageView.setBackgroundResource(R.drawable.temp_4_o_menos);
}
for (ItemCluster item : cluster.getItems()) {
vientoStr = item.getViento();
if (Objects.equals(vientoStr, "appvalenciaplayas/static/images/viento/viento_NE2.svg")) {
mClusterViento.setImageResource(R.drawable.viento_ne2);
} else if (Objects.equals(vientoStr, "appvalenciaplayas/static/images/viento/viento_NW2.svg")) {
mClusterViento.setImageResource(R.drawable.viento_nw2);
} else if (Objects.equals(vientoStr, "appvalenciaplayas/static/images/viento/viento_E2.svg")) {
mClusterViento.setImageResource(R.drawable.viento_e2);
} else if (Objects.equals(vientoStr, "appvalenciaplayas/static/images/viento/viento_NE1.svg")) {
mClusterViento.setImageResource(R.drawable.viento_ne1);
} else if (Objects.equals(vientoStr, "appvalenciaplayas/static/images/viento/viento_NE3.svg")) {
mClusterViento.setImageResource(R.drawable.viento_ne3);
} else if (Objects.equals(vientoStr, "appvalenciaplayas/static/images/viento/viento_NE4.svg")) {
mClusterViento.setImageResource(R.drawable.viento_ne4);
} else if (Objects.equals(vientoStr, "appvalenciaplayas/static/images/viento/viento_NW1.svg")) {
mClusterViento.setImageResource(R.drawable.viento_nw1);
} else if (Objects.equals(vientoStr, "appvalenciaplayas/static/images/viento/viento_NW3.svg")) {
mClusterViento.setImageResource(R.drawable.viento_nw3);
} else if (Objects.equals(vientoStr, "appvalenciaplayas/static/images/viento/viento_NW4.svg")) {
mClusterViento.setImageResource(R.drawable.viento_nw4);
} else if (Objects.equals(vientoStr, "appvalenciaplayas/static/images/viento/viento_E1.svg")) {
mClusterViento.setImageResource(R.drawable.viento_e1);
} else if (Objects.equals(vientoStr, "appvalenciaplayas/static/images/viento/viento_E3.svg")) {
mClusterViento.setImageResource(R.drawable.viento_e3);
} else if (Objects.equals(vientoStr, "appvalenciaplayas/static/images/viento/viento_E4.svg")) {
mClusterViento.setImageResource(R.drawable.viento_e4);
} else if (Objects.equals(vientoStr, "appvalenciaplayas/static/images/viento/viento_N1.svg")) {
mClusterViento.setImageResource(R.drawable.viento_n1);
} else if (Objects.equals(vientoStr, "appvalenciaplayas/static/images/viento/viento_N2.svg")) {
mClusterViento.setImageResource(R.drawable.viento_n2);
} else if (Objects.equals(vientoStr, "appvalenciaplayas/static/images/viento/viento_N3.svg")) {
mClusterViento.setImageResource(R.drawable.viento_n3);
} else if (Objects.equals(vientoStr, "appvalenciaplayas/static/images/viento/viento_N4.svg")) {
mClusterViento.setImageResource(R.drawable.viento_n4);
} else if (Objects.equals(vientoStr, "appvalenciaplayas/static/images/viento/viento_W1.svg")) {
mClusterViento.setImageResource(R.drawable.viento_w1);
} else if (Objects.equals(vientoStr, "appvalenciaplayas/static/images/viento/viento_W2.svg")) {
mClusterViento.setImageResource(R.drawable.viento_w2);
} else if (Objects.equals(vientoStr, "appvalenciaplayas/static/images/viento/viento_W3.svg")) {
mClusterViento.setImageResource(R.drawable.viento_w3);
} else if (Objects.equals(vientoStr, "appvalenciaplayas/static/images/viento/viento_W4.svg")) {
mClusterViento.setImageResource(R.drawable.viento_w4);
} else if (Objects.equals(vientoStr, "appvalenciaplayas/static/images/viento/viento_SW1.svg")) {
mClusterViento.setImageResource(R.drawable.viento_sw1);
} else if (Objects.equals(vientoStr, "appvalenciaplayas/static/images/viento/viento_SW2.svg")) {
mClusterViento.setImageResource(R.drawable.viento_sw2);
} else if (Objects.equals(vientoStr, "appvalenciaplayas/static/images/viento/viento_SW3.svg")) {
mClusterViento.setImageResource(R.drawable.viento_sw3);
} else if (Objects.equals(vientoStr, "appvalenciaplayas/static/images/viento/viento_SW4.svg")) {
mClusterViento.setImageResource(R.drawable.viento_sw4);
} else if (Objects.equals(vientoStr, "appvalenciaplayas/static/images/viento/viento_SE1.svg")) {
mClusterViento.setImageResource(R.drawable.viento_se1);
} else if (Objects.equals(vientoStr, "appvalenciaplayas/static/images/viento/viento_SE2.svg")) {
mClusterViento.setImageResource(R.drawable.viento_se2);
} else if (Objects.equals(vientoStr, "appvalenciaplayas/static/images/viento/viento_SE3.svg")) {
mClusterViento.setImageResource(R.drawable.viento_se3);
} else if (Objects.equals(vientoStr, "appvalenciaplayas/static/images/viento/viento_SE4.svg")) {
mClusterViento.setImageResource(R.drawable.viento_se4);
} else if (Objects.equals(vientoStr, "appvalenciaplayas/static/images/viento/viento_S1.svg")) {
mClusterViento.setImageResource(R.drawable.viento_s1);
} else if (Objects.equals(vientoStr, "appvalenciaplayas/static/images/viento/viento_S2.svg")) {
mClusterViento.setImageResource(R.drawable.viento_s2);
} else if (Objects.equals(vientoStr, "appvalenciaplayas/static/images/viento/viento_S3.svg")) {
mClusterViento.setImageResource(R.drawable.viento_s3);
} else if (Objects.equals(vientoStr, "appvalenciaplayas/static/images/viento/viento_S4.svg")) {
mClusterViento.setImageResource(R.drawable.viento_s4);
}
break;
}
mClusterIconGenerator.setBackground(TRANSPARENT_DRAWABLE);
Bitmap bitmap = mClusterIconGenerator.makeIcon(String.valueOf(cluster.getSize()));
markerOptions.icon(BitmapDescriptorFactory.fromBitmap(bitmap));
}
@Override
protected boolean shouldRenderAsCluster(Cluster cluster) {
// Always render clusters.
return cluster.getSize() > 1;
}
}
@Override
public void onResume() {
super.onResume();
mapView.onResume();
}
@Override
public void onDestroy() {
super.onDestroy();
mapView.onDestroy();
}
@Override
public void onPause() {
super.onPause();
mapView.onPause();
}
@Override
public void onLowMemory() {
super.onLowMemory();
mapView.onLowMemory();
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
mapView.onSaveInstanceState(outState);
}
}
最佳答案
您在每个事件后替换 fragment ,请将 fragment 添加到后台堆栈,然后将其从后台堆栈中弹出。然后它将起作用。检查以下代码:
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.add(android.R.id.content, testFragment);
fragmentTransaction.addToBackStack(TAG);
fragmentTranscation.commit();
关于android - 如何防止在 Fragment 内重新创建/重新加载 MapView?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39368136/
我更新了 Android Google Maps v2 示例应用程序中的 RawMapViewDemoActivity.java 以编程方式创建 MapView,但未显示 map 。我只是得到一个空白
我需要一个禁用所有手势的 MapView。它基本上只是 fragment 中的一个小 View ,当您按下它时,它会转到 LocationPickerFragment。我认为我没有以正确的方式做这件事
我试图通过函数在 mapView 上放置一些注释,但 mapView 在调试器中显示为 nil。我从不同的类调用下面的函数,调用似乎通过设置一些断点来工作。它只是在尝试将注释数组添加到 mapView
我刚刚构建了一个 swift 应用程序,它从 dynamoDB 获取 data+coords 并将其放到 ios mapkit 上。有两组坐标:一组用于当前用户位置,一组来自dynamoDB。我希望这
我在屏幕的一角有一个 Google map View 。我想添加一个事件,使其通过 map 界面上的按钮进入全屏,然后再次使用相同的按钮进入小屏幕。有什么想法吗?? 安卓 v4.0.4谷歌地图 API
问题 我有一个包含全屏 MKMapView 的 View 。 MapView 的下半部分覆盖有具有半透明单元格的 TableView,以便 map 显示。 当我以注释坐标为中心时,例如当前位置,注释
我如何从 React-Native 中 MapView 组件的纬度和经度值计算 delta-latitude 和 delta-longitude 值。谢谢你 最佳答案 如果您有一个坐标数组并且您想要一
为什么 CameraUpdateFactory 类在我的项目中不起作用? 如果执行以下命令,应用程序会崩溃: CameraUpdate pino= CameraUpdateFactory.newLat
我搜索了这个问题,发现了一些关于相同但不知道如何解决问题的 stackoverflow 问题。 我创建了一个 Activity ,我想添加一个非常小的 map View (例如 140w X 70h)
我在 android 市场上有一个应用程序,它使用 mapview 来显示一些位置。这在我测试过的所有设备上都能正常工作。将其转换市场一段时间后,我看到有几个人报告了一个问题。 map View 不拼
我在谷歌地图中用标记绘制了路径。所以路径是静态的,但标记需要改变它们的位置。如何在不使用 mapview.clear() 的情况下删除所有标记,因为它也会从 map 上清除我的路径。 有什么解决办法吗
在使用 react-native-maps 的 native 应用程序中,我试图以编程方式显示 MapView.Callout特定的 MapView.Marker在许多之中。我打算使用 showCal
我将尝试在名为 RoeteFragment 的 fragment 上显示我的 Android 应用程序中的 map 。如果我调试我的代码,我会看到方法 onMapReady 从未被调用,因此 map
我正在使用 mapbox 和 firebase。我有一个委托(delegate)函数,当用户的位置发生变化时,它会更新用户的坐标(在 firebase 数据库内部)。据我所知,它在登录到应用程序后可以
我在 SwiftUI 中使用 GMSMapView,在 map View 中我必须显示一些带有经纬度的标记。单击任何标记后,我必须移动到另一个 View 。我这样做是通过将 MapView 放入 导航
我想找到一种简单的方法来获取路线。有没有办法用mapview来做到这一点?下面是我当前的代码。 - (IBAction)goToMap:(id)sender { [self.view addSubvi
我正在尝试为 map 创建自定义注释。我遇到的问题是,我无法使注释一个接一个地删除。所有销钉同时落下。这是 didAddAnnotations 的委托(delegate)代码。你能帮我重写代码,以便我
我正在制作一款 iPhone 应用程序,该应用程序专为密苏里州本地的事物而设计。所以我的想法是将 map 限制为仅密苏里州/靠近密苏里州的地区。我的 map 工作正常,显示用户的位置,如果他们更新位置
是否可以让嵌入的 MKMapView 旋转以始终面向 iPhone 所面向的方向?基本上我想在我自己的应用程序上模仿 map 应用程序旋转功能。 我发现 iPhone SDK 没有公开该功能。但是,我
我已加载用户当前位置的自定义注释图像。我将在后台每 1 秒更新一次当前用户位置。 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
我是一名优秀的程序员,十分优秀!