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

标签 android android-fragments

这是我的代码和我尝试过的。我的代码中的问题是我有三个选项卡,第二个是我的 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/

相关文章:

android - Android fragment 中的 dispatchTouchEvent

javascript - javascript可以检测android浏览器是在前台还是后台吗?

android - 使用 AppsFlyer 跟踪应用程序安装

android - "android: onOptionsItemSelected"应该返回 true 还是 false

android - 如何为ActionBar溢出图标设置contentDescription

android - 禁用触摸背景 fragment

android - 获取用于从 Android 设备连接到 Firebase 的 IP 地址

android - 从android中的Fragment访问工具栏textview

java - 无法在 UserRecyclerAdapter 类中使用 "custom_list_users.xml"来膨胀 fragment

android - FragmentPagerAdapter 和 PagerAdapter 的区别