相同活动中的Android两个片段

时间:2013-07-10 20:49:14

标签: android android-fragments

我知道以下帖子: Using Multiple Fragments in an single activity

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

的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文件。

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文件: First Fragment(列表片段在顶部)

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 个答案:

答案 0 :(得分:27)

执行以下操作:

  1. 将主要活动XML中的Fragment更改为FrameLayout
  2. layout_widthfill_parent更改为match_parent,对于主XML文件中的FrameLayout,(在步骤1中创建的那些)。
  3. layout_heightfill_parent更改为wrap_content,对于主XML文件中的FrameLayout,(在步骤1中创建的那些)。
  4. 将列表片段XML中的FrameLayout更改为ListView,因为它是一个列表。
  5. 将此LisView的ID更改为@android:id/list,因为ListFragment需要它。
  6. 然后让我知道,干杯。

    编辑,也是这些:

    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);
    3. 编辑:

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

      <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"...",因为我不知道它是什么,也不知道它是什么,如果你知道它确实是什么,只需加回来,应该没问题。