1. 程式人生 > >viewpager+fragment出現fragment不顯示問題

viewpager+fragment出現fragment不顯示問題

問題出現在adapter裡面,貼出原來有問題的adapter

package com.jingcai.fu.kuangjia.adapter;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.view.View;

import java.util.List;

/**
 * Created by 付
 * on 2016/3/15
 */
public class TopTabAdapter extends FragmentPagerAdapter  {
    private List<Fragment> fragments;
    private List<String> titles;
    private List<Integer > icons;

    public TopTabAdapter(FragmentManager fm, List<Fragment> fragments, List<String> titles, List<Integer> icons) {
        super(fm);
        this.fragments = fragments;
        this.titles = titles;
        this.icons = icons;

    }

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

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

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


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

樓主重寫了
isViewFromObject()
這個方法;官方對他的解釋

Determines whether a page View is associated with a specific key object as returned by instantiateItem(ViewGroup, int). This method is required for a PagerAdapter to function properly.

Parameters
view Page View to check for association with object
object Object to check for association with view
Returns
true if view is associated with the key object objectDetermines whether a page View is associated with a specific key object as returned by instantiateItem(ViewGroup, int). This method is required for a PagerAdapter to function properly.

Parameters
view Page View to check for association with object
object Object to check for association with view
Returnstrue if view is associated with the key object object

樓主也看不懂,嘿嘿嘿

所以就翻譯一下

決定一個頁面檢視與特定的重點物件返回的instantiateitem相關(ViewGroup,int)。該方法是一個PagerAdapter正常需要。

引數

檢視頁面檢視以檢查物件的關聯

物件物件以檢查關聯檢視

退換商品

如果檢視與關鍵物件決定是否頁面檢視與特定的重點物件返回的instantiateitem相關(ViewGroup,int)。該方法是一個PagerAdapter正常需要。

引數

檢視頁面檢視以檢查物件的關聯

物件物件以檢查關聯檢視

退換商品

如果檢視與關鍵物件物件關聯

然後你會發現fragment檢視沒有顯示出來,因為他是pagerAdapter需要的,但是我在這重寫了 ,所以導致fragemnt不顯示

把這個重寫的方法去掉就好了