使用onClick()在选项卡式片段内导航

时间:2018-01-23 15:12:15

标签: java android

我有一个带有3个标签的Tabbed片段,每个标签都有自己的片段。第一个选项卡中有一个按钮,我想点击它并导航到选项卡片段中的其他2个片段之一(我选择过)。

到目前为止我累了一些没有成功的事情:

    Button placeAnORderNow = (Button) getView().findViewById(R.id.place_oreder);
    placeAnORderNow.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            FragmentTransaction fragmentTransaction = getChildFragmentManager().beginTransaction();
            fragmentTransaction.replace(getId(), new OrderNowFragment());
            fragmentTransaction.commit();
        }
    });

以下是TabbedFragment

public class ProductDetailsFragment extends Fragment {
@Nullable
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setRetainInstance(true);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    View view = inflater.inflate(R.layout.product_details,container, false);
    // Setting ViewPager for each Tabs
    ViewPager viewPager = (ViewPager) view.findViewById(R.id.viewpager_productDetails);
    setupViewPager(viewPager);
    // Set Tabs inside Toolbar
    TabLayout tabs = (TabLayout) view.findViewById(R.id.result_tabs_productDetails);
    tabs.setupWithViewPager(viewPager);
    MainActivity.headerName.setText("Product Details");

    return view;

}


// Add Fragments to Tabs
private void setupViewPager(ViewPager viewPager) {


    DispensaryDisplayFragment.Adapter adapter = new DispensaryDisplayFragment.Adapter(getChildFragmentManager());
    adapter.addFragment(new ProductInfoFragment(), "Info     ");
    adapter.addFragment(new OrderNowFragment(), "Order Now     ");
    adapter.addFragment(new ProductReviewsFragment(), "Reviews");;
    viewPager.setAdapter(adapter);



}

static class Adapter extends FragmentPagerAdapter {
    private final List<Fragment> mFragmentList = new ArrayList<>();
    private final List<String> mFragmentTitleList = new ArrayList<>();

    public Adapter(FragmentManager manager) {
        super(manager);
    }

    @Override
    public Fragment getItem(int position) {
        return mFragmentList.get(position);
    }

    @Override
    public int getCount() {
        return mFragmentList.size();
    }

    public void addFragment(Fragment fragment, String title) {
        mFragmentList.add(fragment);
        mFragmentTitleList.add(title);
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return mFragmentTitleList.get(position);
    }

}

}

标签片段的xml文件:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
xmlns:app="http://schemas.android.com/apk/res-auto">


<android.support.design.widget.CoordinatorLayout
    android:id="@+id/main_content_productDetails"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/appbar_productDetails"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

        <android.support.design.widget.TabLayout
            android:id="@+id/result_tabs_productDetails"
            android:background="@color/grey_button"
            app:tabSelectedTextColor="@color/black"
            android:layout_gravity="center"
            app:tabContentStart="0dp"
            app:tabIndicatorColor="@color/valet_green"
            app:tabTextColor="@color/black"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:tabGravity="fill"
            app:tabMode="fixed"/>

    </android.support.design.widget.AppBarLayout>

    <android.support.v4.view.ViewPager
        android:id="@+id/viewpager_productDetails"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior" />

</android.support.design.widget.CoordinatorLayout>

</RelativeLayout>

2 个答案:

答案 0 :(得分:0)

This will do it

viewPager.setCurrentItem(no of the fragment);

Note: Fragment numbering starts from 0

答案 1 :(得分:0)

要完成Rnld的回答,您实际上必须创建一个ViewPager并呼吁父片段为他提供正确的ID。

以下代码对我有用:

ViewPager viewPager = (ViewPager) getParentFragment().getView().findViewById(R.id.viewpager_productDetails);
viewPager.setCurrentItem(1);

将其放在onClick()内。