如何更改与片段整个布局? [英] How to change entire layout with fragments?

查看:106
本文介绍了如何更改与片段整个布局?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想,当点击导航抽屉的项目之一改变布局,或膨胀的布局,主要活动于关于活动的。

I am trying to change the layout, or inflate the layout, of the main activity to the About activity when one of the items is clicked in the navigation drawer.

这是我的主要活动的样子

This is how my main activity looks

public class MainActivity extends ActionBarActivity implements
        NavigationDrawerFragment.NavigationDrawerCallbacks {

    private NavigationDrawerFragment mNavigationDrawerFragment;

    private CharSequence mTitle;

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

        mNavigationDrawerFragment = (NavigationDrawerFragment) getSupportFragmentManager()
                .findFragmentById(R.id.navigation_drawer);
        mTitle = getTitle();

        // Set up the drawer.
        mNavigationDrawerFragment.setUp(R.id.navigation_drawer,
                (DrawerLayout) findViewById(R.id.drawer_layout));
    }

    @Override
    public void onNavigationDrawerItemSelected(int position) {
        // update the main content by replacing fragments
        FragmentManager fragmentManager = getSupportFragmentManager();
        fragmentManager
                .beginTransaction()
                .replace(R.id.container,
                        PlaceholderFragment.newInstance(position + 1)).commit();
    }

    public void onSectionAttached(int number) {
        switch (number) {
        case 1:
            mTitle = getString(R.string.title_section1);
            break;
        case 2:
            mTitle = getString(R.string.title_section2);
            break;
        case 3:
            mTitle = getString(R.string.title_section3);
            break;
        }
    }

    public void restoreActionBar() {
        ActionBar actionBar = getSupportActionBar();
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
        actionBar.setDisplayShowTitleEnabled(true);
        actionBar.setTitle(mTitle);
    }

    public static class PlaceholderFragment extends Fragment {

        private static final String ARG_SECTION_NUMBER = "section_number";

        public static PlaceholderFragment newInstance(int sectionNumber) {

            PlaceholderFragment holderFragment = new PlaceholderFragment();
            Bundle bundle = new Bundle();

            switch (sectionNumber) {
            case 1:
                bundle.putInt(ARG_SECTION_NUMBER, 05);
                break;
            case 2:
                fragment = new About();
                bundle.putInt(ARG_SECTION_NUMBER, 16);
                break;
            case 3:
                bundle.putInt(ARG_SECTION_NUMBER, 1991);
                break;
            }

            holderFragment.setArguments(bundle);
            return holderFragment;

        }

        public PlaceholderFragment() {
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_main, container,
                    false);
            TextView textView = (TextView) rootView
                    .findViewById(R.id.section_label);
            textView.setText(Integer.toString(getArguments().getInt(
                    ARG_SECTION_NUMBER)));

            return rootView;
        }

    }

}

我的主要活动的.xml看起来像这样

my main activity .xml looks like this


<!--
     As the main content view, the view below consumes the entire
     space available using match_parent in both dimensions.
-->

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

<!--
     android:layout_gravity="start" tells DrawerLayout to treat
     this as a sliding drawer on the left side for left-to-right
     languages and on the right side for right-to-left languages.
     If you're not building against API 17 or higher, use
     android:layout_gravity="left" instead.
-->
<!--
     The drawer is given a fixed width in dp and extends the full height of
     the container.
-->

<fragment
    android:id="@+id/navigation_drawer"
    android:name="com.projectname.NavigationDrawerFragment"
    android:layout_width="@dimen/navigation_drawer_width"
    android:layout_height="match_parent"
    android:layout_gravity="start" />

我有一个fragment_about.xml
这就是有关类的外观。

I have a fragment_about.xml and this is how the about class looks.

public class About extends Fragment {

    public About(){}

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

        View rootView = inflater.inflate(R.layout.fragment_about, container, false);
        return rootView;
    }
}

这是我的fragment_main.xml怎么看起来像

this is how my fragment_main.xml looks like

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.projectname.MainActivity$PlaceholderFragment">

    <TextView
        android:id="@+id/section_label"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

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

</RelativeLayout>

抽屉式导航工作正常,但它只是改变了一个文本视图,我无法弄清楚如何将整个布局从主要活动更改为活动有关的物品的抽屉里点击。

the navigation drawer works fine but it only changes a text view, i cant figure out how to change the entire layout from the main activity to the about activity on the click of an item in the drawer.

我只是不知道做什么,每次我做一些事情时我得到一个错误。

i Just don't know what to do, every time i do something i get an error.

推荐答案

首先,你应该在你MainActivity扩展FragmentActivity类。然后在NavigationDrawer点击监听做到这一点:

First of all you should extend the FragmentActivity class by your MainActivity. Then in NavigationDrawer click listener do this:

RelativeLayout l = (RelativeLayout) findViewById(R.id.container);
FragmentManager manager = getSupportFragmentManager();
About fragment = new About();
transaction = getSupportFragmentManager().beginTransaction();
    transaction.replace(R.id.fragment_container, fragment);
transaction.commit();

和来自fragment_main删除(如果你不打算用在下诸如One更深的片段)。

And remove from fragment_main (if you not gonna use next like one deeper fragment).

这篇关于如何更改与片段整个布局?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆