我正在使用 Jeremy Feinstein 滑动菜单,我想使用此菜单导航到设备设置页面,该页面的代码被放入名为 SettingsFragment 的类中(它扩展了 PreferenceFragment)。
当我想将片段切换到 SettingsFragment 时,出现编译错误:
类型不匹配:无法从 SettingsFragment 转换为 Fragment
要查找引发错误的位置,请在代码中查找“<- WHERE”。
内容活动.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);
}
}
简单片段.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 (即使它会堆叠一个新活动)来处理这个用例?还是有其他解决方案可以使这项工作?