这是我的代码以及我尝试过的内容。我的代码中的问题是我有三个选项卡,第二个是我的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,但该应用程序崩溃,提示给出了“空值”。
谁能告诉我我做错了什么以及如何解决这个问题
答案 0 :(得分:0)
如果您希望推迟显示TapTargetView到展开布局/视图时,可以在要等待的视图上使用View.post(Runnable)
。您还可以使用View.postDelayed(Runnable, delay)
并在通货膨胀后稍稍延迟代码。在这些情况下,您可以确保视图是膨胀的,因为post
在膨胀之前不会被调用,并且其布局是完整的。
答案 1 :(得分:0)
我发现问题是由于viewpager夸大了视图中的下一个片段,所以仅在下面打开特定片段时才执行特定行。
@Override
public void setUserVisibleHint(boolean isVisibleToUser) {
super.setUserVisibleHint(isVisibleToUser);
if (isVisibleToUser) {
//Write down your refresh code here, it will call every time user come to this fragment.
//OR code that has to be executed only when this fragment of opened
}
}
下面是上述问题的完整代码解决方案.....
@Override
public void setUserVisibleHint(boolean isVisibleToUser) {
super.setUserVisibleHint(isVisibleToUser);
if (isVisibleToUser) {
//Write down your refresh code here, it will call every time user come to this fragment.
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();
}
});
}
}