2

我尝试使用带有标签的片段。我的应用程序中有三个选项卡,FragA、FragB、FragC。在 FragA 类中,我有一个按钮。如果我点击了按钮,那么想导航到 Class FragNew。我怎样才能做到这一点..

这是我的 FragA 课程:

public class AFragment extends Fragment {

    Button next;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.afragment, container, false);
        next = (Button)view.findViewById(R.id.button1);

        next.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Toast.makeText(getActivity(), "FragmentA Clicked...", Toast.LENGTH_SHORT).show();
            }
        });

        return view;

    }

}

片段.xml:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#85ff34"
    android:orientation="vertical" >

    <TextView android:id="@+id/textView1"
        android:text="Fragment A"
        android:textColor="#000000"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

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

</LinearLayout>

我的 FragNew 课程是:

public class FragNew extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.bfragment, container, false);
    }
    }

这是我的要求的看法:

MainTabActivity
  ActivityInTab1
    Fragment1 -> Fragment2 -> Fragment3
  ActivityInTab2
    Fragment4
  ActivityInTab3
    Fragment5 -> Fragment6

如何在 FragA 到 FragNew 之间进行导航,并维护 TabGroup Activity 之类的选项卡。

4

2 回答 2

0

以下是从片段导航到新片段的方法:

Fragment fragment = new FragNew();
    FragmentManager fm = this.getActivity().getFragmentManager();

    FragmentTransaction transaction = fm.beginTransaction();

    transaction.replace(R.id.container, fragment);
    transaction.commit();

希望这可以帮助。

于 2014-04-24T18:04:21.180 回答
0

这可能会帮助您了解更多详细信息片段详细信息

/** * 帮助函数显示所选项目的详细信息,通过 * 在当前 UI 中就地显示片段,或启动显示它的全新活动。*/

void showDetails(int index) {
    mCurCheckPosition = index;

    if (mDualPane) {
        // We can display everything in-place with fragments, so update
        // the list to highlight the selected item and show the data.
        getListView().setItemChecked(index, true);

        // Check what fragment is currently shown, replace if needed.
        DetailsFragment details = (DetailsFragment)
                getFragmentManager().findFragmentById(R.id.details);
        if (details == null || details.getShownIndex() != index) {
            // Make new fragment to show this selection.
            details = DetailsFragment.newInstance(index);

            // Execute a transaction, replacing any existing fragment
            // with this one inside the frame.
            FragmentTransaction ft = getFragmentManager().beginTransaction();
            ft.replace(R.id.details, details);
            ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
            ft.commit();
        }

    } else {
        // Otherwise we need to launch a new activity to display
        // the dialog fragment with selected text.
        Intent intent = new Intent();
        intent.setClass(getActivity(), DetailsActivity.class);
        intent.putExtra("index", index);
        startActivity(intent);
    }
}
于 2012-09-04T11:29:17.537 回答