PreferenceFragment在另一个片段上

时间:2016-04-01 13:54:13

标签: android android-fragments navigation-drawer preferences

我有导航抽屉的应用程序。它由两个项目组成:Main和Settings。所有项目都是碎片。 “设置”扩展了PreferenceFragment。

我有问题。当我切换到PreferencesFragment然后切换到MainFragment - PreferencesFragment未关闭且在MainFragment上。

我有showFragment()函数来切换片段。这个函数的一个论点是Fragment。但是当我为PreferencesFragment创建了这个函数时,我遇到了错误,因为在这种情况下,参数是PreferencesFragment。之后我创建了showFragmentPreferences()函数。

这很有效,但不会从屏幕上删除PreferencesFragment。

MainActivity

private Toolbar toolbar;
private DrawerLayout drawerLayout;
private ActionBarDrawerToggle drawerToggle;
private ListView drawerListView;
private ArrayList<HashMap<String, String>> arrayListDrawer;
private HashMap<String, String> mapElements;

public static final String TAG_NAME = "TAG_NAME";
public static final String TAG_FRAGMENT = "TAG_FRAGMENT";

private String itemDrawerMain;
private String itemDrawerSettings;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    itemDrawerMain = getString(R.string.item_drawer_main);
    itemDrawerSettings = getString(R.string.item_drawer_settings);

    drawerLayout = (DrawerLayout) findViewById(R.id.draver_layout);
    toolbar = (Toolbar) findViewById(R.id.toolbar);

    drawerListView = (ListView) findViewById(R.id.nav_list);
    drawerListView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);

    arrayListDrawer = new ArrayList<HashMap<String, String>>();
    AddMenuItem(mapElements, arrayListDrawer);

    drawerListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            String tag = arrayListDrawer.get(position).get(TAG_FRAGMENT);

            if (tag.equals(MainFragment.TAG_MAINFRAGMENT)) {
                showFragment(
                        MainFragment.newInstance(),
                        MainFragment.TAG_MAINFRAGMENT,
                        null,
                        true
                );
                drawerLayout.closeDrawers();
            }

            if (tag.equals(PreferencesFragment.TAG_PREFERENCE)) {
                showFragmentPreferences(
                        new PreferencesFragment(),
                        PreferencesFragment.TAG_PREFERENCE,
                        MainFragment.TAG_MAINFRAGMENT,
                        false
                );
                drawerLayout.closeDrawers();
            }
        }
    });

    setSupportActionBar(toolbar);
    drawerToggle = new ActionBarDrawerToggle(
            this,
            drawerLayout,
            toolbar,
            R.string.drawer_open,
            R.string.drawer_close
    );

    drawerLayout.addDrawerListener(drawerToggle);

    if (savedInstanceState == null) {
        showFragment(MainFragment.newInstance(), MainFragment.TAG_MAINFRAGMENT, null, true);
    }
}

private void AddMenuItem(HashMap<String, String> mapElements, ArrayList<HashMap<String, String>> arrayListDrawer) {
    mapElements = new HashMap<String, String>();
    mapElements.put(TAG_NAME, itemDrawerMain);
    mapElements.put(TAG_FRAGMENT, MainFragment.TAG_MAINFRAGMENT);
    arrayListDrawer.add(mapElements);

    mapElements = new HashMap<String, String>();
    mapElements.put(TAG_NAME, itemDrawerSettings);
    mapElements.put(TAG_FRAGMENT, PreferencesFragment.TAG_PREFERENCE);
    arrayListDrawer.add(mapElements);


    //дописать адаптер

    SimpleAdapter adapter = new SimpleAdapter(this, arrayListDrawer, android.R.layout.simple_list_item_activated_1,
            new String[]{TAG_NAME},
            new int[]{android.R.id.text1});

    drawerListView.setAdapter(adapter);
}


private void showFragmentPreferences(
        PreferencesFragment fragment,
        String tag,
        String clearToTag,
        boolean clearInclusive
) {
    FragmentManager fragmentManager = getSupportFragmentManager();
    if (clearToTag != null || clearInclusive) {
        fragmentManager.popBackStack(
                clearToTag,
                clearInclusive ? FragmentManager.POP_BACK_STACK_INCLUSIVE : 0
        );
    }
    android.app.FragmentTransaction transaction = getFragmentManager().beginTransaction();

    transaction.replace(R.id.fragment_container, fragment, tag);
    transaction.addToBackStack(tag);
    transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
    transaction.commit();
}

//функция для переключения фрагментом
private void showFragment(
        Fragment fragment,
        String tag,
        String clearToTag,
        boolean clearInclusive
) {
    FragmentManager fragmentManager = getSupportFragmentManager();
    if (clearToTag != null || clearInclusive) {
        fragmentManager.popBackStack(
                clearToTag,
                clearInclusive ? FragmentManager.POP_BACK_STACK_INCLUSIVE : 0
        );
    }

    FragmentTransaction transaction = fragmentManager.beginTransaction();
    transaction.replace(R.id.fragment_container, fragment, tag);
    transaction.addToBackStack(tag);
    transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);

    transaction.commit();
}

PreferencesFragment

public class PreferencesFragment extends PreferenceFragment {

public PreferencesFragment() {
}

public static final String TAG_PREFERENCE = "TAG_PREFERENCE";

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    addPreferencesFromResource(R.xml.preferences);
}

public static android.app.Fragment newInstanse() {
    return new PreferencesFragment();
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = super.onCreateView(inflater, container, savedInstanceState);
    view.setBackgroundColor(getResources().getColor(android.R.color.white));

    return view;
}}

MainFragment

public class MainFragment extends Fragment {
public MainFragment() {
}

public static final String TAG_MAINFRAGMENT = "TAG_MAINFRAGMENT";

public static MainFragment newInstance() {
    return new MainFragment();
}

private Button btnGetResult;
private EditText editText;

@Override
public void onStart() {
    super.onStart();
    getActivity().setTitle(R.string.app_name);
}

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.fragment_main, container, false);

    btnGetResult = (Button) v.findViewById(R.id.main_fragment_btn);
    editText = (EditText) v.findViewById(R.id.main_fragment_edit_text);

    btnGetResult.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            startActivity(new Intent(getActivity(), ResultActivity.class)
                            .putExtra(ResultActivity.EXTRA_EDIT_TEXT, editText.getText().toString())
            );
        }
    });

    return v;
}
}

0 个答案:

没有答案