这是我的代码以及我尝试过的内容。我的代码中的问题是我有三个选项卡,第二个是我的TapTarget View,但是当第一个片段进入视图时,TapTargetView就会膨胀。
这是我的主要活动
public class HomeActivity extends AppCompatActivity {
TabLayoutAdapter adapter;
private TabLayout tabLayout;
private int[] tabIcons = {
R.drawable.ic_tab_new,
R.drawable.ic_random,
R.drawable.ic_favorite
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_category);
ViewPager viewPager = findViewById(R.id.viewpager);
// setting up the adapter, adapter tells which fragment to load
adapter = new TabLayoutAdapter(getSupportFragmentManager());// call of consturctor
setupViewPager(viewPager);
tabLayout = findViewById(R.id.tabs);
tabLayout.setupWithViewPager(viewPager);
setupTabIcons();
tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
int tabIconColor = ContextCompat.getColor(getBaseContext(), R.color.selectedTabColor); // change color of selected tab
tab.getIcon().setColorFilter(tabIconColor, PorterDuff.Mode.SRC_IN);
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
int tabIconColor = ContextCompat.getColor(getBaseContext(), R.color.tabUnselectedColor);
tab.getIcon().setColorFilter(tabIconColor, PorterDuff.Mode.SRC_IN);
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
}
我必须在其中填充TapTargetView的片段
public class CategoryFragment extends Fragment {
private List<String> lastSearches;
private MaterialSearchBar searchBar;
private List<Category> categoryList;
private ProgressBar progressBar;
private RecyclerView recyclerView;
private CategoriesAdapter adapter, duplicateAdapter;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_category, container, false);
}
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
progressBar = view.findViewById(R.id.progressBar);
progressBar.setVisibility(View.VISIBLE);
recyclerView = view.findViewById(R.id.recycler_view);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new GridLayoutManager(getActivity(), 2));
categoryList = new ArrayList<>();
adapter = new CategoriesAdapter(getActivity(), categoryList);
duplicateAdapter = new CategoriesAdapter(getActivity(), categoryList);
recyclerView.setAdapter(adapter);
searchBar = view.findViewById(R.id.searchBar);
searchBar.setHint("Luffy...");
TapTargetView.showFor(getActivity(), // `this` is an Activity
TapTarget.forView(searchbar, "This is a target", "We have the best targets, believe me")
// All options below are optional
.outerCircleColor(R.color.red) // Specify a color for the outer circle
.outerCircleAlpha(0.96f) // Specify the alpha amount for the outer circle
.targetCircleColor(R.color.white) // Specify a color for the target circle
.titleTextSize(20) // Specify the size (in sp) of the title text
.titleTextColor(R.color.white) // Specify the color of the title text
.descriptionTextSize(10) // Specify the size (in sp) of the description text
.descriptionTextColor(R.color.red) // Specify the color of the description text
.textColor(R.color.blue) // Specify a color for both the title and description text
.textTypeface(Typeface.SANS_SERIF) // Specify a typeface for the text
.dimColor(R.color.black) // If set, will dim behind the view with 30% opacity of the given color
.drawShadow(true) // Whether to draw a drop shadow or not
.cancelable(false) // Whether tapping outside the outer circle dismisses the view
.tintTarget(true) // Whether to tint the target view's color
.transparentTarget(false) // Specify whether the target is transparent (displays the content underneath)
.icon(Drawable) // Specify a custom drawable to draw as the target
.targetRadius(60), // Specify the target radius (in dp)
new TapTargetView.Listener() { // The listener can listen for regular clicks, long clicks or cancels
@Override
public void onTargetClick(TapTargetView view) {
super.onTargetClick(view); // This call is optional
doSomething();
}
});
}
}
我还通过检查当前打开了哪些选项卡来尝试在活动中使用TapTargetView,但这使应用程序崩溃,并说给出了Null值。
谁能告诉我我做错了什么以及如何解决这个问题
最佳答案
如果您希望在展开布局/视图时推迟显示TapTargetView,则可以在试图等待的视图上使用View.post(Runnable)
。您也可以使用View.postDelayed(Runnable, delay)
并在通货膨胀后延迟代码。在这些情况下,您可以确保视图已膨胀,因为post
在膨胀之前不会被调用且其布局已完成。