通过Side Nav Menu发出交换片段

时间:2014-03-28 10:55:59

标签: android android-fragments viewpagerindicator

我正在试图弄清楚如何在我的SideNavagationMenu类中更改容器中的片段,该片段包含onItemClick中的可滑动列表片段。

我正在使用this sliding menuthis view pager indicator.

使用我现在设置的代码,我收到以下错误:

java.lang.IllegalArgumentException: No view found for id 0x7f04004e (com.question.question:id/content_frame) for fragment Art{41b782e0 #9 id=0x7f04004e}

我有谷歌,并且遇到了许多其他问题,人们遇到了同样的错误,但我没有运气修复和理解我需要用我的代码修复它。

使用onClick滑动菜单类(滚动到底部):

public class RandomList extends SherlockListFragment {

public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

    return inflater.inflate(R.layout.list, null);

}

public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    SampleAdapter adapter = new SampleAdapter(getActivity());
    {
        adapter.add(new SampleItem("Archietecture",
                R.drawable.archietecture));
        adapter.add(new SampleItem("Art", R.drawable.art));
        adapter.add(new SampleItem("Cinema", R.drawable.cinema));
        adapter.add(new SampleItem("Discoveries", R.drawable.discoveries));
        adapter.add(new SampleItem("Events", R.drawable.events));
        adapter.add(new SampleItem("Literature", R.drawable.literature));
        adapter.add(new SampleItem("Music", R.drawable.music));
        adapter.add(new SampleItem("Nature", R.drawable.nature));
        adapter.add(new SampleItem("Politics", R.drawable.politics));
        adapter.add(new SampleItem("Science", R.drawable.science));
        adapter.add(new SampleItem("Sports", R.drawable.sports));
        adapter.add(new SampleItem("Technology", R.drawable.technology));

    }
    setListAdapter(adapter);
}

private class SampleItem {
    public String tag;
    public int iconRes;

    public SampleItem(String tag, int iconRes) {
        this.tag = tag;
        this.iconRes = iconRes;
    }
}

public class SampleAdapter extends ArrayAdapter<SampleItem> {

    public SampleAdapter(Context context) {
        super(context, 0);
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = LayoutInflater.from(getContext()).inflate(
                    R.layout.row, null);
        }
        ImageView icon = (ImageView) convertView
                .findViewById(R.id.row_icon);
        icon.setImageResource(getItem(position).iconRes);
        TextView title = (TextView) convertView
                .findViewById(R.id.row_title);
        title.setText(getItem(position).tag);

        return convertView;
    }

}

@Override
public void onListItemClick(ListView lv, View v, int position, long id) {

    switch (position) {
    case 0:


        break;
    case 1:


         //Here is where I am trying to swap fragments
        Fragment fragment = new Art();

        FragmentManager fm = getFragmentManager();
        FragmentTransaction transaction = fm.beginTransaction();
        transaction.replace(R.id.content_frame, fragment);
        transaction.commit();

        break;
     }
 }

我试图换成的课程:

public class Art extends SherlockListFragment {

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

    String[] values = new String[] { "Art", "Art", "Art",
            "Art", "Art", "Art", "Art", "Art",
            "Art", "Art" };
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(),
            R.layout.listcategories, R.id.label, values);
        setListAdapter(adapter);


}

@Override
public void onListItemClick(ListView l, View v, int position, long id) {
    // do something with the data
    Intent dialog = new Intent(getActivity(), Dialog_Art.class);
    startActivity(dialog);
}
}

控制视图寻呼机的MainActivity:

public class MainActivity extends BaseActivity {
TestFragmentAdapter mAdapter;
ViewPager mPager;
PageIndicator mIndicator;

private Fragment mContent;

public MainActivity() {
    super(R.string.app_name);

}

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

    mAdapter = new TestFragmentAdapter(getSupportFragmentManager());

    mPager = (ViewPager) findViewById(R.id.pager);
    mPager.setAdapter(mAdapter);

    mIndicator = (TitlePageIndicator) findViewById(R.id.indicator);
    mIndicator.setViewPager(mPager);

    if (savedInstanceState != null)
        mContent = getSupportFragmentManager().getFragment(
                savedInstanceState, "mContent");
    if (mContent == null)
        mContent = new MainView();

    getSlidingMenu().setTouchModeAbove(SlidingMenu.TOUCHMODE_MARGIN);
    setSlidingActionBarEnabled(true);

    getSupportFragmentManager().beginTransaction()
            .add(mContent, "MainView").commit();

}

@Override
public void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    getSupportFragmentManager().putFragment(outState, "mContent", mContent);
}

public void switchContent(Fragment fragment) {
    mContent = fragment;
    getSupportFragmentManager().beginTransaction()
            .replace(R.id.content_frame, fragment).commit();
    getSlidingMenu().showContent();
}

content_frame layout:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/Frame"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<FrameLayout
    android:id="@+id/content_frame"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

</FrameLayout>

0 个答案:

没有答案