如何使用PrereFragment的Jeremy Feinstein滑动菜单?

时间:2013-08-13 03:04:27

标签: android android-fragments slidingmenu

我正在使用Jeremy Feinstein滑动菜单,我想使用此菜单导航到设备设置页面,该页面将代码放入名为SettingsFragment的类中(扩展了PreferenceFragment)。

当我想将片段切换到SettingsFragment时,我收到编译错误:

  

类型不匹配:无法从SettingsFragment转换为Fragment

要查找引发错误的位置,请在代码中查找“< - WHERE”。

ContentActivity.java

创建滑动菜单并成为片段占位符。

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.util.Log;

import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu;


public class ContentActivity extends FragmentActivity {

    public static final String TAG = ContentActivity.class.getName();

    SlidingMenu slidingMenu;

    private Fragment mContent;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // set the Above View
        if (savedInstanceState != null)
            mContent = getSupportFragmentManager().getFragment(savedInstanceState, "mContent");
        if (mContent == null) {
            SimpleFragment firstWOListFragment = new SimpleFragment();
            mContent = firstWOListFragment;

        }

        //Creates the sliding menu
        slidingMenu = new SlidingMenu(this);
        slidingMenu.setMode(SlidingMenu.LEFT);
        slidingMenu.setShadowWidth(5);
        slidingMenu.setFadeDegree(0.0f);
        slidingMenu.setBehindWidth(200);
        slidingMenu.setShadowWidthRes(R.dimen.shadow_width);
        slidingMenu.setBehindOffsetRes(R.dimen.slidingmenu_offset);
        slidingMenu.setTouchModeAbove(SlidingMenu.LEFT);
        slidingMenu.setMenu(R.layout.menu_frame);
        slidingMenu.attachToActivity(this, SlidingMenu.SLIDING_WINDOW);

        // Make the left menu appear.
        this.getSupportFragmentManager().beginTransaction().replace(R.id.menu_frame, new LeftMenuItemFragment())
                .commit();

        // set the Above View
        setContentView(R.layout.content_frame);
        getSupportFragmentManager().beginTransaction().replace(R.id.content_frame, mContent).commit();

    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        try {
            getSupportFragmentManager().putFragment(outState, "mContent", mContent);
        } catch (IllegalStateException e) {
            Log.e(TAG, "Error on onSaveInstanceState");
        }
    }

    /**
     * Replaces the content frame with the fragment given as parameter
     * @param fragment
     */
    public void switchContent(Fragment fragment) {
        mContent = fragment;
        // Switch to the new fragment
        getSupportFragmentManager().beginTransaction().replace(R.id.content_frame, fragment).commit();
    }

}

LeftMenuItemFragment.java

滑动菜单布局和逻辑以在片段

之间切换
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.ListFragment;
import android.view.ContextThemeWrapper;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class LeftMenuItemFragment extends ListFragment {

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

    return view;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        String[] menuItemTitles = { "MENU", "ITEM 1", "SETTINGS" };
        ArrayAdapter<String> menuItemAdapter = new ArrayAdapter<String>(getActivity(), 
                android.R.layout.simple_list_item_1, android.R.id.text1, menuItemTitles);
        setListAdapter(menuItemAdapter);
    }

    @Override
    public void onListItemClick(ListView lv, View v, int position, long id) {
        Fragment newContent = null;
        switch (position) {
        case 1:
            newContent = new SimpleFragment();
            break;
        case 2:
            newContent = new SettingsFragment();//<- WHERE THE COMPILATION ERROR IS
            break;
        }
        if (newContent != null)
            switchFragment(newContent);
    }

    // the meat of switching the above fragment
    private void switchFragment(Fragment fragment) {
        if (getActivity() == null)
            return;

        if (getActivity() instanceof ContentActivity) {
            ContentActivity fca = (ContentActivity) getActivity();
            fca.switchContent(fragment);
        }
    }

}

SettingsFragment.java

public class SettingsFragment extends PreferenceFragment{

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getActivity().setTitle(R.string.settings);

        // Set the settings menu
        addPreferencesFromResource(R.xml.pref_all);
    }
}

SimpleFragment.java

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class SimpleFragment extends Fragment {

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

        return view;
    }

}

显然android.app.fragment和android.support.v4.app.fragment之间存在不兼容性。此滑动菜单处理v4.fragment,而PreferenceFragment是app.fragment。

有没有办法让这个滑动菜单处理app.fragment?或者我应该用PreferenceActivity替换PreferenceFragment(即使它会堆叠一个新的活动)来处理这个用例?或者还有其他解决方案可以使这项工作吗?

2 个答案:

答案 0 :(得分:0)

http://forum.xda-developers.com/showthread.php?t=1363906使用反射看看Fr4gg0r的PreferenceFragment实现 它解决了我的问题

答案 1 :(得分:0)

这是我的方法

public void onPageSelected(int selected) {
if(selected == 0)
     getSlidingMenu().setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
else
     getSlidingMenu().setTouchModeAbove(SlidingMenu.TOUCHMODE_NONE);
}