ViewPager + Fragment + TabLayout

製作如圖的界面

1、 在layout中          添加   Tablayout      和  ViewPager

<android.support.design.widget.TabLayout
    android:id="@+id/tablayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/base_titlebar_bg"
    app:tabIndicatorColor="#ff0000"
    app:tabIndicatorHeight="3dp"
    app:tabMode="fixed"
    app:tabSelectedTextColor="#ff0000"
    app:tabTextColor="#00ff00"/>

<android.support.v4.view.ViewPager
    android:id="@+id/vp_content"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>
2、創建 Fragment

mListFragment = new ArrayList<>();
mListFragment.add(new VedioListFragment("視頻"));
mListFragment.add(new BaseFragment("音樂"));

3、創建  fragmentManager

注意:必須是getSupportFragmentManager()

mFragmentManager = getSupportFragmentManager();
mFragmentPagerAdapter = new MyFragmentPagerAdapter(mFragmentManager);
4、給viewPager  添加 FragmentAdapter

vpContent.setAdapter(mFragmentPagerAdapter);
5、將tablayout   和 viewPager  關聯起來

注意:必須設置 兩個   一個viewpager   另外一個 viewPagerAdapter

tablayout.setupWithViewPager(vpContent);
tablayout.setTabsFromPagerAdapter(mFragmentPagerAdapter);

6、viewPagerAdapter

注意:1 、必須 複寫  public CharSequence getPageTitle(int position);方法  以便Tablayout 獲取 標題

2、@Override

public boolean isViewFromObject(View view, Object object) {
    return view == object;
}這個方法不能複寫   否則有可能   顯示Fragment 的內容  不正常(無法顯示內容)  
說起來全是淚 啊!我搞了一下午才發現這個問題! 到現在也不太明白!!!!!!!!!!

   public class MyFragmentPagerAdapter extends FragmentPagerAdapter {
        public MyFragmentPagerAdapter(FragmentManager fm ) {
            super(fm);
        }
        @Override
        public int getCount() {
            return mListFragment.size();
        }
        @Override
        public Fragment getItem(int position) {
            return mListFragment.get(position);
        }

/*
        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }
*/

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


以上fragment     fragmentManager     viewPager       FragmentPagerAdapter  都是support  v4  包中的

還有activity 要繼承AppCompatActivity     因爲support  design   還是 要support v7 包對他的支持

public class GuideActivity extends AppCompatActivity

還有一個問題就是tablayout  是support design 中的類  使用起來還是 有一些莫名的錯誤

我遇到的問題:   

.NoClassDefFoundError: android.support.v7.internal.widget.TintManager

以運行就包上面的錯誤,也不知道什麼問題,搞了很長時間,網上所導入的包又能不是同一個版本照成的  如下

比如都才用的  23.1.0 版本的  如果不是同一版本  有可能產生莫名的異常,所以儘量讓他們版本一致!!!!!!

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    testCompile 'junit:junit:4.12'
    compile files('libs/butterknife-6.0.0.jar')
    compile 'com.android.support:support-v4:23.1.0'
    compile 'com.android.support:appcompat-v7:23.1.0'
    compile 'com.android.support:design:23.1.0'
    compile project(':vitamiolibrary')
    compile project(':splashandguidlelibrary')
}

但是我一致了,還是沒有解決上面的  異常!!!!!!!!

我還是不明白,那麼我就把

 compile 'com.android.support:support-v4:23.1.0'

給去掉了,奇蹟發生了盡然  可以了顯示正常了!!!!!!!!!!!

但是然後我又將    它 添加回去  再次編譯一下,結果也不會產生那個異常了,真是很奇怪!!



再提下Fragement的小 知識  : onCreateView  只是必須重寫的方法

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

可以有很多種方式

1方式、 返回一個  textview     在 fragment中間顯示一個       哈哈哈

 public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
TextView textView = new TextView(getContext());
     textView.setText(title+ "哈哈哈 ");
     textView.setTextSize(30);
     textView.setGravity(Gravity.CENTER);
     container.addView(textView);  // 這句也可以不加
     return textView ;
 }

第2種方式 :

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = View.inflate(getActivity(),R.layout.item_layout,null);
        return view ;
    }

第3種方式:

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.item_layout,container,false);
        return view ;
    }
第一個參數  被填充的佈局文件         view的父控件    主要用來獲取一些 佈局參數     第三個參數   是否自動添加到父控件上(一般我們都是手動添加所以  一般設爲false) 通常都是false

如果 改爲true    

爆出了    java.lang.StackOverflowError   異常



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章