gpt4 book ai didi

android - 如何将 TapTargetView 附加到选项卡布局中 fragment 中的特定 View

转载 作者:行者123 更新时间:2023-11-29 18:49:11 28 4
gpt4 key购买 nike

这是我的代码和我尝试过的。我的代码中的问题是我有三个选项卡,第二个是我的 TapTarget View ,但是当第一个 fragment 进入 View 时,TapTargetView 就会膨胀。

这是我的主要 Activity

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 的 fragment

  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();
}
});

}
}

我还通过检查当前打开了哪些选项卡来尝试在 Activity 中使用 TapTargetView,但这会导致应用程序崩溃,提示已给出 Null Value of View。

谁能告诉我我做错了什么,以及如何解决这个问题

最佳答案

如果你想延迟显示 TapTargetView 到布局/ View 膨胀时,你可以在你试图等待的 View 上使用 View.post(Runnable)。您还可以使用 View.postDelayed(Runnable, delay) 并在通货膨胀后稍微延迟您的代码。在这些情况下,您可以确保您的 View 已膨胀,因为在膨胀之前未调用 post 并且它的布局已完成。

关于android - 如何将 TapTargetView 附加到选项卡布局中 fragment 中的特定 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51220147/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com