Android片段中的导航



我尝试过使用带标签的Fragments。我的应用程序中有三个选项卡,FragA、FragB和FragC。在FragA课堂上,我有一个按钮。如果我点击了按钮,然后想要导航到类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;
    }
}

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

如何通过维护类似TabGroup的选项卡活动在FragA到FragNew之间进行导航。

这可能有助于您获得更多细节片段细节

/***Helper函数显示所选项目的详细信息,可以通过*在当前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);
    }
}

以下是如何从一个片段导航到一个新片段:

Fragment fragment = new FragNew();
    FragmentManager fm = this.getActivity().getFragmentManager();
    FragmentTransaction transaction = fm.beginTransaction();
    transaction.replace(R.id.container, fragment);
    transaction.commit();

希望这能有所帮助。

相关内容

  • 没有找到相关文章

最新更新