使用简单的片段在活动之间切换

时间:2013-10-14 16:12:08

标签: android android-fragments

我使用过片段

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="Button" />

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

        </LinearLayout>

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

</LinearLayout>

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);
        b2 = (Button) findViewById(R.id.button2);
        b1.setOnClickListener(this);
        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;
    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        switch (v.getId()) {
        case R.id.button1:

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

            break;
        case R.id.button2:
            addFragment(new F2(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;
    }
}

输出屏幕: - &gt;当我在左侧点击BUtton时

enter image description here

同样适用于下一个按钮

enter image description here


现在我如何合并下面的按钮

enter image description here

使用我使用的片段.....

  • 如果我点击按钮,Activity - A1必须在按钮中显示 下面.....
  • 同样如果我再次点击按钮Activity - A2必须显示 以下
  • 然后,如果我第三次点击...... Activity - A1必须显示 再次

单个按钮的布尔性质,重复其循环!


如何实现这一点!

2 个答案:

答案 0 :(得分:1)

有很多方法可以做到这一点,但这是一个简单的解决方案。

首先更改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" >

        <FrameLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" 
            android:layout_gravity="center_horizontal">

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

            <Button
                android:id="@+id/button2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Button" 
                android:visibility="gone"/>

        </FrameLayout>

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

</LinearLayout>
在FragmentDemo.java中接下来只需更改你的onClick方法:

@Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        switch (v.getId()) {
        case R.id.button1:

            addFragment(new F1(this), false,
                FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
            b1.setVisibility(View.GONE);
            b2.setVisibility(View.VISIBLE);

            break;
        case R.id.button2:
            addFragment(new F2(this), false,
                FragmentTransaction.TRANSIT_FRAGMENT_OPEN);

            b2.setVisibility(View.GONE);
            b1.setVisibility(View.VISIBLE);
            break;

        default:
            break;
        }
    }

答案 1 :(得分:0)

您的Activity可以使用boolean跟踪状态。例如:

   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;
        default:
            break;
        }

    }
相关问题