我在两个地方用map as实现了我的map片段:
(主)-->(容器)
-->(碎片膨胀)-->-->(容器)
-->(片段膨胀)-->-->(容器)
-->(Map碎片膨胀)-->
(主)-->(容器)
-->(碎片膨胀)-->(容器)
-->(Map碎片膨胀)-->
问题是,在第一种情况下,当我单击Map标记时,或者当我从“Map片段”视图组中单击一个imageview时,我的Map会闪烁,该组只在“消失”和“可见”之间切换图标可见性。第二种情况很好。它给我一个提示,Map的闪烁是由于其他视图改变了它们在nestedscrollview中的可见性。也许这也是因为家长人数过多。
我试图实现 android:layerType="hardware"
但这没用。另外,当可见性在可见和不可见之间切换时,所有操作都正常,但这不是解决方案。
我已经记录了这张Map的行为并附在这里。在记录上,我点击一个切换按钮,在消失和可见之间切换一个小的勾号图标。
你知道怎么解决吗?
代码
主要活动
这是一个导航抽屉活动
public class MainActivityC extends AppCompatActivity {
private AppBarConfiguration mAppBarConfiguration;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_c);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
DrawerLayout drawer = findViewById(R.id.drawer_layout);
NavigationView navigationView = findViewById(R.id.nav_view);
// Passing each menu ID as a set of Ids because each
// menu should be considered as top level destinations.
mAppBarConfiguration = new AppBarConfiguration.Builder(
R.id.nav_home, R.id.nav_gallery, R.id.nav_slideshow)
.setDrawerLayout(drawer)
.build();
NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
NavigationUI.setupActionBarWithNavController(this, navController, mAppBarConfiguration);
NavigationUI.setupWithNavController(navigationView, navController);
}
@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_activity_c, menu);
return true;
}
@Override
public boolean onSupportNavigateUp() {
NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
return NavigationUI.navigateUp(navController, mAppBarConfiguration)
|| super.onSupportNavigateUp();
}
}
碎片1
此片段包含viewpager2
public class GalleryFragment extends Fragment{
public View onCreateView(@NonNull LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_gallery, container, false);
}
private Context context;
private ViewPager2 viewPager2;
private TabLayout tabLayout;
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
context = getActivity();
initTab();
}
private void initTab() {
viewPager2 = getView().findViewById(R.id.viewpager2_walks_saved);
viewPager2.setAdapter(new WalksSavedPagerAdapter(getActivity()));
viewPager2.setUserInputEnabled(false); // NO SCROLL
viewPager2.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
@Override
public void onPageSelected(int position) {
super.onPageSelected(position);
switch (position) {
case 0:
String text1 = "Walks History";
((MainActivityC) context).getSupportActionBar().setTitle(text1);
break;
case 1:
String text2 = "Walks Statistics";
((MainActivityC) context).getSupportActionBar().setTitle(text2);
break;
}
}
});
tabLayout = getView().findViewById(R.id.tabLayout_walks_statistics);
TabLayoutMediator tabLayoutMediator = new TabLayoutMediator(
tabLayout, viewPager2, new TabLayoutMediator.TabConfigurationStrategy() {
@Override
public void onConfigureTab(@NonNull TabLayout.Tab tab, int position) {
switch (position) {
case 0:
tab.setText("Walks History");
break;
case 1:
tab.setText("Walks Statistics");
break;
}
}
}
);
tabLayoutMediator.attach();
}
}
布局:
<androidx.coordinatorlayout.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<com.google.android.material.appbar.AppBarLayout
android:layout_width="match_parent"
android:layout_height="160dp"
android:background="?attr/colorPrimary">
<com.google.android.material.appbar.CollapsingToolbarLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_scrollFlags="scroll|snap"
android:elevation="4dp">
<TextView
android:id="@+id/drawer_fragment_saved_walks_info"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:text="Saved Walks"
android:textSize="30sp"
android:textColor="@color/white"
android:layout_gravity="center"/>
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_height="0dp"
android:layout_width="match_parent"
app:layout_collapseMode="pin" />
</com.google.android.material.appbar.CollapsingToolbarLayout>
<!--android:layout_height="?attr/actionBarSize"-->
</com.google.android.material.appbar.AppBarLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<!-- Tab to chose between Saved Walks and Statistics -->
<com.google.android.material.tabs.TabLayout
android:id="@+id/tabLayout_walks_statistics"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/transparent"
android:layout_gravity="bottom"
app:tabIndicatorHeight="3dp"
app:tabMode="fixed"
app:tabPaddingBottom="4dp"
app:tabPaddingTop="4dp" />
<androidx.viewpager2.widget.ViewPager2
android:id="@+id/viewpager2_walks_saved"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"/>
</LinearLayout>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
碎片2
这个片段显示在viewpager2中,并且有自己的viewpager2
public class StatisticsFragment extends Fragment {
private ViewPager2 viewPager2;
private TabLayout tabLayoutViewPager;
private StatisticsPagerAdapter statisticsPagerAdapter;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_walks_statistics, container, false);
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
init();
}
private void init(){
statisticsPagerAdapter = new StatisticsPagerAdapter(getActivity());
viewPager2 = getView().findViewById(R.id.viewpager2_statistics);
viewPager2.setAdapter(statisticsPagerAdapter);
viewPager2.setUserInputEnabled(false); // NO SCROLL
tabLayoutViewPager = getView().findViewById(R.id.tabLayout_statistics);
TabLayoutMediator tabLayoutMediator = new TabLayoutMediator(
tabLayoutViewPager, viewPager2, new TabLayoutMediator.TabConfigurationStrategy() {
@Override
public void onConfigureTab(@NonNull TabLayout.Tab tab, int position) {
switch (position) {
case 0:
tab.setText("Events");
break;
case 1:
tab.setText("Chronology");
break;
case 2:
tab.setText("Heat Map");
break;
}
}
}
);
tabLayoutMediator.attach();
}
}
布局:
<androidx.core.widget.NestedScrollView
android:id="@+id/heatmap_nested_scroll_view"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<com.google.android.material.tabs.TabLayout
android:id="@+id/tabLayout_statistics"
android:layout_width="match_parent"
android:layout_height="30dp"
android:background="@android:color/transparent"
android:layout_marginTop="8dp"
app:tabIndicatorHeight="3dp"
app:tabMode="fixed"
app:tabPaddingBottom="4dp"
app:tabPaddingTop="4dp" />
<View
android:id="@+id/view"
android:layout_width="match_parent"
android:layout_height="2dp"
android:background="@android:color/darker_gray" />
<androidx.viewpager2.widget.ViewPager2
android:id="@+id/viewpager2_statistics"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
</androidx.core.widget.NestedScrollView>
谷歌Map碎片3
这个片段显示在viewpager2中并保存fickeringMap
public class StatisticsHeatmapFragment extends Fragment implements OnMapReadyCallback,
CompoundButton.OnCheckedChangeListener {
private Context context;
private Fragment parentFragment;
private ToggleButton iconRescale;
private ImageView iconCheck;
private MapFragment mapFragment;
private GoogleMap map;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_statistics_heatmap, container, false);
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
initiate();
}
@SuppressLint("ClickableViewAccessibility") // Due to the touch listener
private void initiate() {
context = getActivity();
// A neat way to find the parent fragment in a child ViewPager fragment
List<Fragment> fragment = getActivity().getSupportFragmentManager().getFragments();
for (Fragment f:fragment) {
if (f instanceof StatisticsFragment){
parentFragment = f;
}
}
ImageView transparentImageView = (ImageView) getView().findViewById(R.id.heatmap_transparent_image);
// Tag is automatically given by ViewPager2 like "f" + position, but I have overwritten it.
// So the StatisticsFragment is "f101". There the NestedScrollView locates.
final NestedScrollView nestedScrollView = (NestedScrollView)
getActivity().getSupportFragmentManager().findFragmentByTag("f101").getView();
// The warning comes up because Android wants to remind you to think about the blind or visually impaired people who may be using your app
transparentImageView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
int action = event.getAction();
switch (action) {
case MotionEvent.ACTION_DOWN:
// Disallow ScrollView to intercept touch events.
nestedScrollView.requestDisallowInterceptTouchEvent(true);
// Disable touch on transparent view
return false;
case MotionEvent.ACTION_UP:
// Allow ScrollView to intercept touch events.
nestedScrollView.requestDisallowInterceptTouchEvent(false);
return true;
case MotionEvent.ACTION_MOVE:
nestedScrollView.requestDisallowInterceptTouchEvent(true);
return false;
default:
return true;
}
}
});
mapFragment = (MapFragment) getActivity().getFragmentManager()
.findFragmentById(R.id.map_statistics);
mapFragment.getMapAsync(this);
}
@Override
public void onMapReady(GoogleMap googleMap) {
map = googleMap;
initToggleButtons();
}
private void initToggleButtons() {
iconCheck = getView().findViewById(R.id.icon_check_heatmap_stats);
iconRescale = getView().findViewById(R.id.icon_rezoom_heatmap_stats);
iconRescale.setChecked(true);
iconRescale.setOnCheckedChangeListener(this);
}
/**
* CheckChange listener for events icon (ToggleButton)
*/
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (buttonView.equals(iconRescale)){
if (isChecked) {
DrawableCompat.setTint(
DrawableCompat.wrap(iconRescale.getBackground()),
ContextCompat.getColor(context, R.color.black));
iconCheck.setVisibility(View.VISIBLE);
Toast.makeText(context, "Map scale to the data is ON", Toast.LENGTH_SHORT).show();
} else {
DrawableCompat.setTint(
DrawableCompat.wrap(iconRescale.getBackground()),
ContextCompat.getColor(context, R.color.common_google_signin_btn_text_light));
iconCheck.setVisibility(View.GONE);
Toast.makeText(context, "Map scale to the data is OFF", Toast.LENGTH_SHORT).show();
}
}
}
}
布局:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<fragment
xmlns:map="http://schemas.android.com/apk/res-auto"
android:name="com.google.android.gms.maps.MapFragment"
android:id="@+id/map_statistics"
android:layout_width="match_parent"
android:layout_height="match_parent"
map:mapType="normal" />
<ImageView
android:id="@+id/heatmap_transparent_image"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@android:color/transparent" />
<ImageView
android:id="@+id/icon_check_heatmap_stats"
android:layout_width="14dp"
android:layout_height="14dp"
android:background="@drawable/ic_baseline_check_24"
android:backgroundTint="@android:color/holo_red_light"
android:layout_gravity="end|top"
android:layout_marginTop="28dp"
android:layout_marginEnd="69dp"/>
<ToggleButton
android:id="@+id/icon_rezoom_heatmap_stats"
android:layout_width="30dp"
android:layout_height="30dp"
android:background="@drawable/ic_baseline_zoom_out_map_24"
android:backgroundTint="@color/black"
android:layout_gravity="end|top"
android:layout_marginTop="20dp"
android:layout_marginEnd="60dp"
android:textOn=" "
android:textOff=" "/>
</FrameLayout>
p、 需要多次单击图标才能使用此代码发现问题。
1条答案
按热度按时间pkmbmrz71#
我已经解决了我自己的问题,但是其他的解决方案是非常受欢迎的。
问题是Map的父级
<NestedScrollView>
有android:fillViewport="true"
属性,强制视图拉伸其内容以填充视口。解决方法是将Map的视图设置为恒定的高度。所以,
android:layout_height="match_parent"
应改为android:layout_height="500dp"
例如。此外,高度可根据需要进行数字设置。