Android studio:如何使用 ViewPager 添加标签
Posted
技术标签:
【中文标题】Android studio:如何使用 ViewPager 添加标签【英文标题】:Android studio: how add tabs with ViewPager 【发布时间】:2017-02-17 07:23:41 【问题描述】:我想在片段中添加一个带有 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_
android:layout_>
<LinearLayout
android:orientation="vertical"
android:layout_
android:layout_>
<android.support.v4.view.ViewPager
android:id="@+id/realtabcontent"
android:layout_
android:layout_
android:layout_weight="1"/>
</LinearLayout>
</android.support.v4.app.FragmentTabHost>
我尝试了几种解决方案,但都不起作用。 我需要使用片段,而不是片段活动。 编写的代码工作。
【问题讨论】:
使用 TabLayout 代替 TabHost 查看本教程androidhive.info/2015/09/… 【参考方案1】: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_
android:layout_
android:orientation="vertical">
<com.google.android.material.tabs.TabLayout
android:id="@+id/tablayout"
android:layout_
android:layout_ />
<androidx.viewpager.widget.ViewPager
android:id="@+id/viewpager"
android:layout_
android:layout_
android:layout_weight="1" />
</LinearLayout>
注意如果您还没有使用AndroidX,您需要在布局中更改以下内容。
将com.google.android.material.tabs.TabLayout
更改为android.support.design.widget.TabLayout
Chagne androidx.viewpager.widget.ViewPager
到 android.support.v4.view.ViewPager
但我强烈建议您迁移到 AndroidX,请参阅 @this answer 了解原因。
这是 common 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.
【讨论】:
【参考方案2】:使用的xml文件是这样的:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_
android:layout_>
<android.support.design.widget.TabLayout
android:id="@+id/tabLayout"
android:layout_
android:layout_
android:background="?attr/colorPrimary"
app:tabGravity="fill"
app:tabMode="fixed"
app:tabMaxWidth="0dp"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"/>
<!-- View pager to swipe views -->
<android.support.v4.view.ViewPager
android:id="@+id/pager"
android:layout_
android:layout_/>
</LinearLayout>
你的 java 文件在这里:
public class MyFragment extends Fragment
private View view;
private TabLayout tabLayout;
//This is our viewPager
private ViewPager viewPager;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
view = inflater.inflate(R.layout.sticker_fragment, container, false);
tabLayout = (TabLayout) view.findViewById(R.id.tabLayout);
viewPager = (ViewPager) view.findViewById(R.id.pager);
Viewpager adapter = new Viewpager(getActivity().getSupportFragmentManager(), getActivity());
//Adding adapter to pager
viewPager.setAdapter(adapter);
tabLayout.setupWithViewPager(viewPager);
tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener()
@Override
public void onTabSelected(TabLayout.Tab tab)
viewPager.setCurrentItem(tab.getPosition());
@Override
public void onTabUnselected(TabLayout.Tab tab)
@Override
public void onTabReselected(TabLayout.Tab tab)
);
return view;
在那个viewpager适配器之后是这样的:
public class Viewpager extends FragmentStatePagerAdapter
final int PAGE_COUNT = 2;
private String tabTitles[] = new String[]"Local","Online";
private Context context;
public Viewpager(FragmentManager fm, Context context)
super(fm);
this.context = context;
@Override
public Fragment getItem(int position)
switch (position)
case 0:
OneFragment oneFragment=new OneFragment();
return oneFragment;
case 1:
TwoFragment twoFragment=new TwoFragment();
return twoFragment;
default:
return null;
@Override
public int getCount()
return tabTitles.length;
@Override
public CharSequence getPageTitle(int position)
return tabTitles[position];
【讨论】:
以上是关于Android studio:如何使用 ViewPager 添加标签的主要内容,如果未能解决你的问题,请参考以下文章
如何设置android studio run release
如何解决android studio2.3导入android studio2.1的问题