2 fenix14 fenix14 于 2013.07.14 15:14 提问

求指导,谢谢各位大神。 ViewPager和FragmentPagerAdapter实现切换,但是无法显示数据

想做个应用市场,效果如图,下面的是一级标签,用的TabHost跳转Activity实现。然后上面这一排是二级标签,想用TextView + ViewPager + Fragment 实现,现在滑动有效,但是数据加载不出来。求指教,捉急一整天了。
CSDN移动问答CSDN移动问答
这是“推荐”对应的Activity:

public class CommendActivity extends FragmentActivity {
private static final int INDEX_COM = 0;
private static final int INDEX_NEW = 1;
private static final int INDEX_HOT = 2;
private static final int INDEX_GUESS = 3;

private ImageView comPointer; // 指针
private TextView comCom, comNew, comHot, comGuess; // 标签
private ViewPager comPager;
private PagerAdapter pagerAdapter;
private List<CommendFragment> fragments;

private int currentPage = 0; // 当前二级页
private int delta = 0; // 指针单位移动量
private int offset = 0; // 指针移动偏移值

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_commend);

    initPointer();
    initTextView();
    initViewPager();
}

/*
 * 配置标签
 */
private void initTextView() {
    comCom = (TextView) findViewById(R.id.comCom);
    comNew = (TextView) findViewById(R.id.comNew);
    comHot = (TextView) findViewById(R.id.comHot);
    comGuess = (TextView) findViewById(R.id.comGuess);

    comClickListener listener = new comClickListener();
    comCom.setOnClickListener(listener);
    comNew.setOnClickListener(listener);
    comHot.setOnClickListener(listener);
    comGuess.setOnClickListener(listener);
}

/*
 * 配置标签指针
 */
private void initPointer() {
    comPointer = (ImageView) findViewById(R.id.comPointer);
    int imageWidth = BitmapFactory.decodeResource(getResources(), R.drawable.pointer).getWidth();
    DisplayMetrics dm = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(dm);
    delta = dm.widthPixels / 4;
    offset = (delta - imageWidth) / 2;
    Matrix matrix = new Matrix();
    matrix.postTranslate(offset, 0);
    comPointer.setImageMatrix(matrix);
}

/*
 * 配置ViewPager
 */
private void initViewPager() {
    comPager = (ViewPager) findViewById(R.id.comPager);

    fragments = new ArrayList<CommendFragment>();
    CommendFragment comFragment = CommendFragment.newInstance(INDEX_COM);
    CommendFragment newFragment = CommendFragment.newInstance(INDEX_NEW);
    CommendFragment hotFragment = CommendFragment.newInstance(INDEX_HOT);
    CommendFragment guessFragment = CommendFragment.newInstance(INDEX_GUESS);
    fragments.add(comFragment);
    fragments.add(newFragment);
    fragments.add(hotFragment);
    fragments.add(guessFragment);

    comPager.setAdapter(new TabsAdapter(getSupportFragmentManager(),
            fragments));
    comPager.setCurrentItem(currentPage);
    comPager.setOnPageChangeListener(new ComPageChangeListener());
}

private class comClickListener implements OnClickListener {
    @Override
    public void onClick(View v) {
        int id = v.getId();
        switch (id) {
        case R.id.comCom:
            comPager.setCurrentItem(INDEX_COM);
            break;
        case R.id.comNew:
            comPager.setCurrentItem(INDEX_NEW);
            break;
        case R.id.comHot:
            comPager.setCurrentItem(INDEX_HOT);
            break;
        case R.id.comGuess:
            comPager.setCurrentItem(INDEX_GUESS);
            break;
        }
    }
}

private class ComPageChangeListener implements OnPageChangeListener {
    @Override
    public void onPageScrollStateChanged(int state) {
    }

    @Override
    public void onPageScrolled(int position, float positionOffset,
            int positionOffsetPixels) {
    }

    @Override
    public void onPageSelected(int position) {
        Toast.makeText(
                CommendActivity.this,
                "this is "
                        + ((TabsAdapter)comPager.getAdapter()).getItem(position), Toast.LENGTH_SHORT)
                .show();
        Animation anim = new TranslateAnimation(delta * currentPage, delta
                * position, 0, 0);

        currentPage = position;
        anim.setFillAfter(true);
        anim.setDuration(300);
        comPointer.startAnimation(anim);
    }
}

}

Adapter的java代码:

public class TabsAdapter extends FragmentPagerAdapter {
private List<CommendFragment> fragments;

public TabsAdapter(FragmentManager fm, List<CommendFragment> list) {
    super(fm);
    this.fragments = list;
}

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

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

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

}

Fragment的java代码:

public class CommendFragment extends Fragment {
private static final int DEFAULT_TAG = -1;
private int tag;
private TextView tv;

/**
 * 添加标识,返回带标识的对象。
 * 
 * @param tag
 *            标识
 * @return 带标识的Fragment
 */
public static CommendFragment newInstance(int tag) {
    CommendFragment fragment = new CommendFragment();
    Bundle b = new Bundle();
    b.putInt("tag", tag);
    fragment.setArguments(b);
    return fragment;
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Bundle b = getArguments();
    tag = (null == b ? DEFAULT_TAG : b.getInt("tag"));
}

/**
 * 解析布局
 */
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_com, container, false);
    tv = (TextView) view.findViewById(R.id.empty);
    tv.setText(tv.getText().toString() + tag);

    return view;
}


@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);

}

}

3个回答

u010588886
u010588886   2014.10.12 18:44
已采纳

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

把适配器里面的这个方法去掉就可以了,我也是折腾了一天

u010426462
u010426462 同求解决方法?
一年多之前 回复
wzb1412
wzb1412 瞎扯,这方法能去掉??去掉就报错的好吧
接近 2 年之前 回复
sinat_28149767
sinat_28149767 回复哥也会丨掉毛: 数据出不来有多种原因,这只是可能的解决方案之一
接近 2 年之前 回复
sinat_28149767
sinat_28149767 同样折腾了半天,多谢
接近 2 年之前 回复
u013223154
u013223154 扯蛋根本没卵用.都没用到这个方法照样出不来
大约 2 年之前 回复
u012222371
u012222371 太棒了!!
2 年多之前 回复
coder_y_h_q
coder_y_h_q   2015.12.23 18:51

就是下面这个答案:@Override
public boolean isViewFromObject(View view, Object obj) {
return view == obj;
}

我的情况是,使用toolbar + tablayout + viewpager 做了一个滑动的选项卡效果。结果 fragment里面数据显示不出来。 注意我这里只有ViewPager下面是fragment,外面并没有包裹fragment。 至于有的人外面还有一层fragment,可能使用 getSupportFragmentManager 能够解决。还是要明白原理才行啊。

coder_y_h_q
coder_y_h_q   2015.12.23 18:47

6666666啊,虽然出错的原因有很多种,但是这个方法的确帮助我了,搞了半天,泥煤。

Csdn user default icon
上传中...
上传图片
插入图片