Android 同一活动中的两个片段 [英] Android Two Fragments in Same Activity

查看:25
本文介绍了Android 同一活动中的两个片段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道以下帖子:在单个活动中使用多个片段

我正在寻找的是针对特定问题的特定答案.以下代码的结果是一个空白的 FragmentActivity.我在下面的代码中缺少什么来让它呈现带有两个片段的活动.一个是空列表片段,另一个是包含一个输入框和一个水平布局按钮的片段,(这个布局可以在 http://developer.android.com/training/basics/firstapp/starting-activity.html) 我想绝对放在屏幕底部固定高度约25d.

What I am looking for is a specific answer to a specific problem. The result of the following code is a blank FragmentActivity. What am I missing in my following code to get it to render an activity with two fragments. One is a empty list fragment, the other is a fragment which contains a input box and a button in a horizontal layout, (this layout can be found at http://developer.android.com/training/basics/firstapp/starting-activity.html) that I want to be placed absolutely at the bottom of the screen with a fixed height of about 25 dip.

Manifest.xml

Manifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.my.package"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.my.package.Application"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

我的主要活动及其相关的 application.xml 文件.

My main activity and its associated application.xml file.

package com.my.package;

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;

public class Application  
extends FragmentActivity 
implements MessageListViewFragment.OnLineSelectedListener, 
SendMessageFragment.OnSendButtonPressed {

    MessageListViewFragment mMessageListFragment;
    SendMessageFragment mSendMessageFragment;

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

        mMessageListFragment = new MessageListViewFragment();
        mSendMessageFragment = new SendMessageFragment();

        FragmentTransaction transaction = 
                getSupportFragmentManager().beginTransaction();

        transaction.add(R.id.message_fragment, mMessageListFragment);
        transaction.add(R.id.send_fragment, mSendMessageFragment);

        transaction.commit();
    }

    @Override
    public void onListItemSelected(int position) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onSendButtonPressed() {
        // TODO Auto-generated method stub

    }
}

布局:

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

    <fragment
        android:id="@+id/message_fragment"
        android:name="com.example.android.fragments.MessageListViewFragment"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:gravity="top" />

    <fragment
        android:id="@+id/send_fragment"
        android:name="com.example.android.fragments.SendMessageFragment"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:gravity="bottom" />

</LinearLayout>

现在对于两个片段及其相关的 xml 文件:第一个片段(在顶部列出片段)

And now for the two fragments and their associated xml files: First Fragment (list fragment at top)

package com.my.package;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class MessageListViewFragment extends ListFragment {
    OnLineSelectedListener mCallback;

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);

        // This makes sure that the container activity has implemented
        // the callback interface. If not, it throws an exception
        try {
            mCallback = (OnLineSelectedListener)activity;
        } catch (ClassCastException e) {
            throw new ClassCastException(activity.toString()
                    + " must implement OnLineSelectedListener");
        }
    }

    // Container Activity must implement this interface
    public interface OnLineSelectedListener {
        public void onListItemSelected(int position);
    }

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

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

        return inflater.inflate(R.layout.list_fragment, null);
    }
}

布局:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/fragment_container"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

第二个片段(底部)

package com.my.package;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class SendMessageFragment extends Fragment {
    OnSendButtonPressed mCallback;

    // Container Activity must implement this interface
    public interface OnSendButtonPressed {
        public void onSendButtonPressed();
    }

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

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

        return inflater.inflate(R.layout.input_fragment, null);
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);

        // This makes sure that the container activity has implemented
        // the callback interface. If not, it throws an exception
        try {
            mCallback = (OnSendButtonPressed)activity;
        } catch (ClassCastException e) {
            throw new ClassCastException(activity.toString()
                    + " must implement OnHeadlineSelectedListener");
        }
    }
}

布局:

<LinearLayout 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:orientation="horizontal">
    <EditText android:id="@+id/edit_message"
        android:layout_weight="1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:hint="@string/edit_message" />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/button_send" />
</LinearLayout>

推荐答案

执行以下操作:

  1. 将主活动 XML 中的 Fragment 更改为 FrameLayout,两者都适用.
  2. layout_widthfill_parent 更改为 match_parent,对于主 XML 文件中的 FrameLayout,(创建的在第 1 步).
  3. layout_heightfill_parent 更改为 wrap_content,对于主 XML 文件中的 FrameLayout,(创建的在第 1 步).
  4. 将 List Fragment XML 中的 FrameLayout 更改为 ListView,因为它是一个列表.
  5. 把这个LisView的id改成@android:id/list,因为ListFragment需要它.
  1. Change Fragment to FrameLayout in the main activity XML, for both.
  2. Change layout_width from fill_parent to match_parent, for both FrameLayout in the main XML file, (ones created in step 1).
  3. Change layout_height from fill_parent to wrap_content, for both FrameLayout in the main XML file, (ones created in step 1).
  4. Change FrameLayout to ListView in the List Fragment XML because it is a List.
  5. Change the id of this LisView to @android:id/list, because it is needed for the ListFragment.

然后告诉我,干杯.

编辑,还有这些:

  1. return inflater.inflate(R.layout.list_fragment, null);改为return inflater.inflate(R.layout.list_fragment, container, false);.
  2. return inflater.inflate(R.layout.input_fragment, null);改为return inflater.inflate(R.layout.input_fragment, container, false);

像这样制作您的主要活动 XML 文件:

Make your main activity XML file like this:

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

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

    <FrameLayout
        android:id="@+id/send_fragment"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true" />

</RelativeLayout>

我取出了 android:name"..." 因为我不知道那是什么,也无法找出它是什么,如果您确定知道它的作用,只需加回来,应该没问题.

I took out the android:name"..." because I don't know what that is nor able to find out what it is, if you know what it does for sure, just add it back, should be okay.

这篇关于Android 同一活动中的两个片段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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