gpt4 book ai didi

安卓工作室 : how add tabs with ViewPager

转载 作者:行者123 更新时间:2023-11-29 17:13:23 27 4
gpt4 key购买 nike

我想在 fragment 中添加一个带有 pagerview(可滚动)的选项卡。

    public class MyFragment extends Fragment {
private FragmentTabHost tabHost;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
tabHost = new FragmentTabHost(getActivity());
tabHost.setup(getActivity(), getChildFragmentManager(), R.id.realtabcontent);
tabHost.addTab(tabHost.newTabSpec("one").setIndicator("One"), OneFragment.class, null);
tabHost.addTab(tabHost.newTabSpec("two").setIndicator("Two"), TwoFragment.class, null);
return tabHost;
}


@Override
public void onDestroyView(){
super.onDestroyView();
tabHost=null;
}
}

采用这种布局

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.app.FragmentTabHost
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent">

<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">

<android.support.v4.view.ViewPager
android:id="@+id/realtabcontent"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"/>

</LinearLayout>
</android.support.v4.app.FragmentTabHost>

我尝试了几种解决方案,但都不起作用。我需要使用 fragment ,而不是 fragment Activity 。编写的代码有效。

最佳答案

Tablayout + ViewPager 小代码

// find views by id
ViewPager vp= findViewById(R.id.viewpager);
TabLayout tl = findViewById(R.id.tablayout);

// attach tablayout with viewpager
tl.setupWithViewPager(vp);

ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());

// add your fragments
adapter.addFrag(new SampleFragment(), "Tab1");
adapter.addFrag(new SampleFragment(), "Tab2");
adapter.addFrag(new SampleFragment(), "Tab3");

// set adapter on viewpager
vp.setAdapter(adapter);

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:orientation="vertical">

<com.google.android.material.tabs.TabLayout
android:id="@+id/tablayout"
android:layout_width="match_parent"
android:layout_height="wrap_content" />

<androidx.viewpager.widget.ViewPager
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />

</LinearLayout>

注意 如果您没有使用 AndroidX但是,您需要更改以下布局。

  • com.google.android.material.tabs.TabLayout 更改为 android.support.design.widget.TabLayout
  • androidx.viewpager.widget.ViewPager 更改为 android.support.v4.view.ViewPager

但我强烈建议迁移到 AndroidX,请参阅 @this answer了解原因。

这是通用 ViewPagerAdapter 对于所有您在应用程序中的Viewpager。

public class ViewPagerAdapter extends FragmentStatePagerAdapter {
private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();

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

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

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

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

public void addFrag(Fragment fragment) {
mFragmentList.add(fragment);
mFragmentTitleList.add("");
}

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

如果需要在Fragment中设置ViewPager,请查看@this answer.

关于安卓工作室 : how add tabs with ViewPager,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39931728/

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