简体   繁体   English

Android:如何使用ViewPager滑动ListView的每个项目?

[英]Android : how to use ViewPager to swipe each item of my ListView?

I have a listview in my mainActivity and I have a detailActivity to display listview items. 我的mainActivity中有一个listview ,并且有一个detailActivity可显示listview项。

I want to swipe those items using ViewPager inside my detailActivity. 我想在我的detailActivity中使用ViewPager刷这些项目

mylistview is populated using a json feed. 使用json feed填充mylistview。

To implement the detailActivity , you need to supply a PageAdapter to the view pager. 要实现detailActivity,您需要向视图分页器提供一个PageAdapter。

Extend the FragmentPagerAdapter or FragmentStatePagerAdapter depending on the need and set it to the ViewPager. 根据需要扩展FragmentPagerAdapter或FragmentStatePagerAdapter并将其设置为ViewPager。

Pass the same json feed as to the listview in the main activity to the above adapter and implement it. 将与主活动中的listview相同的json feed传递给上述适配器并实现它。

I hope this helps. 我希望这有帮助。

There is many tutorials and examples on the web. 网上有许多教程和示例。 Just have a look at Android Developers page for example. 例如,仅查看Android开发人员页面。

If you are using Android Studio you can press right button on the package and create new Activity from template. 如果您使用的是Android Studio,则可以按包装上的右键,然后从模板创建新的活动。 Pick Tabbed Activity , it will contain layout with ViewPager and basic implementation. 选择Tabbed Activity ,它将包含带有ViewPager的布局和基本实现。

Are you trying to open new tabs when each ListView item is clicked? 单击每个ListView项时是否要尝试打开新选项卡? If so then you should in your onItemClick() method for the list view set the viewPager to the position of the desired fragment: 如果是这样,则应在列表视图的onItemClick()方法中将viewPager设置为所需片段的位置:

mPager.setCurrentItem(positionOfDesiredFragment);

Otherwise could you please explain your goal in more detail. 否则,请您详细说明您的目标。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM