0

我用过片段

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>

片段演示.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;
    }
}

输出屏幕:-> 当我单击左侧的按钮时

在此处输入图像描述

同样对于下一个按钮

在此处输入图像描述


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

在此处输入图像描述

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

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

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


如何做到这一点!

4

2 回答 2

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;
        }
    }
于 2013-10-14T16:50:53.043 回答
0

Your Activity could keep track of the state with a boolean. For example like this:

   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;
        }

    }
于 2013-10-14T16:27:49.747 回答