我有一个包含3个片段的ViewPager
,其中一个名为PostsFragment
的{{1}}包含RecyclerView
。 RecyclerView
根据帖子的类型(照片,视频或相册)保存3种不同的视图类型。一张专辑可以包含任意数量的照片和视频,其目的是能够在它们之间滑动,但我只看到我的TabLayout
点表示有多少照片/视频,而不是图像本身。
CarouselVideoFragment.kt
class CarouselVideoFragment() : Fragment() {
companion object Factory {
fun newInstance() : CarouselVideoFragment = CarouselVideoFragment()
}
lateinit var carouselMedia : InstagramCarouselMedia
private val videoThumb : ImageView by bindView(R.id.carousel_photo)
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
val bundle = arguments
carouselMedia = bundle?.get("media") as InstagramCarouselMedia
return inflater.inflate(R.layout.fragment_carousel_video, container, false)
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
videoThumb.visibility = View.VISIBLE
Log.d("CAROUSEL", "URL: ${carouselMedia.image_versions2.candidates[0].url}")
Picasso.with(context).load(carouselMedia.image_versions2.candidates[0].url).into(videoThumb)
videoThumb.setOnClickListener { _ ->
val intent = Intent(Intent.ACTION_VIEW)
val data = Uri.parse(carouselMedia.video_versions[0].url)
intent.setDataAndType(data, "video/*")
ContextCompat.startActivity(context!!, intent, null)
}
super.onViewCreated(view, savedInstanceState)
}
}
CarouselPhotoFragment.kt
class CarouselPhotoFragment : Fragment() {
companion object Factory {
fun newInstance() : CarouselPhotoFragment = CarouselPhotoFragment()
}
private val carouselPhoto : ImageView by bindView(R.id.carousel_photo)
lateinit var carouselMedia : InstagramCarouselMedia
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
val args = arguments
carouselMedia = args?.get("media") as InstagramCarouselMedia
return inflater.inflate(R.layout.fragment_carousel_photo, container, false)
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
carouselPhoto.visibility = View.VISIBLE
Log.d("CAROUSEL", "URL: ${carouselMedia.image_versions2.candidates[0].url}")
Picasso.with(context).load(carouselMedia.image_versions2.candidates[0].url).into(carouselPhoto)
super.onViewCreated(view, savedInstanceState)
}
}
PostsListAdapter.kt
class PostsListAdapter(private val postsFrag : PostsFragment,
private val items : List<InstagramTimelineFeedItem>) : RecyclerView.Adapter<RecyclerView.ViewHolder>() {
override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
when(holder.itemViewType) {
1 -> {
val viewHolder = holder as ViewHolder
val item = items[position]
if(item.media_or_ad != null) {
viewHolder.username.text = item.media_or_ad.user.username
Picasso.with(postsFrag.activity).load(item.media_or_ad.user.profile_pic_url).noFade().into(viewHolder.profilePic)
Picasso.with(postsFrag.activity).load(item.media_or_ad.image_versions2.candidates[0].url).into(viewHolder.postImage)
if(item.media_or_ad.caption != null) {
viewHolder.postCaption.text = item.media_or_ad.caption.get("text") as String
}
}
}
2 -> {
val viewHolder = holder as ViewHolderVideo
val item = items[position]
if(item.media_or_ad != null) {
viewHolder.username.text = item.media_or_ad.user.username
Picasso.with(postsFrag.activity).load(item.media_or_ad.user.profile_pic_url).noFade().into(viewHolder.profilePic)
Picasso.with(postsFrag.activity).load(item.media_or_ad.image_versions2.candidates[0].url).into(viewHolder.postVideoThumb)
viewHolder.postVideoThumb.setOnClickListener { _ ->
val intent = Intent(android.content.Intent.ACTION_VIEW)
val data = Uri.parse(item.media_or_ad.video_versions[0].url)
intent.setDataAndType(data, "video/*")
startActivity(postsFrag.context!!, intent, null)
}
if(item.media_or_ad.caption != null) {
viewHolder.postCaption.text = item.media_or_ad.caption.get("text") as String
}
}
}
else -> {
// Album type
val viewHolder = holder as ViewHolderCarousel
val item = items[position]
viewHolder.username.text = item.media_or_ad.user.username
Picasso.with(postsFrag.activity).load(item.media_or_ad.user.profile_pic_url).noFade().into(viewHolder.profilePic)
val adapter = CarouselAdapter(postsFrag.childFragmentManager, item.media_or_ad.carousel_media)
viewHolder.viewPager.adapter = adapter
viewHolder.tabs.setupWithViewPager(viewHolder.viewPager)
viewHolder.caption.text = item.media_or_ad.caption.get("text").toString()
}
}
}
override fun getItemViewType(position: Int): Int {
val item = items[position]
if(item.media_or_ad == null)
return 5
return item.media_or_ad.media_type
}
override fun getItemCount(): Int {
return items.size
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
when(viewType) {
1 -> return ViewHolder(LayoutInflater.from(postsFrag.activity).inflate(R.layout.post_list_item, parent, false))
2 -> return ViewHolderVideo(LayoutInflater.from(postsFrag.activity).inflate(R.layout.post_list_item_video, parent, false))
else -> {
return ViewHolderCarousel(LayoutInflater.from(postsFrag.activity).inflate(R.layout.post_list_item_carousel, parent, false))
}
}
}
inner class CarouselAdapter(fm: FragmentManager,
val carouselMedia : List<InstagramCarouselMedia>) : FragmentPagerAdapter(fm) {
override fun getCount(): Int {
return carouselMedia.size
}
override fun getItem(position: Int): Fragment {
Log.d("ADAPTER", "Type: ${carouselMedia[position].media_type}")
if(carouselMedia[position].media_type == 1) {
val frag = CarouselVideoFragment.newInstance()
val args = Bundle()
args.putSerializable("media", carouselMedia[position])
frag.arguments = args
return frag
} else if(carouselMedia[position].media_type == 2) {
val frag = CarouselVideoFragment.newInstance()
val args = Bundle()
args.putSerializable("media", carouselMedia[position])
frag.arguments = args
return frag
}
return Fragment()
}
}
class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
val profilePic : CircleImageView by bindView(R.id.profile_image)
val username : TextView by bindView(R.id.username)
val postImage : ImageView by bindView(R.id.post_image)
val postCaption : TextView by bindView(R.id.post_caption)
}
class ViewHolderVideo(itemView: View) : RecyclerView.ViewHolder(itemView) {
val profilePic : CircleImageView by bindView(R.id.profile_image)
val username : TextView by bindView(R.id.username)
val postCaption : TextView by bindView(R.id.post_caption)
val postVideoThumb : ImageView by bindView(R.id.video_thumb)
}
class ViewHolderCarousel(itemView : View) : RecyclerView.ViewHolder(itemView) {
val profilePic : CircleImageView by bindView(R.id.profile_image)
val username : TextView by bindView(R.id.username)
val viewPager : ViewPager by bindView(R.id.carousel_viewpager)
val tabs : TabLayout by bindView(R.id.carousel_tabs)
val caption : TextView by bindView(R.id.post_caption)
}
}
post_list_item_carousel.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical">
<LinearLayout
android:id="@+id/header"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center_vertical"
android:padding="12dp">
<de.hdodenhof.circleimageview.CircleImageView
android:id="@+id/profile_image"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_gravity="center_vertical"/>
<TextView
android:id="@+id/username"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="16sp"
android:layout_marginStart="10dp"
android:layout_gravity="center_vertical"/>
</LinearLayout>
<android.support.design.widget.TabLayout
android:id="@+id/carousel_tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabGravity="center"
app:tabIndicatorHeight="0dp"
app:tabBackground="@drawable/tab_selector"
android:layout_gravity="center_horizontal" />
<android.support.v4.view.ViewPager
android:id="@+id/carousel_viewpager"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/post_caption"
android:layout_width="320dp"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"/>
</LinearLayout>
fragment_carousel_video.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/carousel_photo"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</RelativeLayout>
fragment_carousel_photo.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/carousel_photo"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"/>
<ImageView
android:src="@drawable/play_button"
android:layout_width="55dp"
android:layout_height="55dp"
android:layout_centerInParent="true"/>
</RelativeLayout>
我现在拥有的: