【问题标题】:Android: Using ViewPager to fill RelativeLayout from MainActivityAndroid:使用 ViewPager 从 MainActivity 填充 RelativeLayout
【发布时间】:2013-12-16 17:03:26
【问题描述】:

好的,我先简短地尝试一下。有谁知道我如何插入这个类

public class ResultBar extends FragmentActivity implements ActionBar.TabListener {
AppSectionsPagerAdapter mAppSectionsPagerAdapter;
ViewPager mViewPager;

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.pagerlayout);
   ...

从 MainActivity 到它的 ViewPager 布局?

【问题讨论】:

  • 只是为了让我理解清楚。您想将 ResultBar 添加到您的 MainActivity 中吗?
  • 是的,我想将它与主要活动一起加载到布局中
  • 嗯,我的回答有用吗?
  • 我还在测试,但是是的,我可以这样集成它

标签: android class android-viewpager android-fragmentactivity main-activity


【解决方案1】:

无法将一项活动添加到另一项活动中。 Android 使用 Fragments 来实现此功能。您可以通过herehere 了解一些关于它们的信息。

因此,您可以将您的ResultBar 转换为Fragment,也可以将您的MainActivity 转换为FragmentActivity,将其以前的内容转换为MainActivityContentFragment,并将ResultBarFragment 添加到您的MainActivity。这就是您的主要活动层的样子:

/res/layout/main_activity.xml:

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

    <fragment class="com.example.MainActivityContentFragment
            android:id="@+id/content" android:layout_weight="1"
            android:layout_width="0px" android:layout_height="match_parent" />

    <fragment class="com.example.ResultBarFragmnt
            android:id="@+id/result_bar" android:layout_weight="1"
            android:layout_width="0px" android:layout_height="match_parent" />

</LinearLayout>

这里还有 sn-p 你的 ResultBarFragment 的样子:

public class ResultBarFragment extends Fragment
{
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
        return inflater.inflate(R.layout.pagerlayout, container, false);
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-04-21
    • 1970-01-01
    • 1970-01-01
    • 2017-07-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-03
    相关资源
    最近更新 更多