0

我正在尝试使用简单的按钮来获得 Tab 功能


现在发生了什么::

  • 点击Button1---->F1活动显示
  • 点击button1(再次)----> F2Activity isplayed
  • 点击Button1(第三次) -----> F1Activity is Displayed again

- 与 Button2 wrt G1 & G2 活动类似


片段演示.java

public class FragmentDemo extends FragmentActivity implements OnClickListener {

    Button b1, b2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_fragment_demo);

        b1 = (Button) findViewById(R.id.button1);
        b1.setOnClickListener(this);

        b2 = (Button) findViewById(R.id.button2);
        b2.setOnClickListener(this);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.fragment_demo, menu);
        return true;
    }

    private boolean state = false;

       @Override
        public void onClick(View v) {
            switch (v.getId()) {
            case R.id.button1:
                state = !state;
                if (state) {
                addFragment(new F2(this), false,
                        FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
                } else {
                addFragment(new F1(this), false,
                        FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
                }
                break;

            case R.id.button2:
                state = !state;
                if (state) {
                addFragment(new G2(this), false,
                        FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
                } else {
                addFragment(new G1(this), false,
                        FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
                }
                break;    

            default:
                break;
            }

        }

    void addFragment(Fragment fragment, boolean addToBackStack, int transition) {
        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        ft.replace(R.id.simple_fragment, fragment);
        ft.setTransition(transition);
        if (addToBackStack)
            ft.addToBackStack(null);
        ft.commit();
    }

}

F1.java

public class F1 extends Fragment {

    Context c;
    View v;

    public F1(FragmentDemo fragmentDemo) {
        // TODO Auto-generated constructor stub
        this.c = fragmentDemo;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        // TODO Auto-generated method stub

        v = inflater.inflate(R.layout.f1, container, false);

        return v;
    }

}

F2.java

public class F2 extends Fragment {
    Context c;
    View v;

    public F2(FragmentDemo fragmentDemo) {
        // TODO Auto-generated constructor stub
        this.c = fragmentDemo;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        // TODO Auto-generated method stub

        v = inflater.inflate(R.layout.f2, container, false);

        return v;
    }
}

对于 G1 和 G2 类似

activity_fragment_demo.xml

<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="vertical"
    tools:context=".FragmentDemo" >

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

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

            <Button
                android:id="@+id/button1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="Button1" />

            <Button
                android:id="@+id/button2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="Button2" />

        </LinearLayout>

        <LinearLayout
            android:id="@+id/simple_fragment"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical" >
        </LinearLayout>
    </LinearLayout>

</LinearLayout>

输出::

在此处输入图像描述


很明显,我们可以看到,当我启动项目时,我来到了空白屏幕.....没有显示默认活动。就像在选项卡中一样

How can i make sure a default activity say F1 be already be present when i load the project

像这样::

在此处输入图像描述

任何想法 我需要在代码中进行哪些更改

希望我清楚

4

2 回答 2

0

根据他的评论中的建议user1950599,他给出了.....我做到了

只需要更新一行


分享这可能会帮助某人

public class FragmentDemo extends FragmentActivity implements OnClickListener {

    Button b1, b2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_fragment_demo);

        b1 = (Button) findViewById(R.id.button1);
        b1.setOnClickListener(this);

        b2 = (Button) findViewById(R.id.button2);
        b2.setOnClickListener(this);


        addFragment(new F1(this), false,
                FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.fragment_demo, menu);
        return true;
    }

    private boolean state = false;

       @Override
        public void onClick(View v) {
            switch (v.getId()) {
            case R.id.button1:
                state = !state;
                if (state) {
                addFragment(new F2(this), false,
                        FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
                } else {
                addFragment(new F1(this), false,
                        FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
                }
                break;

            case R.id.button2:
                state = !state;
                if (state) {
                addFragment(new G2(this), false,
                        FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
                } else {
                addFragment(new G1(this), false,
                        FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
                }
                break;    

            default:
                break;
            }

        }

    void addFragment(Fragment fragment, boolean addToBackStack, int transition) {
        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        ft.replace(R.id.simple_fragment, fragment);
        ft.setTransition(transition);
        if (addToBackStack)
            ft.addToBackStack(null);
        ft.commit();
    }

}
于 2013-10-15T05:29:24.903 回答
0

您的 Layout activity_fragment_demo 看起来不错,所以我将使用它。id 为 simple_fragment 的 LinearLayout 将是保存片段视图的容器

因此,假设片段 F1 代表您的第一个选项卡,片段 F2 代表您的第二个选项卡。

所以你应该有三种方法。

以下方法将每个片段添加到活动中,并且必须在活动的 onCreate 中为 F1 和 F2 调用

public void addFragment(Fragment fragment)
{
    FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
    ft.add(R.id.simple_fragment, fragment);
}

以下方法本质上显示了一个片段。我不确定附加的背后工作原理,但它可以看作是 View.VISIBLE

public void attachFragment(Fragment fragment)
{
    FragmentTransaction ft = getSupportManager().beginTransaction();
    ft.attach(fragment).commit();
}

以下方法本质上隐藏了一个片段。我不确定分离的背后工作原理,但它可以看作是 View.GONE

public void attachFragment(Fragment fragment)
{
    FragmentTransaction ft = getSupportManager().beginTransaction();
    ft.detach(fragment).commit();
}

您的 Activity 的 onCreate 方法

public void onCreate()
{
    //Create all your fragments here eg F1 f1 = new F1(); etc

    //For whatever fragment you have created you should call the method addFragment

    //Now depending on what fragment you want shown by default you should call attachFragment or detachFragment. eg. if F1 has to be shown by default
    attachFragment(F1);
    detachFragment(F2) //and all otherfragment you want hidden by default

    //Set the listeners for the buttons

    //The purpose of the next two lines is to store the current state of the buttons. Since F1 is attached to button1 and it is currently being shown we set the tag to "clicked"
    //and button2 tag has been set to notClicked
    button1.setTag("clicked");
    button2.setTag("notClicked");
}

在你的按钮的 onClickListener 里面

OnClickListener onTabButtonClickListener = new OnClickListener()
{
    public void onClick(View view)
    {
        switch(view.getId)
        {
            case R.id.button1
            {
                if(button1.getTag().equals("clicked"))
                {
                    detachFragment(F2);
                    attachFragment(F1);

                    button1.setTag("notClicked");
                    button2.setTag("clicked");
                }
                else
                {
                    detachFragment(F2);
                    attachFragment(F1);

                    button1.setTag("clicked");
                    button2.setTag("notClicked");
                }
            }
            case R.id.button2
            {
                //Same thing as before except opposite
            }
        }
    }
}

这应该可行。我不太擅长解释东西,所以请随时提出您的任何问题。

于 2013-10-15T05:48:53.223 回答