0

在此处输入图像描述

我有一个ButtonFragment. 如果Button单击它必须Fragment用另一个替换Fragment. Fragmentsactivity_main.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:background="#efefef"
android:orientation="vertical"
tools:context=".MainActivity" >

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:weightSum="3" >

    <Button
        android:id="@+id/bHome"
        android:layout_width="wrap_content"
        android:layout_height="50dp"
        android:layout_weight="1"
        android:background="#000000"
        android:padding="3dp"
        android:text="Home"
        android:textColor="#ffffff" />

    <Button
        android:id="@+id/bEvents"
        android:layout_width="wrap_content"
        android:layout_height="50dp"
        android:layout_weight="1"
        android:background="#f8bd0e"
        android:padding="3dp"
        android:text="Events" />

    <Button
        android:id="@+id/bNearby"
        android:layout_width="wrap_content"
        android:layout_height="50dp"
        android:layout_weight="1"
        android:background="#f8bd0e"
        android:padding="3dp"
        android:text="Nearby" />
</LinearLayout>

<LinearLayout
    android:id="@+id/llFragment"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:orientation="horizontal" >

    <fragment
        android:id="@+id/fHome"
        android:name="com.example.fragmentstack.Home"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_margin="10dp" />

    <fragment
        android:id="@+id/fEvents"
        android:name="com.example.fragmentstack.Events"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_margin="10dp" />

    <fragment
        android:id="@+id/fNear"
        android:name="com.example.fragmentstack.NearBy"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_margin="10dp" />

    <FrameLayout
        android:id="@+id/fragment_container"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
    </FrameLayout>
</LinearLayout>

activity_main.xml 的输出是

在此处输入图像描述

主要活动:

public class MainActivity extends FragmentActivity implements OnClickListener {
Button home, events, near;
Fragment f1, f2, f3, f4;
FragmentManager manager;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    home = (Button) findViewById(R.id.bHome);
    events = (Button) findViewById(R.id.bEvents);
    near = (Button) findViewById(R.id.bNearby);

    home.setOnClickListener(this);
    events.setOnClickListener(this);
    near.setOnClickListener(this);

    manager = getSupportFragmentManager();
    f1 = manager.findFragmentById(R.id.fHome);
    f2 = manager.findFragmentById(R.id.fEvents);
    f3 = manager.findFragmentById(R.id.fNear);
}

@Override
public void onClick(View v) {
    switch (v.getId()) {
    case R.id.bHome:
        if (home.isPressed()) {
            home.setBackgroundColor(Color.BLACK);
            home.setTextColor(Color.WHITE);
            events.setBackgroundResource(R.color.Yellow);
            near.setBackgroundResource(R.color.Yellow);
            events.setTextColor(Color.BLACK);
            near.setTextColor(Color.BLACK);
            FragmentTransaction transaction = getSupportFragmentManager()
                    .beginTransaction();

            transaction.hide(f2);
            transaction.hide(f3);
            transaction.show(f1);
            transaction.commit();
        }
        break;
    case R.id.bEvents:
        if (events.isPressed()) {
            events.setBackgroundColor(Color.BLACK);
            events.setTextColor(Color.WHITE);
            home.setBackgroundResource(R.color.Yellow);
            near.setBackgroundResource(R.color.Yellow);
            home.setTextColor(Color.BLACK);
            near.setTextColor(Color.BLACK);
            FragmentTransaction transaction1 = getSupportFragmentManager()
                    .beginTransaction();
            transaction1.hide(f1);
            transaction1.hide(f3);
            transaction1.show(f2);
            transaction1.commit();
        }
        break;
    case R.id.bNearby:
        if (near.isPressed()) {
            near.setBackgroundColor(Color.BLACK);
            near.setTextColor(Color.WHITE);
            home.setBackgroundResource(R.color.Yellow);
            events.setBackgroundResource(R.color.Yellow);
            home.setTextColor(Color.BLACK);
            events.setTextColor(Color.BLACK);
            FragmentTransaction transaction2 = getSupportFragmentManager()
                    .beginTransaction();
            transaction2.hide(f1);
            transaction2.hide(f2);
            transaction2.show(f3);
            transaction2.commit();
        }
        break;
    }
}

  }

现在Home扩展Fragment. home 的 xml 布局有一个Button和一个。TextView在这里我将 onClicking Button 替换为Home Fragmentnew Update Profile Fragment

public class Home extends Fragment {
Button btn;
View view;  


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    view = inflater.inflate(R.layout.home, container, false);
    init();

    return view;
}

private void init() {
    btn = (Button) view.findViewById(R.id.bUpdate);
    btn.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            Fragment fg = new UpdateProfile();
            FragmentTransaction fragmentTransaction = getFragmentManager()
                    .beginTransaction();
            fragmentTransaction.replace(R.id.fragment_container, fg);
            fragmentTransaction.addToBackStack(null);
            fragmentTransaction.commit();
        }
    });

}

  }

R.id.fragment_container存在于activity_main.xml 中。现在我面临的问题是单击按钮时它没有用新的片段替换片段。

4

1 回答 1

1

现在我面临的问题是单击按钮时它没有用新的片段替换片段。

事务确实发生了,但是当您将该片段添加到将被推出屏幕的容器时它不可见(所有布局的片段的宽度都设置为,match_parent因此它们将填充LinearLayout' 的宽度并且FrameLayout将被推出屏幕)。此外,您在不包含先前片段的容器上执行替换事务,这意味着旧片段仍将在屏幕上可见(加上新添加的片段)。

您设置的系统不是处理您的场景的正确方法。从图像中,您似乎有一个类似布局的选项卡,在这种情况下,您应该只有一个容器,所有片段都将驻留在其中(以避免显示/隐藏正确片段的额外工作,并提供体面的 BACK 按钮体验)。

其次,如果您要使用这些片段工作(进行事务),您应该真正以编程方式添加它们,因为替换不会很好地处理静态片段。有很多关于如何制作带有片段的选项卡式布局的教程。

于 2013-04-13T07:19:30.863 回答