FragmentPagerAdapter getItem()显示位置1而不是0

时间:2016-05-03 14:15:32

标签: android fragmentpageradapter

我在这里看了大部分答案,找不到对我有用的东西。

我有一个名为ActiveThreads的片段我希望每当用户点击此页面上的某个项目时,它就会加载一个pagerActivity,它使用FragmentPagerActivity,用户可以滚动它。

我遇到的问题是,当我点击一个项目时,它会显示NEXT项目。我通过调试运行它,它调用当前位置,然后调用下一个位置。然而它首先显示下一个位置而不是第一个?知道我做错了什么吗?

PostPager Activity的代码getItem方法

private void populateUIWithData() {
    FragmentManager fragmentManager = getSupportFragmentManager();
    mViewPager.setAdapter(new FragmentPagerAdapter(fragmentManager) {
        @Override
        public Fragment getItem(int position) {
            if (mThreadItems != null){
                mThreads = mThreadItems.get(position);
                mFragment = PostFragment.newInstance(mThreads.getId(), mThreads.getTopic_name(),
                        mThreads.getName(), mThreads.getNum_posts());
                mViewPager.getCurrentItem();
            }
            return mFragment;
        }

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

        @Override
        public boolean isViewFromObject(View view, Object object) {
            if (object != null) {
                return ((Fragment) object).getView() == view;
            } else {
                return false;
            }
        }
    });
}

这是我在PostFragment中的新实例

public static PostFragment newInstance(String threadID, String topicName, String threadName, String postCount) {
    mThreadID = threadID;
    mTopicName = topicName;
    mThreadName = threadName;
    mPostCount = postCount;

    Bundle args = new Bundle();
    args.putSerializable("threadID", threadID);
    args.putSerializable("topicName", topicName);
    args.putSerializable("threadName", threadName);
    args.putSerializable("postCount", postCount);

    mCurrentFragment = new PostFragment();

    mCurrentFragment.setArguments(args);

    return mCurrentFragment;
}

编辑这是我按下某个项目时活动线程的代码

   private void initUI() {
    mRecyclerView = (RecyclerView) mRootView.findViewById(R.id.threadRecyclerView);
    mLayoutManager = new GridLayoutManager(getActivity(), 1);
    mRecyclerView.setLayoutManager(mLayoutManager);
    mSwipeRefreshLayout = (SwipeRefreshLayout) mRootView.findViewById(R.id.swipeForNewThreads);
    mSwipeRefreshLayout.setColorSchemeResources(R.color.blue, R.color.green, R.color.yellow, R.color.red);

    mSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
        @Override
        public void onRefresh() {
            loadNewData();
        }
    });

    mRecyclerView.addOnItemTouchListener(new RecyclerItemClickListener
            (getActivity(), new RecyclerItemClickListener.OnItemClickListener() {
                @Override
                public void onItemClick(View view, int position) {
                    showItemSelectedAtPosition(position);

                }
            }));
    loadData();

}

private void loadNewData() {

    new Handler().postDelayed(new Runnable() {
        @Override
        public void run() {
            loadData();
            mAdapter.notifyDataSetChanged();
            mSwipeRefreshLayout.setRefreshing(false);
        }
    }, 0);

}

private void loadData() {

    mDataFactory = new ThreadFeedDataFactory(getActivity());
    mDataFactory.getActiveThreadFeed(new ThreadFeedDataFactory.ThreadFeedDataFactoryCallback() {
        @Override
        public void onThreadDataReceived(ThreadResponse response) {
            mData = response;
            populateUIWithData();

        }

        @Override
        public void onThreadDataFailed(Exception exception) {

        }
    });

}

private void populateUIWithData() {


    mAdapter = new ThreadAdapter(getActivity(), mData);
    mRecyclerView.setAdapter(mAdapter);
}

private void showItemSelectedAtPosition(int position) {
    Threads selectedThread = mData.getItem(position);

    Intent intent = new Intent(getActivity(), PostPagerActivity.class);
    intent.putExtra("Threads", Parcels.wrap(selectedThread.getThreadItem()));

    startActivity(intent);

}

查看showSelectedItemOnPosition方法,这是我加载PostPagerActivity OnClick的地方。有任何想法吗?

编辑来自我的日志猫

05-03 10:47:54.857 7894-7894/com.mumsnet.android E/threadItemID: 2628772
05-03 10:47:54.865 7894-7894/com.mumsnet.android E/POSTFRAG: threadItemID: 2628772
05-03 10:47:54.867 7894-7894/com.mumsnet.android E/threadItemID: 2628697
05-03 10:47:54.867 7894-7894/com.mumsnet.android E/POSTFRAG: threadItemID: 2628697
05-03 10:47:54.916 7894-7894/com.mumsnet.android E/LoadData: threadItemID: 2628697
05-03 10:47:54.925 7894-7894/com.mumsnet.android E/LoadData: threadItemID: 2628697

你能看到id的变化吗?它显示与第二个ID相关的项目! :(

提前致谢

1 个答案:

答案 0 :(得分:0)

看来你的mThreadItems.get(position);返回错误的对象。