我的Viewpager无法正常工作。我将从类似位置7的列表中选择一个项目,然后在我的寻呼机类中将其设置为0.如何将位置从一个页面传递到下一个页面?
选择项目的代码:
private void showItemSelectedAtPosition(int position) {
Threads selectedThread = mData.getItem(position);
Intent intent = new Intent(getActivity(), ActivePostPagerActivity.class);
intent.putExtra("Threads", Parcels.wrap(selectedThread.getThreadItem()));
intent.putExtra("ActiveTitle", "Active");
startActivity(intent);
}
并且这里是ActivePostPagerActivity中的页面加载:
public class ActivePostPagerActivity extends AppCompatActivity {
private ThreadResponse mData;
private ThreadFeedDataFactory mDataFactory;
private ThreadItem mThreads;
private ViewPager mViewPager;
private List<ThreadItem> mThreadItems;
private PostFragment mFragment;
private String mActiveString;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_post_pager);
//Load Parcel
Intent intent = getIntent();
mThreads = Parcels.unwrap(intent.getParcelableExtra("Threads"));
if (getIntent().getStringExtra("ActiveTitle") != null
&& getIntent().getStringExtra("ActiveTitle").equalsIgnoreCase("Active")) {
mActiveString = getIntent().getStringExtra("ActiveTitle");
} else if (getIntent().getStringExtra("ActiveTitle") != null
&& getIntent().getStringExtra("ActiveTitle").equalsIgnoreCase("Watching")) {
mActiveString = getIntent().getStringExtra("ActiveTitle");
} else if (getIntent().getStringExtra("ActiveTitle") != null
&& getIntent().getStringExtra("ActiveTitle").equalsIgnoreCase("I'm On")) {
mActiveString = getIntent().getStringExtra("ActiveTitle");
}
initUI();
}
private void initUI() {
mViewPager = (ViewPager) findViewById(R.id.postPager);
mThreadItems = new ArrayList<>();
mDataFactory = new ThreadFeedDataFactory(this.getApplicationContext());
//Toolbar
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbarForPosts);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayShowTitleEnabled(false);
toolbar.setNavigationIcon(R.drawable.ic_back);
toolbar.setTitle(mActiveString);
toolbar.setTitleTextColor(getResources().getColor(R.color.mnBlue));
toolbar.invalidate();
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
finish();
}
});
loadData();
}
private void loadData() {
if (getIntent().getStringExtra("ActiveTitle") != null
&& getIntent().getStringExtra("ActiveTitle").equalsIgnoreCase("Active")) {
mDataFactory.getActiveThreadFeed(new ThreadFeedDataFactory.ThreadFeedDataFactoryCallback() {
@Override
public void onThreadDataReceived(ThreadResponse response) {
mData = response;
for (int i = 0; i <= mData.getItems().size(); i++) {
if (mData != null
&& mData.getItem(i) != null
&& mData.getItem(i).getThreadItem() != null)
mThreadItems.add(mData.getItem(i).getThreadItem());
}
populateUIWithData();
}
@Override
public void onThreadDataFailed(Exception exception) {
}
});
} else if (getIntent().getStringExtra("ActiveTitle") != null
&& getIntent().getStringExtra("ActiveTitle").equalsIgnoreCase("Watching")) {
WatchedThreadsRequestData watchedThreadsRequestData = new WatchedThreadsRequestData(this);
watchedThreadsRequestData.getWatchedData(new WatchedThreadsRequestData.WatchedThreadsFeedback() {
@Override
public void onWatchedRequestReceived(ThreadResponse response) {
mData = response;
for (int i = 0; i <= mData.getItems().size(); i++) {
if (mData != null
&& mData.getItem(i) != null
&& mData.getItem(i).getThreadItem() != null)
mThreadItems.add(mData.getItem(i).getThreadItem());
}
populateUIWithData();
}
@Override
public void onWatchedRequestFailed(Exception exception) {
}
});
} else if (getIntent().getStringExtra("ActiveTitle") != null
&& getIntent().getStringExtra("ActiveTitle").equalsIgnoreCase("I'm On")) {
ThreadsImOnRequestData threadsImOnRequestData = new ThreadsImOnRequestData(this);
threadsImOnRequestData.getThreadsImOnData(new ThreadsImOnRequestData.ThreadsImOnFeedback() {
@Override
public void onThreadsImOnRequestReceived(ThreadResponse response) {
mData = response;
for (int i = 0; i <= mData.getItems().size(); i++) {
if (mData != null
&& mData.getItem(i) != null
&& mData.getItem(i).getThreadItem() != null)
mThreadItems.add(mData.getItem(i).getThreadItem());
}
populateUIWithData();
}
@Override
public void onThreadsImOnRequestFailed(Exception exception) {
}
});
}
}
private void populateUIWithData() {
FragmentManager fragmentManager = getSupportFragmentManager();
mViewPager.setAdapter(new FragmentPagerAdapter(fragmentManager) {
@Override
public Fragment getItem(int position) {
if (mThreadItems != null){
ThreadItem threadItem = mThreadItems.get(mViewPager.getCurrentItem());
mFragment = PostFragment.newInstance(threadItem.getId(), threadItem.getTopic_name(),
threadItem.getName(), threadItem.getNum_posts());
}
return mFragment;
}
@Override
public int getCount() {
return mThreadItems.size();
}
});
}
}
知道我做错了什么吗?可能很多,请不要杀了我。提前谢谢!