PreferenceFragmentCompat inside(child of)Fragment不调度第一次触摸事件

时间:2017-11-22 07:14:17

标签: android android-fragments preferencefragment

我正在创建我的应用程序的设置并使用我创建PreferenceFragmentCompat屏幕的片段,并且在我的父片段中我将其加载到设置点击但是当我第一次点击任何偏好时它不响应我的第一次触摸但在很短的时间后,它正常工作或当我快速向上或向下滚动并点击任何偏好后,它没有响应我的触摸事件有人可以帮助吗?

public class SettingsParent extends Fragment {

Toolbar toolbar;
private HomeActivity homeActivity;


@Override
public View onCreateView(LayoutInflater inflater, @Nullable final ViewGroup container, @Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);



    setHasOptionsMenu(true);

    homeActivity = ((HomeActivity) getActivity());




    View rootView = inflater.inflate(R.layout.settings_parent, container, false);

    toolbar = (Toolbar) rootView.findViewById(R.id.toolbar);

    setTitle(getString(R.string.settings));

    setFragment(new SettingsFragment());

    homeActivity.setSupportActionBar(toolbar);

    toolbar.setNavigationOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {


            homeActivity.onBackPressed();

        }
    });

    rootView.setOnTouchListener(new View.OnTouchListener() {
        public boolean onTouch(View v, MotionEvent event) {
            Log.e("SettingTouch","parent");
            return true;
        }
    });

    android.support.v7.app.ActionBar actionBar = homeActivity.getSupportActionBar();
    assert actionBar != null;
    actionBar.setDisplayHomeAsUpEnabled(true);

    return rootView;
}


private void setTitle(String string) {
    toolbar.setTitle(string);
}



@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {

    //inflater.inflate(R.menu.browser_options_menu, menu);

    menu.setGroupVisible(R.id.grp, false);
    super.onCreateOptionsMenu(menu, inflater);

}

void setFragment(Fragment newFragment){
    FragmentManager fragmentManager = getChildFragmentManager();
    fragmentManager.popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);

    android.support.v4.app.FragmentTransaction ft = getChildFragmentManager().beginTransaction();

    ft.replace(R.id.childF, newFragment, "detailFragment");

    ft.commit();
}

}

这是parentFragment的xml。

 <?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">



    <android.support.design.widget.AppBarLayout
        android:id="@+id/main.appbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay">


        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            app:layout_scrollFlags="scroll|enterAlways"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />

    </android.support.design.widget.AppBarLayout>


    <include layout="@layout/child_fragment" />

</android.support.design.widget.CoordinatorLayout>

这是我的孩子PreferenceFragmentCompat

public class SettingsFragment extends PreferenceFragmentCompat implements HomeActivity.Authentication, Preference.OnPreferenceClickListener, SharedPreferences.OnSharedPreferenceChangeListener, ConfirmAlertDialog.OnDialogClickInterface {
@Override
public boolean onPreferenceClick(final Preference preference) {
    final String key = preference.getKey();

    switch (key) {
        case "remove_ads":
            homeActivity.purchaseRemoveAds();
            break;
        case "hide_icon":
            try {
                homeActivity.loadFragment(new HideOptiosTutorial(), getParentFragment().getFragmentManager().beginTransaction());
            } catch (Exception e) {
                e.printStackTrace();
            }
            break;

        case "key_share_app_link":
            shareAppFile();

            break;

        case "move_to_sdcard":

            showMovetoSdDialog();

            break;
        case "button_internal":

            showMovetoInternalDialog();

            break;
        case "back_up_sdcard":
            if (isSDCardUnmounted()) {
                showSDCardMountError();
            } else {

                globalPasswords = getBackupPasswords();

                if (!FileUtilis.isSafAllowed(getActivity())) {
                    showSDCardNotAllowedDialog(983);
                    //
                    return false;
                }

                if (globalPasswords.size() == 0) {
                    showNoBackupFoundMsg();
                } else {
                    showDialogToBackupFromSDCard();
                }
            }
            break;
        case "change_password":
            showChangePinDialog();
            break;

        case "key_rate_us":
            rateApplication();
            break;
        case "app_link":
            SavedAlbumsFragment.showAppLockLink(getActivity());
            break;
        case "view_intruders":
            startActivity(new Intent(homeActivity, ViewIntruders.class));
            break;
        case "theme":
            showSelectTehmeDialog();
            break;

        case "enter_email":

            showChangeEmailDialog();
            break;
    }


    return false;
}
@Override
public void onCreatePreferences(Bundle bundle, String s) {
    addPreferencesFromResource(R.xml.pref_headers);
    Log.e("Mateen","onCreatePreferences");

    homeActivity = ((HomeActivity) getActivity());
    homeActivity.addAuthenticationListner(this);
    localDatabase = LocalDatabase.getInstance(getActivity());
    localPreferences = new LocalPreferences(getActivity());

    getPreferenceScreen().getSharedPreferences().registerOnSharedPreferenceChangeListener(this);
    LocalBroadcastManager.getInstance(homeActivity).registerReceiver(broadcastReceiver,new IntentFilter("Remove_Is_Purchased"));

    removeAdsPref=findPreference("remove_ads");
    mCategory = (PreferenceCategory)findPreference("cat_remove_ads");



    if (localPreferences.IsRemovedAdsPurchased())
    {
        mCategory.removePreference(removeAdsPref);
        getPreferenceScreen().removePreference(mCategory);
    }
    else
    {
        removeAdsPref.setOnPreferenceClickListener(this);
    }

    findPreference("move_to_sdcard").setOnPreferenceClickListener(this);
    findPreference("view_intruders").setOnPreferenceClickListener(this);
    findPreference("button_internal").setOnPreferenceClickListener(this);
    findPreference("back_up_sdcard").setOnPreferenceClickListener(this);
    findPreference("change_password").setOnPreferenceClickListener(this);
    findPreference("key_rate_us").setOnPreferenceClickListener(this);
    findPreference("app_link").setOnPreferenceClickListener(this);
    findPreference("hide_icon").setOnPreferenceClickListener(this);
    findPreference("key_share_app_link").setOnPreferenceClickListener(this);
    findPreference("listPref_wrong_tries").setOnPreferenceClickListener(this);
    findPreference("theme").setOnPreferenceClickListener(this);
    findPreference("enter_email").setOnPreferenceClickListener(this);




    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        findPreference("key_cam_short").setVisible(false);
        findPreference("key_vid_short").setVisible(false);
    }

    if (!PreferenceManager.getDefaultSharedPreferences(homeActivity).getBoolean("key_intruder_detection",false))
    {
        findPreference("listPref_wrong_tries").setEnabled(false);
    }
    if (!Reprint.isHardwarePresent()) {
        findPreference("key_fingerprint").setEnabled(false);
    }

    if (!FileUtilis.isSdcardPresent(getActivity())) {
        findPreference("move_to_sdcard").setEnabled(false);
        findPreference("button_internal").setEnabled(false);
        findPreference("back_up_sdcard").setEnabled(false);

        //  findPreference("change_password").setEnabled(false);
        findPreference("always_save_to_sdcard").setEnabled(false);
    }
    //add xml

}

但是,如果我直接从我的活动中加载子片段(SettingsParent),它在没有action bar的情况下工作正常。 可以有人帮助或采用更好的方法,但我不想使用活动,我需要片段与动作栏。

1 个答案:

答案 0 :(得分:1)

我通过删除

解决了我的问题
            app:layout_scrollFlags="scroll|enterAlways"

从:

      <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        app:layout_scrollFlags="scroll|enterAlways"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />
现在它正在倾听我的每一次触摸:D 快乐的编码