每次显示片段时都会显示Showcaseview

时间:2016-02-03 09:37:32

标签: android android-fragments showcaseview

我试图在片段中使用showcaseview。它可以工作,但它会不断出现每个片段的开头。我希望它在第一次打开时显示一次,而不再显示。有我的代码:

public class Map extends Fragment {
    public Map() {
    }
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }
    private MapView mMapView;
    private GoogleMap mGoogleMap;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view= inflater.inflate(R.layout.fragment_map, container, false);
        mMapView = (MapView) view.findViewById(R.id.map_dashBoard);
        mMapView.onCreate(savedInstanceState);
        mMapView.onResume();

        try {
            MapsInitializer.initialize(getActivity().getApplicationContext());
        } catch (Exception e) {
            e.printStackTrace();
        }
        mGoogleMap = mMapView.getMap();

        new ShowcaseView.Builder(getActivity())
                .setTarget(new ViewTarget(view.findViewById(R.id.map_dashBoard)))
                .setContentTitle(R.string.map_context)
                .build();
        return view;
    }
} 

我该如何解决这个问题?

3 个答案:

答案 0 :(得分:0)

使用静态或共享首选项布尔值

    if(!getDefaults("shown",getActivity())){
    new ShowcaseView.Builder(getActivity())
                    .setTarget(new ViewTarget(view.findViewById(R.id.map_dashBoard)))
                    .setContentTitle(R.string.map_context)
                    .build();
setDefaults("shown",true,getActivity())
    }

 private void setDefaults(String key, boolean value, Context context) {
        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
        SharedPreferences.Editor editor = prefs.edit();
        editor.putBoolean(key, value);
        editor.commit();
    }

    private boolean getDefaults(String key, Context context) {
        SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
        return preferences.getBoolean(key, false);
    }

答案 1 :(得分:0)

使用SharedPreference:

if(!LoadPreferencesBoolean(context,"firsttime",false)){
new ShowcaseView.Builder(getActivity())
                .setTarget(new ViewTarget(view.findViewById(R.id.map_dashBoard)))
                .setContentTitle(R.string.map_context)
                .build();
SavePreferences(context,"firsttime",true)
}

public void SavePreferences(Context context,String key, Boolean value) {

    SharedPreferences.Editor editor =PreferenceManager.getDefaultSharedPreferences(context).edit();
    editor.putBoolean(key, value);
    editor.commit();
}

public boolean LoadPreferencesBoolean(Context context,String key, Boolean defaul) {
    boolean data = PreferenceManager.getDefaultSharedPreferences(context).getBoolean(key, defaul);
    return data;
}

答案 2 :(得分:-1)

替换:

new ShowcaseView.Builder(getActivity())
            .setTarget(new ViewTarget(view.findViewById(R.id.map_dashBoard)))
            .setContentTitle(R.string.map_context)
            .build();

使用Sharedprefrences:

SharedPreferences prefs = getSharedPreferences("MyPrefs", MODE_PRIVATE);
boolean isShadowShown = prefs.getBoolean("shadowView", false);

if(isShadowShown){
new ShowcaseView.Builder(getActivity())
    .setTarget(new ViewTarget(view.findViewById(R.id.map_dashBoard)))
    .setContentTitle(R.string.map_context)
    .build();

SharedPreferences.Editor editor = getSharedPreferences("MyPrefs", MODE_PRIVATE).edit()
editor.putBoolean("shadowView", true);
editor.commit();
}