gpt4 book ai didi

java - 使用 onClick() 在选项卡式 fragment 内导航

转载 作者:太空宇宙 更新时间:2023-11-04 10:46:01 25 4
gpt4 key购买 nike

我有一个带有 3 个选项卡的选项卡式 fragment ,每个选项卡都有自己的 fragment 。第一个选项卡内有一个按钮,我想单击该按钮并导航到选项卡式 fragment 内的其他 2 个 fragment 之一(无论我选择什么)。

这是我迄今为止一直没有成功的事情:

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

}

}

以及选项卡式 fragment 的 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>

最佳答案

这样就可以了

viewPager.setCurrentItem(no of the fragment);

注意: fragment 编号从0开始

关于java - 使用 onClick() 在选项卡式 fragment 内导航,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48404963/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com