[英]How can I use the RecyclerView to open specific pages in ViewPager?
I have a list of items in RecyclerView, I want that when clicking on each item it goes to specific pages in a ViewPager in another activity.我在 RecyclerView 中有一个项目列表,我希望在单击每个项目时,它会转到另一个活动中 ViewPager 中的特定页面。
I'll assume you already have a listener for the item click on a recyclerview.我假设您已经有一个监听器,用于单击 recyclerview 上的项目。 When you send an intent for opening the other activity, you can add extra information , for example:当您发送打开其他活动的意图时,您可以添加额外信息,例如:
Intent intent = new Intent(this, ActivityWithPager.class);
int pageIndex = 2; // some page index you want to go to
intent.putExtra("page_number", pageIndex );
startActivity(intent);
Then, on your second activity, where you can access the ViewPager
, you can go to the page using:然后,在您可以访问ViewPager
的第二个活动中,您可以使用 go 访问页面:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// ..... do your other stuff
// Get the Intent that started this activity and extract the page index
Intent intent = getIntent();
int pageIndex= intent.getIntExtra("page_number", 0); // Zero as default page
// Tell the view pager to set the page index as current item
viewPager.setCurrentItem(pageIndex);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.