HarmonyOS鸿蒙Next中关于pageSlider的问题,具体看正文,不知道怎么描述
HarmonyOS鸿蒙Next中关于pageSlider的问题,具体看正文,不知道怎么描述 首先这是一个tab栏和pageSlider的联动
tab栏三个对应pageSlider的三个
点击地图栏后,定位完成。
之后在返回首页栏,点击设备删除这一项,跳转到设备删除的页面,这个页面是空的,但是会加载地图的界面,如下所示
设备删除页面的代码(里面什么都没有)
<?xml version="1.0" encoding="utf-8"?>
<DirectionalLayout
xmlns:ohos="http://schemas.huawei.com/res/ohos"
ohos:height="match_parent"
ohos:width="match_parent"
ohos:orientation="vertical"/>
以下是整个mainAbility的代码,我的那一栏代码不涉及到此问题,故没有加入
public class MainAbilitySlice extends AbilitySlice implements LocationSource {
EasyDialog.Builder builder = EasyDialog.builder(this, DefaultTypeConfig.ALERT_DIALOG);
int id=0;
User user;
private MapView mapView;
private AMap aMap;
private Locator locator;
private MylocatorCallBack myLocatorCallback = new MylocatorCallBack();
private static final String PERM_LOCATION = "ohos.permission.LOCATION";
private OnLocationChangedListener mListener = null;
public static final int MY_LOCATION_PERMISSION = 6;
private static final HiLogLabel hilog = new HiLogLabel(HiLog.DEBUG, 0x0000, "APP_LOG");
GeoConvert geoConvert = new GeoConvert();
@Override
public void onStart(Intent intent) {
super.onStart(intent);
super.setUIContent(ResourceTable.Layout_ability_main);
if(intent!=null){
IntentParams params = intent.getParams();
id = (int) params.getParam("userid");
}
OrmContext context = Utils.getUserOrmContext(this);
OrmPredicates predicates = new OrmPredicates(User.class)
.equalTo("userid",id);
List<User> users = context.query(predicates);
user = users.get(0);
context.close();
int[] icons = new int[3];
icons[0] = ResourceTable.Media_ic_public_home;
icons[1] = ResourceTable.Media_ic_gallery_map_all;
icons[2] = ResourceTable.Media_ic_public_settings;
TabList tablist = findComponentById(ResourceTable.Id_tab_list);
tablist.removeAllComponents();
String[] tablistTags = {"首页","地图","我的"};
for(int i=0;i<tablistTags.length;i++){
TabList.Tab tab = tablist.new Tab(this);
tab.setText(tablistTags[i]);
setTabImage(tab,icons[i]);
tablist.addTab(tab);
}
List<Integer> layoutFileIds = new ArrayList<>();
layoutFileIds.add(ResourceTable.Layout_ability_main_index);
layoutFileIds.add(ResourceTable.Layout_ability_main_map);
layoutFileIds.add(ResourceTable.Layout_ability_main_user);
PageSlider pageSlider = findComponentById(ResourceTable.Id_page_slider);
pageSlider.setProvider(new TabPageSliderProvider(layoutFileIds,this));
tablist.addTabSelectedListener(new TabList.TabSelectedListener() {
@Override
public void onSelected(TabList.Tab tab) {
int index = tab.getPosition();
pageSlider.setCurrentPage(index);
if(index==0){
initIndex(user);
}else if(index==1){
initMapView();
}else if(index==2){
inituser(user,id);
}
}
@Override
public void onUnselected(TabList.Tab tab) {}
@Override
public void onReselected(TabList.Tab tab) {}
});
tablist.selectTabAt(0);
pageSlider.setSlidingPossible(false);
}
private void inituser(User user, int id) {
Text userid = findComponentById(ResourceTable.Id_userid);
Text username = findComponentById(ResourceTable.Id_username);
userid.setText(user.getUserid()+"");
username.setText(user.getUserName());
Button changepwdbtn = findComponentById(ResourceTable.Id_changepwd_btn);
changepwdbtn.setClickedListener(component -> {
Intent intent = new Intent();
intent.setParam("userid",id);
present(new ChangepwdAbilitySlice(),intent);
});
Button quitbtn = findComponentById(ResourceTable.Id_quit_btn);
quitbtn.setClickedListener(component -> {
OrmContext context1 = Utils.getTokenOrmContext(this);
OrmPredicates predicates1 = new OrmPredicates(Token.class);
predicates1.clear();
context1.delete(predicates1);
Intent intent1 = new Intent();
present(new LoginAbilitySlice(),intent1);
});
}
private void initMapView() {
mapView = new MapView(this);
DirectionalLayout layout = findComponentById(ResourceTable.Id_map_directionlayout);
DirectionalLayout.LayoutConfig config = new DirectionalLayout.LayoutConfig(
DirectionalLayout.LayoutConfig.MATCH_PARENT, 1785);
mapView.setLayoutConfig(config);
layout.addComponent(mapView);
mapView.onCreate(null);
mapView.onResume();
requestLocationPermission();
aMap = mapView.getMap();
requestLocationPermission();
aMap.setOnMyLocationChangeListener(new AMap.OnMyLocationChangeListener() {
@Override
public void onMyLocationChange(Location location) {
}
});
aMap.setOnMapLoadedListener(new AMap.OnMapLoadedListener() {
@Override
public void onMapLoaded() {
// todo
}
});
aMap.setOnMapTouchListener(new AMap.OnMapTouchListener() {
@Override
public void onTouch(MotionEvent motionEvent) {
System.out.println("~~~~~~~~~~~~~~~~~~~~"+motionEvent.getX()+"~~~~~~~~~~"+motionEvent.getY());
int posX = (int) motionEvent.getX();
int posY = (int) motionEvent.getY();
Point point = new Point(posX,posY);
LatLng latLng = aMap.getProjection().fromScreenLocation(point);
List<GeoAddress> geo = null;
try {
geo = geoConvert.getAddressFromLocation(latLng.latitude,latLng.longitude,1);
} catch (IOException e) {
e.printStackTrace();
}
String location = geo.get(0).getAdministrativeArea()
+geo.get(0).getSubAdministrativeArea()
+geo.get(0).getRoadName()
+geo.get(0).getSubRoadName();
builder.setTitle("位置")
.setMessage(location)
.setOnCancelListener(dialog -> Log.d(TAG, "onCancel"))
.setOnDismissListener(dialog -> Log.d(TAG, "onDismiss"))
.setNeutralButton("know", null)
.setPositiveButton("ok", (dialog, which) -> {
final Marker marker = aMap.addMarker(new MarkerOptions().position(latLng));
Log.d(TAG, "onClick ok");
})
.setNegativeButton("cancel", (dialog, which) -> dialog.dismiss())
.setCancelable(true);
builder.build().show();
}
});
}
public void requestLocationPermission(){
if (verifySelfPermission("ohos.permission.LOCATION") != IBundleManager.PERMISSION_GRANTED ){
if (canRequestPermission("ohos.permission.LOCATION") ){
requestPermissionsFromUser(
new String[]{"ohos.permission.LOCATION","ohos.permission.LOCATION_IN_BACKGROUND"},
MY_LOCATION_PERMISSION
);
}else{
new ToastDialog(getContext()).setText("请进入手机系统【设置】中,开启应用的定位权限").show();
}
}else{
new ToastDialog(getContext()).setText("此应用已授权,可进行定位功能的初始化").show();
HiLog.info(hilog,"此应用已授权,开始定位" );
isOpenLocationSwitch();
}
}
public void isOpenLocationSwitch(){
HiLog.info(hilog,"获取定位器" );
locator = new Locator(getContext());
if(locator.isLocationSwitchOn()){
HiLog.info(hilog,"[位置信息]开关已开启" );
onLocationChangeListener();
} else {
new ToastDialog(getContext()).setText("请在控制中心打开【位置信息】开关,以便获取当前所在位置").show();
locator.stopLocating(myLocatorCallback);
}
}
public void onLocationChangeListener() {
HiLog.info(hilog,"定位功能开始" );
RequestParam requestParam = new RequestParam(RequestParam.SCENE_NAVIGATION);
HiLog.info(hilog,"开启定位" );
locator.startLocating(requestParam,myLocatorCallback);
HiLog.info(hilog,"绘制蓝点");
getMyLocationStyle();
}
private void getMyLocationStyle(){
HiLog.info(hilog,"绘制定位小蓝点" );
MyLocationStyle locationStyle = new MyLocationStyle();
aMap.setMyLocationStyle(locationStyle);
locationStyle.interval(1000);
aMap.setMyLocationStyle(locationStyle);
aMap.getUiSettings().setMyLocationButtonEnabled(false);
aMap.setLocationSource(this);
aMap.setMyLocationEnabled(true);
}
@Override
public void activate(OnLocationChangedListener onLocationChangedListener) {
mListener = onLocationChangedListener;
}
@Override
public void deactivate() {
mListener = null;
}
public class MylocatorCallBack implements LocatorCallback {
@Override
public void onLocationReport(final Location location) {
HiLog.info(hilog,"AMap定位,Latitude为:" + location.getLatitude()+",Longitude为:" + location.getLongitude());
getUITaskDispatcher().asyncDispatch(() -> {
getLocation(location);
});
}
@Override
public void onStatusChanged(int i) {}
@Override
public void onErrorReport(int i) {}
}
public void getLocation(Location location){
LatLng latLng = new LatLng(location.getLatitude(),location.getLongitude());
System.out.println("Latitude"+location.getLatitude()+"Longtitude"+location.getLongitude());
aMap.moveCamera(CameraUpdateFactory.newLatLngZoom(latLng,16));
HiLog.info(hilog,"AMap定位,Latitude为:" + location.getLatitude()+",Longitude为:" + location.getLongitude());
getMyLocationStyle();
mListener.onLocationChanged(location);
}
@Override
public void onActive() {
super.onActive();
}
@Override
public void onForeground(Intent intent) {
super.onForeground(intent);
}
@Override
protected void onStop() {
super.onStop();
if (mapView != null) {
mapView.onDestroy();
}
}
}
第一次写鸿蒙开发,代码可能有点乱。麻烦各位了,还请大佬们不吝赐教。
如果还有什么需要的,我一定及时补充
更多关于HarmonyOS鸿蒙Next中关于pageSlider的问题,具体看正文,不知道怎么描述的实战教程也可以访问 https://www.itying.com/category-93-b0.html
2 回复
在HarmonyOS鸿蒙Next中,PageSlider
是一个用于实现页面滑动效果的组件。如果你遇到问题,可以从以下几个方面排查:
- 布局问题:确保
PageSlider
的父容器和子页面布局正确,避免尺寸或位置异常。 - 数据绑定:检查数据源是否正确绑定到
PageSlider
,确保页面内容正常显示。 - 事件处理:确认滑动事件是否被正确监听和处理,避免交互失效。
- 性能优化:如果页面较多,注意优化加载和渲染性能,避免卡顿。
提供具体代码或错误信息,可以更精准地定位问题。