Android的片段复制 [英] Android Fragment Duplication

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

问题描述

我想出Android蜂窝preVIEW新的类,我有一个小问题。我想提出一个棒球得分应用程序,我想在左边(操作菜单)按钮来切换操作窗格上的权利,我已经设置了一个片段。

我想按钮onClickListener()调用一个片段交易掉出来。到目前为止,它的工作原理,只是当应用程序加载时,它会创建默认的片段,但是当我打了一个按钮,而不是替换默认的片段,它会创建一个旁边全新的。

我已经看了好几个小时,我看不出我做错了什么......

请记住我是新来的Java / Android的/编程,所以我可能失去了一些东西那是非常明显。

hc_test.java(主要活动)

 包com.pte.hc_test;进口android.app.Activity;
进口android.app.Fragment;
进口android.app.FragmentTransaction;
进口android.os.Bundle;
进口android.view.View;
进口android.widget.Button;公共类hc_test延伸活动{
    / **当第一次创建活动调用。 * /    / *声明类级变量* /
//私人的LinearLayout触摸板;
//私人TextView的touchCoordText;    私人按钮pitchButton;
    私人按钮hitButton;    @覆盖
    公共无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.main);        pitchButton =(按钮)findViewById(R.id.actionButton1);
        hitButton =(按钮)findViewById(R.id.actionButton2);        pitchButton.setOnClickListener(新View.OnClickListener(){
            @覆盖
            公共无效的onClick(视图v){
                swapFragment(actionType.PITCH_ACTION);
            }
        });        hitButton.setOnClickListener(新View.OnClickListener(){
            @覆盖
            公共无效的onClick(视图v){
                swapFragment(actionType.HIT_ACTION);
            }
        });
    }    私人无效swapFragment(INT的myType){        片f =新actionFragment(的myType);        //执行事务时,替换任何现有的
        //这一个框架内的片段。
        FragmentTransaction英尺= getFragmentManager()调用BeginTransaction()。
        ft.replace(R.id.actionFragment,F);
        ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
        ft.addToBackStack(NULL);
        ft.commit();    }
}

main.xml中

 <?XML版本=1.0编码=UTF-8&GT?;<! - 这是整个画面的布局母公司 - >
<的LinearLayout
    的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:方向=垂直
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =FILL_PARENT
    机器人:背景=#2F2F4F
    >    <! - SCOREBOARD总纲 - >
    <的LinearLayout
        机器人:方向=横向
        机器人:layout_width =FILL_PARENT
        机器人:layout_height =WRAP_CONTENT
        >        <! - 队名的占位符 - >
        <的LinearLayout
            机器人:方向=垂直
            机器人:layout_width =WRAP_CONTENT
            机器人:layout_height =WRAP_CONTENT
            机器人:layout_weight =0
            机器人:填充=5DP
            >
            < TextView的风格=@风格/ inningLabel
                机器人:文字=TEAM
                机器人:layout_width =WRAP_CONTENT
                机器人:layout_height =WRAP_CONTENT
                />
            < TextView的风格=@风格/ inningValue
                机器人:文字=@字符串/访客
                机器人:layout_width =WRAP_CONTENT
                机器人:layout_height =WRAP_CONTENT
                />
            < TextView的风格=@风格/ inningValue
                机器人:文字=@字符串/家
                机器人:layout_width =WRAP_CONTENT
                机器人:layout_height =WRAP_CONTENT
                />
        < / LinearLayout中>
        <! - 结束球队的名字 - >        <! - 围垦表 - >
        < Horizo​​ntalScrollView
            机器人:layout_width =FILL_PARENT
            机器人:layout_height =WRAP_CONTENT
            机器人:layout_weight =1
            机器人:fadingEdge =垂直>            < TableLayout
                机器人:ID =@ + ID /局
                机器人:layout_width =WRAP_CONTENT
                机器人:layout_height =WRAP_CONTENT
                机器人:填充=5DP
                >
                <! - 围垦标签行 - >                <! - 让这些与code做? - >
                <的TableRow机器人:layout_height =WRAP_CONTENT>
                    < TextView的风格=@风格/ inningLabel
                        机器人:ID =@ + ID / inn1_label
                        机器人:文字=1
                        机器人:layout_height =WRAP_CONTENT
                        />
                    < TextView的风格=@风格/ inningLabel
                        机器人:ID =@ + ID / inn2_label
                        机器人:文字=2
                        机器人:layout_height =WRAP_CONTENT
                        />
                    < TextView的风格=@风格/ inningLabel
                        机器人:ID =@ + ID / inn3_label
                        机器人:文字=3
                        机器人:layout_height =WRAP_CONTENT
                        />
                    < TextView的风格=@风格/ inningLabel
                        机器人:ID =@ + ID / inn4_label
                        机器人:文字=4
                        机器人:layout_height =WRAP_CONTENT
                        />
                    < TextView的风格=@风格/ inningLabel
                        机器人:ID =@ + ID / inn5_label
                        机器人:文字=5
                        机器人:layout_height =WRAP_CONTENT
                        />
                    < TextView的风格=@风格/ inningLabel
                        机器人:ID =@ + ID / inn6_label
                        机器人:文字=6
                        机器人:layout_height =WRAP_CONTENT
                        />
                    < TextView的风格=@风格/ inningLabel
                        机器人:ID =@ + ID / inn7_label
                        机器人:文字=7
                        机器人:layout_height =WRAP_CONTENT
                        />
                    < TextView的风格=@风格/ inningLabel
                        机器人:ID =@ + ID / inn8_label
                        机器人:文字=8
                        机器人:layout_height =WRAP_CONTENT
                        />
                    < TextView的风格=@风格/ inningLabel
                        机器人:ID =@ + ID / inn9_label
                        机器人:文字=9
                        机器人:layout_height =WRAP_CONTENT
                        />
                < /&的TableRow GT;
                <! - 结束一局的标签 - >                <! - 顶级局一行 - >
                <的TableRow机器人:layout_height =WRAP_CONTENT>
                    < TextView的风格=@风格/ inningValue
                        机器人:ID =@ + ID / top1_label
                        机器人:文字=0
                        机器人:layout_height =WRAP_CONTENT
                        />
                    < TextView的风格=@风格/ inningValue
                        机器人:ID =@ + ID / top2_label
                        机器人:文字=0
                        机器人:layout_height =WRAP_CONTENT
                        />
                    < TextView的风格=@风格/ inningValue
                        机器人:ID =@ + ID / top3_label
                        机器人:文字=0
                        机器人:layout_height =WRAP_CONTENT
                        />
                    < TextView的风格=@风格/ inningValue
                        机器人:ID =@ + ID / top4_label
                        机器人:文字=0
                        机器人:layout_height =WRAP_CONTENT
                        />
                    < TextView的风格=@风格/ inningValue
                        机器人:ID =@ + ID / top5_label
                        机器人:文字=0
                        机器人:layout_height =WRAP_CONTENT
                        />
                    < TextView的风格=@风格/ inningValue
                        机器人:ID =@ + ID / top6_label
                        机器人:文字=0
                        机器人:layout_height =WRAP_CONTENT
                        />
                    < TextView的风格=@风格/ inningValue
                        机器人:ID =@ + ID / top7_label
                        机器人:文字=0
                        机器人:layout_height =WRAP_CONTENT
                        />
                    < TextView的风格=@风格/ inningValue
                        机器人:ID =@ + ID / top8_label
                        机器人:文字=0
                        机器人:layout_height =WRAP_CONTENT
                        />
                    < TextView的风格=@风格/ inningValue
                        机器人:ID =@ + ID / top9_label
                        机器人:文字=0
                        机器人:layout_height =WRAP_CONTENT
                        />
                < /&的TableRow GT;
                <! - 结束前一局行 - >                <! - 底排局 - >
                <的TableRow机器人:layout_height =WRAP_CONTENT>
                    < TextView的风格=@风格/ inningValue
                        机器人:ID =@ + ID / bot1_label
                        机器人:文字=0
                        机器人:layout_height =WRAP_CONTENT
                        />
                    < TextView的风格=@风格/ inningValue
                        机器人:ID =@ + ID / bot2_label
                        机器人:文字=0
                        机器人:layout_height =WRAP_CONTENT
                        />
                    < TextView的风格=@风格/ inningValue
                        机器人:ID =@ + ID / bot3_label
                        机器人:文字=0
                        机器人:layout_height =WRAP_CONTENT
                        />
                    < TextView的风格=@风格/ inningValue
                        机器人:ID =@ + ID / bot4_label
                        机器人:文字=0
                        机器人:layout_height =WRAP_CONTENT
                        />
                    < TextView的风格=@风格/ inningValue
                        机器人:ID =@ + ID / bot5_label
                        机器人:文字=0
                        机器人:layout_height =WRAP_CONTENT
                        />
                    < TextView的风格=@风格/ inningValue
                        机器人:ID =@ + ID / bot6_label
                        机器人:文字=0
                        机器人:layout_height =WRAP_CONTENT
                        />
                    < TextView的风格=@风格/ inningValue
                        机器人:ID =@ + ID / bot7_label
                        机器人:文字=0
                        机器人:layout_height =WRAP_CONTENT
                        />
                    < TextView的风格=@风格/ inningValue
                        机器人:ID =@ + ID / bot8_label
                        机器人:文字=0
                        机器人:layout_height =WRAP_CONTENT
                        />
                    < TextView的风格=@风格/ inningValue
                        机器人:ID =@ + ID / bot9_label
                        机器人:文字=0
                        机器人:layout_height =WRAP_CONTENT
                        />
                < /&的TableRow GT;
                <! - 结束底部局一行 - >            < / TableLayout>
        < / Horizo​​ntalScrollView>
        <! - 结束围垦表 - >        <! - 奔跑,命中,错误计数 - >
        < TableLayout
            机器人:ID =@ + ID / RHE
            机器人:layout_width =WRAP_CONTENT
            机器人:layout_height =WRAP_CONTENT
            机器人:layout_weight =0
            机器人:填充=5DP>            <的TableRow机器人:layout_height =WRAP_CONTENT>
                < TextView的风格=@风格/ inningLabel
                    机器人:ID =@ + ID / runLabel
                    机器人:文字=R
                    机器人:layout_height =WRAP_CONTENT
                    />
                < TextView的风格=@风格/ inningLabel
                    机器人:ID =@ + ID / hitLabel
                    机器人:文字=H
                    机器人:layout_height =WRAP_CONTENT
                    />
                < TextView的风格=@风格/ inningLabel
                    机器人:ID =@ + ID / errorLabel
                    机器人:文字=E
                    机器人:layout_height =WRAP_CONTENT
                    />
            < /&的TableRow GT;            <的TableRow机器人:layout_height =WRAP_CONTENT>
                < TextView的风格=@风格/ inningValue
                    机器人:ID =@ + ID / visitorRuns
                    机器人:文字=0
                    机器人:layout_height =WRAP_CONTENT
                    />
                < TextView的风格=@风格/ inningValue
                    机器人:ID =@ + ID / visitorHits
                    机器人:文字=0
                    机器人:layout_height =WRAP_CONTENT
                    />
                < TextView的风格=@风格/ inningValue
                    机器人:ID =@ + ID / visitorErrors
                    机器人:文字=0
                    机器人:layout_height =WRAP_CONTENT
                    />
            < /&的TableRow GT;            <的TableRow机器人:layout_height =WRAP_CONTENT>
                < TextView的风格=@风格/ inningValue
                    机器人:ID =@ + ID /本垒打
                    机器人:文字=0
                    机器人:layout_height =WRAP_CONTENT
                    />
                < TextView的风格=@风格/ inningValue
                    机器人:ID =@ + ID / homeHits
                    机器人:文字=0
                    机器人:layout_height =WRAP_CONTENT
                    />
                < TextView的风格=@风格/ inningValue
                    机器人:ID =@ + ID / homeErrors
                    机器人:文字=0
                    机器人:layout_height =WRAP_CONTENT
                    />
            < /&的TableRow GT;        < / TableLayout>    < / LinearLayout中>
    <! - SCOREBOARD布局完 - >    <! - 主要业务部分 - >
    <的LinearLayout
        机器人:方向=横向
        机器人:layout_width =FILL_PARENT
        机器人:layout_height =WRAP_CONTENT>        <! - 操作菜单BEGIN - >
        <的LinearLayout
            机器人:方向=垂直
            机器人:layout_width =FILL_PARENT
            机器人:layout_height =WRAP_CONTENT
            机器人:layout_weight =1>            <按钮
                机器人:ID =@ + ID / actionButton1
                机器人:文字=ActionButton1
                机器人:layout_width =FILL_PARENT
                机器人:layout_height =WRAP_CONTENT
                />
            <按钮
                机器人:ID =@ + ID / actionButton2
                机器人:文字=ActionButton2
                机器人:layout_width =FILL_PARENT
                机器人:layout_height =WRAP_CONTENT
                />
            <按钮
                机器人:ID =@ + ID / actionButton3
                机器人:文字=ActionButton3
                机器人:layout_width =FILL_PARENT
                机器人:layout_height =WRAP_CONTENT
                />
            <按钮
                机器人:ID =@ + ID / actionButton4
                机器人:文字=ActionButton4
                机器人:layout_width =FILL_PARENT
                机器人:layout_height =WRAP_CONTENT
                />
            <按钮
                机器人:ID =@ + ID / actionButton5
                机器人:文字=ActionButton5
                机器人:layout_width =FILL_PARENT
                机器人:layout_height =WRAP_CONTENT
                />        < / LinearLayout中>
        <! - 操作菜单END - >        <! - 动作帧BEGIN - >        <片段类=com.pte.hc_test.actionFragment
            机器人:ID =@ + ID / actionFragment
            机器人:layout_width =FILL_PARENT
            机器人:layout_height =WRAP_CONTENT
            机器人:layout_weight =1/>        <! - ACTION帧尾 - >    < / LinearLayout中>
    <! - 主要业务第一节结束​​ - >    <! - LIVE STATS FRAME BEGIN - >
    <的LinearLayout
        机器人:方向=横向
        机器人:layout_width =FILL_PARENT
        机器人:layout_height =WRAP_CONTENT>        <! - 离开现场统计窗格 - >
        <的LinearLayout
            机器人:ID =@ + ID / left_stat_pane
            机器人:方向=垂直
            机器人:layout_width =FILL_PARENT
            机器人:layout_height =WRAP_CONTENT
            机器人:layout_weight =1
            >            < TextView的风格=@风格/ statHeader
                机器人:ID =@ + ID / left_playerName
                机器人:文本={PITCHER NAME}
                机器人:layout_width =WRAP_CONTENT
                机器人:layout_height =WRAP_CONTENT
                />            < TextView的风格=@风格/ statText
                机器人:ID =@ + ID / left_stat1
                机器人:文字=化Stat1
                机器人:layout_width =WRAP_CONTENT
                机器人:layout_height =WRAP_CONTENT
                />
            < TextView的风格=@风格/ statText
                机器人:ID =@ + ID / left_stat2
                机器人:文字=STAT2
                机器人:layout_width =WRAP_CONTENT
                机器人:layout_height =WRAP_CONTENT
                />
            < TextView的风格=@风格/ statText
                机器人:ID =@ + ID / left_stat3
                机器人:文字=Stat3的
                机器人:layout_width =WRAP_CONTENT
                机器人:layout_height =WRAP_CONTENT
                />
        < / LinearLayout中>
        <! - 留底现场统计窗格 - >        <! - 右现场统计窗格 - >
        <的LinearLayout
            机器人:ID =@ + ID / right_stat_pane
            机器人:方向=垂直
            机器人:layout_width =FILL_PARENT
            机器人:layout_height =WRAP_CONTENT
            机器人:layout_weight =1
            >            < TextView的风格=@风格/ statHeader
                机器人:ID =@ + ID / right_playerName
                机器人:文本={面糊NAME}
                机器人:layout_width =WRAP_CONTENT
                机器人:layout_height =WRAP_CONTENT
                />            < TextView的风格=@风格/ statText
                机器人:ID =@ + ID / right_stat1
                机器人:文字=化Stat1
                机器人:layout_width =WRAP_CONTENT
                机器人:layout_height =WRAP_CONTENT
                />
            < TextView的风格=@风格/ statText
                机器人:ID =@ + ID / right_stat2
                机器人:文字=STAT2
                机器人:layout_width =WRAP_CONTENT
                机器人:layout_height =WRAP_CONTENT
                />
            < TextView的风格=@风格/ statText
                机器人:ID =@ + ID / right_stat3
                机器人:文字=Stat3的
                机器人:layout_width =WRAP_CONTENT
                机器人:layout_height =WRAP_CONTENT
                />        < / LinearLayout中>
        <! - 到底对不对现场统计窗格 - >    < / LinearLayout中>< / LinearLayout中>

actionFragment.java

 包com.pte.hc_test;进口android.app.Fragment;
进口android.content.Context;
进口android.os.Bundle;
进口android.util.Log;
进口android.view.LayoutInflater;
进口android.view.View;
进口android.view.ViewGroup;
进口android.widget.ImageView;
进口android.widget.LinearLayout;
进口android.widget.TableLayout;
进口android.widget.TableRow;
进口android.widget.TextView;公共类actionFragment扩展片段{    //类变量
    INT mActivityType;    //默认(null)构造函数
    公共actionFragment(){
        Log.v(PTE,空构造);
    }    公共actionFragment(INT N){
        mActivityType = N;
        Log.v(PTE,显式构造(+ N +));
    }    @覆盖
    公共无效的onCreate(捆绑保存){
        super.onCreate(保存);
        如果(救了!= NULL){
            mActivityType = saved.getInt(类型);
        }
        Log.v(PTE,火:actionFragment.onCreate());
    }    @覆盖
    公共无效的onSaveInstanceState(捆绑toSave){
        toSave.putInt(类型,mActivityType);
        Log.v(PTE,火:actionFragment.onSaveInstanceState());
    }    @覆盖
    公共查看onCreateView(LayoutInflater充气器,容器的ViewGroup,捆绑savedInstanceState){        Log.v(PTE,火:actionFragment.onCreateView());
        Log.v(PTE,带mActivityType ==+ mActivityType);
        尝试{
            Log.v(PTE,带onCreateView()的集装箱=+ container.toString());
        }赶上(例外五){
            Log.v(PTE,不能转换容器字符串必须是空。);
            Log.v(PTE,e.toString());
        }        上下文C = getActivity()getApplicationContext()。
        的LinearLayout actionPane =新的LinearLayout(C);        开关(mActivityType){
            案例actionType.PITCH_ACTION:
                Log.v(PTE,火:actionFragment.pitchPane());                //实例化所需的所有意见
                 //父容器
                TextView中特镭宝=新的TextView(C);
                TableLayout strikeZone =新TableLayout(C);
                ImageView的形象=新ImageView的(C);                //为每个视图设置属性
                actionPane.setOrientation(LinearLayout.VERTICAL);                topLabel.setText(顶标签文本);                //创建好球带表
                的for(int i = 1;我6;;我++){
                    TR的TableRow =新的TableRow(C);                    为(诠释J = 1; J&10 6; J ++){
                        TextView的电视=新的TextView(C);
                        tv.setText(C+ J +:R+ I);
                        tv.setPadding(3,3,3,3);
                        tr.addView(电视);
                    }
                    strikeZone.addView(TR);
                }                image.setPadding(0,60,0,30);
                image.setImageResource(R.drawable.homeplate);                //添加子视图父
                actionPane.addView(特镭宝);
                actionPane.addView(strikeZone);
                actionPane.addView(图片);                返回actionPane;            案例actionType.HIT_ACTION:
                Log.v(PTE,火:actionFragment.hitPane());                //简单的布局与用于测试的文本视图
                TextView的占位符=新的TextView(C);
                placeholder.setText(这是一个占位符);                actionPane.addView(占位符);
                返回actionPane;            默认:
                Log.v(PTE,火:actionFragment.defaultPane());                            //如果我评论这个TextView的时候,我达到我的预期的行为
                TextView的标签=新的TextView(C);
                label.setText(这是默认的窗格);
                actionPane.addView(标签);                返回actionPane;
        }
    }}


解决方案

在默认switch语句中把占位符文本是创建通过您的LinearLayout actionPane的视图。片段从未被添加到容器中。当你打你的按键,swapFragment方法被调用和片段加入到容器旁边actionPane视图。

如果您删除默认的actionPane的创作,你没有看到这个问题你注意。如果你想显示在最初推出的应用程序,以通过按键触发的片段被替换片段一些默认的视图或文字,可以在hc_test onCreate方法片段添加到容器中。当你火按钮这将被换出。我测试了这一点,它的作品,未必是最好的方式和它的凌晨2点,现在,请原谅我,如果我不写清楚!可以向您发送或张贴code,如果你喜欢。

我很好奇,如果你发现了一个教程,以帮助您设置片段的按钮?所有我能找到的是listFragment例子,挣扎着,直到我发现您的文章。

谢谢,

I'm trying out the new class in android Honeycomb preview, and I've got a small issue. I am making a baseball scoring app, and I want the buttons on the left (Action Menu) to switch the "Action Pane" on the right, which I've set up as a fragment.

I'd like the buttons' onClickListener() to call a Fragment Transaction to swap it out. So far it works, EXCEPT that when the app loads, it creates the default fragment, but when I hit a button, instead of REPLACING the default fragment, it creates a whole new one next to it.

I've looked for hours, and I can't see what I'm doing wrong...

Keep in mind I'm new to java/android/programming, so I might be missing something that's painfully obvious.

hc_test.java (main activity)

package com.pte.hc_test;

import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class hc_test extends Activity {
    /** Called when the activity is first created. */

    /* declare class-level variables */
//  private LinearLayout touchPad;
//  private TextView touchCoordText;

    private Button pitchButton;
    private Button hitButton;

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

        pitchButton = (Button)findViewById(R.id.actionButton1);
        hitButton = (Button)findViewById(R.id.actionButton2);

        pitchButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                swapFragment(actionType.PITCH_ACTION);
            }
        });

        hitButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                swapFragment(actionType.HIT_ACTION);
            }
        });
    }

    private void swapFragment(int myType){

        Fragment f = new actionFragment(myType);

        // Execute a transaction, replacing any existing
        // fragment with this one inside the frame.
        FragmentTransaction ft = getFragmentManager().beginTransaction();
        ft.replace(R.id.actionFragment, f);
        ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
        ft.addToBackStack(null);
        ft.commit();

    }
}

main.xml

<?xml version="1.0" encoding="utf-8"?>

<!-- this is the parent layout of the whole screen -->
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#2F2F4F"
    >

    <!-- SCOREBOARD MASTER LAYOUT -->
    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        >

        <!-- team name placeholders -->
        <LinearLayout
            android:orientation="vertical"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="0"
            android:padding="5dp"
            >
            <TextView style="@style/inningLabel"
                android:text="TEAM"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                />
            <TextView style="@style/inningValue"
                android:text="@string/visitor"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                />
            <TextView style="@style/inningValue"
                android:text="@string/home"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                />
        </LinearLayout>
        <!-- end team names -->

        <!-- Inning table -->
        <HorizontalScrollView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:fadingEdge="vertical">

            <TableLayout
                android:id = "@+id/innings"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:padding="5dp"
                >
                <!-- inning label row -->

                <!-- get these done with code?? -->
                <TableRow android:layout_height="wrap_content">
                    <TextView style="@style/inningLabel"
                        android:id="@+id/inn1_label"
                        android:text="1"
                        android:layout_height="wrap_content"
                        />
                    <TextView style="@style/inningLabel"
                        android:id="@+id/inn2_label"
                        android:text="2"
                        android:layout_height="wrap_content"
                        />
                    <TextView style="@style/inningLabel"
                        android:id="@+id/inn3_label"
                        android:text="3"
                        android:layout_height="wrap_content"
                        />
                    <TextView style="@style/inningLabel"
                        android:id="@+id/inn4_label"
                        android:text="4"
                        android:layout_height="wrap_content"
                        />
                    <TextView style="@style/inningLabel"
                        android:id="@+id/inn5_label"
                        android:text="5"
                        android:layout_height="wrap_content"
                        />
                    <TextView style="@style/inningLabel"
                        android:id="@+id/inn6_label"
                        android:text="6"
                        android:layout_height="wrap_content"
                        />
                    <TextView style="@style/inningLabel"
                        android:id="@+id/inn7_label"
                        android:text="7"
                        android:layout_height="wrap_content"
                        />
                    <TextView style="@style/inningLabel"
                        android:id="@+id/inn8_label"
                        android:text="8"
                        android:layout_height="wrap_content"
                        />
                    <TextView style="@style/inningLabel"
                        android:id="@+id/inn9_label"
                        android:text="9"
                        android:layout_height="wrap_content"
                        />
                </TableRow>
                <!-- end inning labels -->

                <!-- top inning row -->
                <TableRow android:layout_height="wrap_content">
                    <TextView style="@style/inningValue"
                        android:id="@+id/top1_label"
                        android:text="0"
                        android:layout_height="wrap_content"
                        />
                    <TextView style="@style/inningValue"
                        android:id="@+id/top2_label"
                        android:text="0"
                        android:layout_height="wrap_content"
                        />
                    <TextView style="@style/inningValue"
                        android:id="@+id/top3_label"
                        android:text="0"
                        android:layout_height="wrap_content"
                        />
                    <TextView style="@style/inningValue"
                        android:id="@+id/top4_label"
                        android:text="0"
                        android:layout_height="wrap_content"
                        />
                    <TextView style="@style/inningValue"
                        android:id="@+id/top5_label"
                        android:text="0"
                        android:layout_height="wrap_content"
                        />
                    <TextView style="@style/inningValue"
                        android:id="@+id/top6_label"
                        android:text="0"
                        android:layout_height="wrap_content"
                        />
                    <TextView style="@style/inningValue"
                        android:id="@+id/top7_label"
                        android:text="0"
                        android:layout_height="wrap_content"
                        />
                    <TextView style="@style/inningValue"
                        android:id="@+id/top8_label"
                        android:text="0"
                        android:layout_height="wrap_content"
                        />
                    <TextView style="@style/inningValue"
                        android:id="@+id/top9_label"
                        android:text="0"
                        android:layout_height="wrap_content"
                        />
                </TableRow>
                <!-- end top inning row -->

                <!-- bottom inning row -->
                <TableRow android:layout_height="wrap_content">
                    <TextView style="@style/inningValue"
                        android:id="@+id/bot1_label"
                        android:text="0"
                        android:layout_height="wrap_content"
                        />
                    <TextView style="@style/inningValue"
                        android:id="@+id/bot2_label"
                        android:text="0"
                        android:layout_height="wrap_content"
                        />
                    <TextView style="@style/inningValue"
                        android:id="@+id/bot3_label"
                        android:text="0"
                        android:layout_height="wrap_content"
                        />
                    <TextView style="@style/inningValue"
                        android:id="@+id/bot4_label"
                        android:text="0"
                        android:layout_height="wrap_content"
                        />
                    <TextView style="@style/inningValue"
                        android:id="@+id/bot5_label"
                        android:text="0"
                        android:layout_height="wrap_content"
                        />
                    <TextView style="@style/inningValue"
                        android:id="@+id/bot6_label"
                        android:text="0"
                        android:layout_height="wrap_content"
                        />
                    <TextView style="@style/inningValue"
                        android:id="@+id/bot7_label"
                        android:text="0"
                        android:layout_height="wrap_content"
                        />
                    <TextView style="@style/inningValue"
                        android:id="@+id/bot8_label"
                        android:text="0"
                        android:layout_height="wrap_content"
                        />
                    <TextView style="@style/inningValue"
                        android:id="@+id/bot9_label"
                        android:text="0"
                        android:layout_height="wrap_content"
                        />
                </TableRow>
                <!-- end bottom inning row -->

            </TableLayout>
        </HorizontalScrollView>
        <!-- end inning table -->

        <!-- Runs, Hits, Errors Count -->
        <TableLayout
            android:id = "@+id/RHE"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="0"
            android:padding="5dp">

            <TableRow android:layout_height="wrap_content">
                <TextView style="@style/inningLabel"
                    android:id="@+id/runLabel"
                    android:text="R"
                    android:layout_height="wrap_content"
                    />
                <TextView style="@style/inningLabel"
                    android:id="@+id/hitLabel"
                    android:text="H"
                    android:layout_height="wrap_content"
                    />
                <TextView style="@style/inningLabel"
                    android:id="@+id/errorLabel"
                    android:text="E"
                    android:layout_height="wrap_content"
                    />
            </TableRow>

            <TableRow android:layout_height="wrap_content">
                <TextView style="@style/inningValue"
                    android:id="@+id/visitorRuns"
                    android:text="0"
                    android:layout_height="wrap_content"
                    />
                <TextView style="@style/inningValue"
                    android:id="@+id/visitorHits"
                    android:text="0"
                    android:layout_height="wrap_content"
                    />
                <TextView style="@style/inningValue"
                    android:id="@+id/visitorErrors"
                    android:text="0"
                    android:layout_height="wrap_content"
                    />
            </TableRow>

            <TableRow android:layout_height="wrap_content">
                <TextView style="@style/inningValue"
                    android:id="@+id/homeRuns"
                    android:text="0"
                    android:layout_height="wrap_content"
                    />
                <TextView style="@style/inningValue"
                    android:id="@+id/homeHits"
                    android:text="0"
                    android:layout_height="wrap_content"
                    />
                <TextView style="@style/inningValue"
                    android:id="@+id/homeErrors"
                    android:text="0"
                    android:layout_height="wrap_content"
                    />
            </TableRow>

        </TableLayout>

    </LinearLayout>
    <!-- END OF SCOREBOARD LAYOUT -->

    <!-- MAIN ACTIVITY SECTION -->
    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content">

        <!-- ACTION MENU BEGIN -->
        <LinearLayout
            android:orientation="vertical"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1">

            <Button
                android:id="@+id/actionButton1"
                android:text="ActionButton1"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                />
            <Button
                android:id="@+id/actionButton2"
                android:text="ActionButton2"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                />
            <Button
                android:id="@+id/actionButton3"
                android:text="ActionButton3"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                />
            <Button
                android:id="@+id/actionButton4"
                android:text="ActionButton4"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                />
            <Button
                android:id="@+id/actionButton5"
                android:text="ActionButton5"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                />

        </LinearLayout>
        <!-- ACTION MENU END -->

        <!-- ACTION FRAME BEGIN -->

        <fragment class="com.pte.hc_test.actionFragment"
            android:id="@+id/actionFragment"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1" />

        <!-- ACTION FRAME END -->

    </LinearLayout>
    <!-- MAIN ACTIVITY SECTION END -->

    <!-- LIVE STATS FRAME BEGIN -->
    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content">

        <!-- left live stat pane -->
        <LinearLayout
            android:id="@+id/left_stat_pane"
            android:orientation="vertical"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            >

            <TextView style="@style/statHeader"
                android:id="@+id/left_playerName"
                android:text="{PITCHER NAME}"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                />

            <TextView style="@style/statText"
                android:id="@+id/left_stat1"
                android:text="Stat1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                />
            <TextView style="@style/statText"
                android:id="@+id/left_stat2"
                android:text="Stat2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                />
            <TextView style="@style/statText"
                android:id="@+id/left_stat3"
                android:text="Stat3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                />
        </LinearLayout>
        <!-- end left live stat pane -->

        <!-- right live stat pane -->
        <LinearLayout
            android:id="@+id/right_stat_pane"
            android:orientation="vertical"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            >

            <TextView style="@style/statHeader"
                android:id="@+id/right_playerName"
                android:text="{BATTER NAME}"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                />

            <TextView style="@style/statText"
                android:id="@+id/right_stat1"
                android:text="Stat1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                />
            <TextView style="@style/statText"
                android:id="@+id/right_stat2"
                android:text="Stat2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                />
            <TextView style="@style/statText"
                android:id="@+id/right_stat3"
                android:text="Stat3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                />

        </LinearLayout>
        <!-- end right live stat pane -->

    </LinearLayout>

</LinearLayout>

actionFragment.java

package com.pte.hc_test;

import android.app.Fragment;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;

public class actionFragment extends Fragment {

    // class variables
    int mActivityType;

    // default (null) constructor
    public actionFragment(){
        Log.v("PTE", "null constructor");       
    }

    public actionFragment(int n){
        mActivityType = n;
        Log.v("PTE", "explicit constructor (" + n + ")");
    }

    @Override
    public void onCreate(Bundle saved){
        super.onCreate(saved);
        if (saved != null){
            mActivityType = saved.getInt("Type");
        }
        Log.v("PTE", "FIRE: actionFragment.onCreate()");
    }

    @Override
    public void onSaveInstanceState(Bundle toSave){
        toSave.putInt("Type", mActivityType);
        Log.v("PTE", "FIRE: actionFragment.onSaveInstanceState()");
    }

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

        Log.v("PTE", "FIRE: actionFragment.onCreateView()");
        Log.v("PTE", "with mActivityType == " + mActivityType);
        try {
            Log.v("PTE", "with onCreateView()'s container = " + container.toString());
        } catch (Exception e) {
            Log.v("PTE", "could not convert container to string. Must be null");
            Log.v("PTE", e.toString());
        }

        Context c = getActivity().getApplicationContext();
        LinearLayout actionPane = new LinearLayout(c);

        switch (mActivityType) {
            case actionType.PITCH_ACTION:
                Log.v("PTE", "FIRE: actionFragment.pitchPane()");

                // instantiate all the required views
                 // parent container
                TextView topLabel = new TextView(c);
                TableLayout strikeZone = new TableLayout(c);
                ImageView image = new ImageView(c);

                // set properties for each view
                actionPane.setOrientation(LinearLayout.VERTICAL);

                topLabel.setText("top Label Text");

                // create the strike zone table
                for(int i=1; i<6; i++){
                    TableRow tr = new TableRow(c);

                    for(int j=1; j<6; j++){
                        TextView tv = new TextView(c);
                        tv.setText("C" + j + ":R" + i);
                        tv.setPadding(3, 3, 3, 3);
                        tr.addView(tv);
                    } 
                    strikeZone.addView(tr);
                }

                image.setPadding(0, 60, 0, 30);
                image.setImageResource(R.drawable.homeplate);

                // add child views to parent
                actionPane.addView(topLabel);
                actionPane.addView(strikeZone);
                actionPane.addView(image);

                return actionPane;

            case actionType.HIT_ACTION:
                Log.v("PTE", "FIRE: actionFragment.hitPane()");

                // simple layout with a text view for testing
                TextView placeholder = new TextView(c);
                placeholder.setText("This is a placeholder");

                actionPane.addView(placeholder);
                return actionPane;

            default:
                Log.v("PTE", "FIRE: actionFragment.defaultPane()");                 

                            // If I comment this TextView out, I achieve my intended behavior
                TextView label = new TextView(c);
                label.setText("This is the default pane");
                actionPane.addView(label);

                return actionPane;
        }
    }

}

解决方案

The placeholder text you were putting in the default for the switch statement was creating a view via your LinearLayout actionPane. A fragment was never added to the container. When you hit your buttons, the swapFragment method was called and a fragment added to the container next to the actionPane view.

If you delete the creation of the actionPane in the default you didn't see the issue as you noted. If you wanted to display some default view or text in a fragment for the initial launch of the app to be replaced by the fragments fired by the buttons, you can add a fragment to the container in the hc_test onCreate method. This will then be swapped out when you fire the buttons. I tested this and it works, may not be the best way and its 2 am now so please excuse me if I'm not writing clearly! Can send you or post the code if you like.

I'm curious if you found a tutorial to help you set up your fragments with the buttons? All I could find were listFragment examples and was struggling until I found your post.

Thanks,

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

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