如何在Android中的一个活动中显示两个片段视图?

时间:2014-08-07 07:02:29

标签: android xml android-fragments

嗨,我是Android开发的新手,我需要在一个活动中显示片段视图。我需要在启动应用后立即显示此活动。请找到随附的屏幕。供您参考,我在下面添加了我的两个XML布局:

enter image description here

XML - 1

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@color/white"
    android:fillViewport="true"
    android:scrollbars="none" >

    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical" >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical" >

            <TextView
                android:id="@+id/tvSubtitle"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@color/grey_dark"
                android:gravity="center"
                android:padding="15dp"
                android:text="Day One"
                android:textColor="@color/white"
                android:textSize="16sp"
                android:typeface="monospace" />

            <View
                android:layout_width="match_parent"
                android:layout_height="1dp"
                android:background="@color/grey_line_border" />

            <RelativeLayout
                android:id="@+id/rlQuestion1"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@drawable/list_selector"
                android:orientation="vertical"
                android:paddingBottom="5dp"
                android:paddingTop="5dp" >

                <TextView
                    android:id="@+id/time"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:ellipsize="end"
                    android:gravity="center"
                    android:text="09:00 - 09:15"
                    android:textColor="@drawable/list_text_top_color"
                    android:textSize="12sp"
                    android:textStyle="bold"
                    android:typeface="monospace" />

                <TextView
                    android:id="@+id/title"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_below="@+id/time"
                    android:gravity="center"
                    android:maxLines="2"
                    android:padding="2dp"
                    android:text="Introduction &amp; Briefing"
                    android:textAllCaps="true"
                    android:textColor="@drawable/list_text_color"
                    android:textSize="16sp"
                    android:typeface="monospace" />
            </RelativeLayout>

            <View
                android:layout_width="match_parent"
                android:layout_height="1dp"
                android:background="@color/grey" />
        </LinearLayout>
    </RelativeLayout>

</ScrollView>

XML - 2

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@color/white"
    android:fillViewport="true"
    android:scrollbars="none" >

    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical" >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical" >

            <TextView
                android:id="@+id/tvSubtitle"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@color/grey_dark"
                android:text="Thank you for attending this event.Kindly take a few minutes to share your feedback with us.Please fill in your contact details clearly or attach your business card.Kindly submit this form at the end of the event."
                android:textColor="@color/white"
                android:textSize="12sp"
                android:typeface="monospace" />

            <View
                android:layout_width="match_parent"
                android:layout_height="1dp"
                android:background="@color/grey_line_border" />

            <com.andreabaccega.widget.FormEditText
                android:id="@+id/etUserId"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@color/transparent"
                android:ems="10"
                android:hint="Name(Mr/Ms/Mdm/Dr)"
                android:inputType="text"
                android:padding="12dp"
                android:textColor="@color/blue"
                android:textColorHint="@color/grey_dark"
                android:textSize="16sp"
                android:typeface="monospace" />

            <View
                android:layout_width="match_parent"
                android:layout_height="1dp"
                android:background="@color/grey_line_border" />

            <com.andreabaccega.widget.FormEditText
                android:id="@+id/etPassword"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@color/transparent"
                android:ems="10"
                android:hint="Company"
                android:inputType="text"
                android:padding="12dp"
                android:textColor="@color/blue"
                android:textColorHint="@color/grey_dark"
                android:textSize="16sp"
                android:typeface="monospace" />

            <View
                android:layout_width="match_parent"
                android:layout_height="1dp"
                android:background="@color/grey_line_border" />
        </LinearLayout>
    </RelativeLayout>

</ScrollView>

这是我尝试过的主要活动代码:

package com.pmg.eventapp;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;

import com.actionbarsherlock.app.SherlockFragmentActivity;
import com.pmg.eventapp.R;
import com.viewpagerindicator.TabPageIndicator;

public class MainActivity extends SherlockFragmentActivity {
    private static final String[] CONTENT = new String[] { "  AGENDA  ",
            "  FEEDBACK  " };

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

        FragmentPagerAdapter adapter = new GoogleMusicAdapter(
                getSupportFragmentManager());

        ViewPager pager = (ViewPager) findViewById(R.id.pager);
        pager.setAdapter(adapter);

        TabPageIndicator indicator = (TabPageIndicator) findViewById(R.id.indicator);
        indicator.setViewPager(pager);
    }

    @Override
    public boolean onOptionsItemSelected(
            com.actionbarsherlock.view.MenuItem item) {

        switch (item.getItemId()) {
        case android.R.id.home:
            // app icon in action bar clicked; finish activity to go home
            finish();
            break;

        default:
            break;
        }

        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onResume() {
        super.onResume();
        // Set title
        getSupportActionBar().setTitle("EVENT APP");
    }

    class GoogleMusicAdapter extends FragmentPagerAdapter {
        public GoogleMusicAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {

            switch (position) {
            case 0:
                // Voting
                return new AgendaFragment();
            case 1:
                // QA
                return new FeedbackFragment();

            default:
                return null;
            }

        }

        @Override
        public CharSequence getPageTitle(int position) {
            return CONTENT[position % CONTENT.length].toString();
        }

        @Override
        public int getCount() {
            return CONTENT.length;
        }
    }

}

请帮我创建剩余的两个片段。谢谢!

1 个答案:

答案 0 :(得分:3)

这是显示片段的容器:main_container_activity

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <RelativeLayout
        android:id="@+id/content_frame"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1" >

        <com.astuetz.viewpager.extensions.PagerSlidingTabStrip
            android:id="@+id/tabs"
            android:layout_width="match_parent"
            android:layout_height="48dip"
            android:background="@drawable/background_tab" />

        <android.support.v4.view.ViewPager
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/pager"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/tabs" />
    </RelativeLayout>

</LinearLayout>

让我们说它是片段:fragment_activity

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:orientation="vertical" >

    <ProgressBar
        android:id="@+id/progressBar_indicator"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</LinearLayout>

这是你的容器,它包含两个片段让我们说:MainContainer

public class MainContainer extends SherlockFragmentActivity{

    private PagerSlidingTabStrip tabs;
    ViewPager mViewPager;
    AppSectionsPagerAdapter mAppSectionsPagerAdapter;
    public static double screenInches;

    @Override
    protected void onCreate(Bundle arg0) {
        super.onCreate(arg0);
        setContentView(R.layout.main_container_activity);

        tabs = (PagerSlidingTabStrip)findViewById(R.id.tabs);
        tabs.setIndicatorColorResource(R.color.color_website);
        tabs.setShouldExpand(true);
        mViewPager = (ViewPager)findViewById(R.id.pager);
        mAppSectionsPagerAdapter = new AppSectionsPagerAdapter(getSupportFragmentManager(), MainContainer.this);

        mViewPager.setAdapter(mAppSectionsPagerAdapter);
        tabs.setViewPager(mViewPager);
        mViewPager.setOffscreenPageLimit(1);

    }

    public static class AppSectionsPagerAdapter extends FragmentPagerAdapter {

        Context privatecontext;
        public AppSectionsPagerAdapter(FragmentManager fm, Context context) {
            super(fm);
            privatecontext = context;
        }

        @Override
        public Fragment getItem(int i) {
            switch (i) {
            case 0:
                Fragment fragment = new FragmentActivity();
                return fragment;

            case 1:
                Fragment fragment1 = new FragmentTagsActivity();
                return fragment1;

            default:
                return new FragmentActivity();
            }
        }

        @Override
        public int getCount() {
            return 2;
        }

        public CharSequence getPageTitle(int position) {
            String name = null;
            if (position==0) {
                name = privatecontext.getString(R.string.main_tab1);
            } else if (position==1) {
                name = privatecontext.getString(R.string.main_tab2);
            }
            return name;
        }
    }
}

FragmentActivity.java

public class FragmentTagsActivity extends SherlockFragment {

    ProgressBar progressBar;


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

        View rootView  = inflater.inflate(R.layout.fragment_activity, container, false);

        return rootView;
    }
}