在android中获取Tab功能的简单按钮

时间:2013-10-15 04:35:42

标签: java android android-fragments android-tabhost

我正在尝试使用简单的按钮

获得Tab功能

现在发生了什么::

  • 点击Button1 ---->显示F1活动
  • 点击button1(再次)----> F2活动已显示
  • 点击Button1(第三次)----->显示F1活动 再次

- 类似于Button2 w.r.t G1& G2活动


FragmentDemo.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>

输出:

enter image description here


显然我们可以看到,当我启动项目时,我会进入空白屏幕.....不会显示默认活动。就像在Tabs

中一样

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

喜欢这个 ::

enter image description here

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

希望我很清楚

2 个答案:

答案 0 :(得分: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();
    }

}

答案 1 :(得分:0)

您的布局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);
}

以下方法基本上显示了一个片段。我不确定attach的后面工作方式,但可以看作是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();
}

您的活动的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
            }
        }
    }
}

这应该有用。我不太善于解释东西,所以随时问你有任何问题。