当前位置: 代码迷 >> 综合 >> ViewPager,RadioGroup,FragmentManager 详解 三 Fragment,ViewPager 和PagerTabStrip
  详细解决方案

ViewPager,RadioGroup,FragmentManager 详解 三 Fragment,ViewPager 和PagerTabStrip

热度:45   发布时间:2023-12-16 15:44:25.0

0:首先创建Fragment,

1:创建布局,注意了 PagerTabStrip 是在ViewPager 里面的,而且PagerTabStrip 必须要是全路劲

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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" android:paddingLeft="@dimen/activity_horizontal_margin"  android:paddingRight="@dimen/activity_horizontal_margin"  android:paddingTop="@dimen/activity_vertical_margin"  android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity"><android.support.v4.view.ViewPager  android:layout_width="match_parent"  android:layout_height="match_parent"  android:id="@+id/viewpage"><android.support.v4.view.PagerTabStrip  android:layout_width="wrap_content"  android:layout_height="wrap_content"  android:id="@+id/PagerTabStrip"></android.support.v4.view.PagerTabStrip></android.support.v4.view.ViewPager>
</LinearLayout>

2:创建adapter,这个里面 在getPagerTitle 进行Title的设置

package tech.androidstudio.pagetabstripdemo;import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;import java.util.List;/**  * Created by Kodulf on 2016/3/14.  */ public class PagerTabStripFragmentPagerAdapter extends FragmentPagerAdapter {List<Fragment> list;public PagerTabStripFragmentPagerAdapter(FragmentManager fm, List<Fragment> list) {super(fm);this.list = list;}@Override
    public Fragment getItem(int position) {return list.get(position);}@Override
    public int getCount() {return list.size();}@Override
    public CharSequence getPageTitle(int position) {
// return super.getPageTitle(position);  String ret;switch (position){case 0:ret="财经";break;case 1:ret="体育";break;case 2:ret="娱乐";break;default:ret="未命名";break;}return ret;}
}

3:MainActivity 里面只要进行简单的正常的设置就可以了,非常简单。

package tech.androidstudio.pagetabstripdemo;import android.support.v4.app.Fragment;
import android.support.v4.view.PagerTabStrip;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;import java.util.ArrayList;
import java.util.List;import tech.androidstudio.pagetabstripdemo.adapter.SlideTabFragmentPagerAdapter;public class MainActivity extends AppCompatActivity {private ViewPager mViewPager;private List<Fragment> mList;private PagerTabStrip mPagerTabStrip;@Override
    protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);mList = new ArrayList<Fragment>();mList.add(new FirstFragment());mList.add(new SecondFragment());mList.add(new ThirdFragment());mViewPager = (ViewPager) findViewById(R.id.viewpage);PagerTabStripFragmentPagerAdapter adapter = new PagerTabStripFragmentPagerAdapter(getSupportFragmentManager(), mList);mViewPager.setAdapter(adapter);}
}


  相关解决方案