如何获取PageAdapter的instantiateItem()发送的正确结束位置?

时间:2017-10-01 10:50:58

标签: android listview android-pageradapter

我的pageView应该显示相同的列表视图,但每次都有不同的信息。

通过发送到函数instantiateItem的位置参数选择信息:

public Object instantiateItem(ViewGroup collection, int position) 

位置不正确的问题因为我从来没有位置= 1。它总是0或2。

class MyPagerAdapter extends PagerAdapter
{

    ListView myListView;

    @Override
    public Object instantiateItem(ViewGroup collection, int position) {
        Log.v("position",position+"");
        LayoutInflater inflater = LayoutInflater.from(MyAndAllWordsActivity.this);
        ViewGroup myView = (ViewGroup) inflater.inflate(R.layout.my_view_pager_list_view, collection, false);
         myListView =(ListView)myView.findViewById(R.id.my_list_view);
        MyCustomAdapter listViewAdapter = setDataForListView(position);
        myListView.setAdapter(listViewAdapter);
        collection.addView(myView);
        return myView;
    }

    @Override
    public void destroyItem(ViewGroup collection, int position, Object view) {
        collection.removeView((View) view);
    }
    @Override
    public int getCount() {
        return 3;
    }
    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }
    @Override
    public CharSequence getPageTitle(int position) {
        String tabTitel =null;
        switch (position){
            case 0 :
                tabTitel= "Namen";
                break;
            case 1 :
                tabTitel= "Adjektive";
                break;
            case 2 :
                tabTitel= "Verben";
                break;
        }
        return tabTitel;

    }
}

mainActivity:

 @Override
protected void onCreate(Bundle savedInstanceState) {
     TabLayout mTabLayout;
    SearchView mSearchView;
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_my_and_all_words);
    ViewPager mPager=(ViewPager) findViewById(R.id.pager);
    mPager.setAdapter(new MyPagerAdapter());
    //mPager.setCurrentItem(mPager.getCurrentItem());
    mTabLayout = (TabLayout) findViewById(R.id.tab_layout);
    mTabLayout.setupWithViewPager(mPager);

0 个答案:

没有答案