RobertApikyan / SegmentedControl

Android SegmentedControl + multi row support
Apache License 2.0
162 stars 30 forks source link

Can't send list from Kotlin code to addSegments() method #21

Closed draskosaric closed 5 years ago

draskosaric commented 5 years ago

Hi,

this is my segment_item.xml:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools"
              android:orientation="vertical"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
             >
    <ImageView
            android:id="@+id/segmented_image"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:layout_gravity="center"
    />
</FrameLayout>

These are SegmentAdapter and SegmentViewHolder :

    class LiveSportsSegmentAdapter : SegmentAdapter<Long, LiveSportsSegmentAdapter.LiveSportsSegmentViewHolder>() {
        override fun onCreateViewHolder(
            layoutInflater: LayoutInflater,
            parent: ViewGroup?,
            viewType: Int
        ): LiveSportsSegmentViewHolder {
            return LiveSportsSegmentViewHolder(layoutInflater.inflate(R.layout.segment_item, null))
        }

        class LiveSportsSegmentViewHolder(segmentView: View) : SegmentViewHolder<Long>(segmentView), LayoutContainer {
            override val containerView: View?
            init {
                containerView = segmentView
            }
            override fun onSegmentBind(segmentData: Long?) {
                segmented_image.setImageDrawable(ContextCompat.getDrawable(segmented_image.context, SportHandler.getIconBySport(segmentData ?: 0)))
            }
        }
    }

In Fragment I tried to set segmentedControl this way:

override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)
        top_matches_segmented.setAdapter(LiveSportsSegmentAdapter())
        val list: List<Long> = listOf(SportHandler.AMERICAN_FOOTBALL, SportHandler.FOOTBALL)

        (top_matches_segmented as SegmentedControl).addSegments( list )

    }

For last line I get this error:

None of the following functions can be called with the arguments supplied: public open fun addSegments(p0: (Nothing..Array<out Nothing!>?)): Unit defined in xxx.SegmentedControl public open fun addSegments(p0: (Nothing..List<Nothing!>?)): Unit defined in xxx.SegmentedControl

How should I send those parameters to addSegments() method?

Thanks!

draskosaric commented 5 years ago

Hi, I've solved it. When you reference SegmentedControl instance, like top_matches_segmented, you should cast it to SegmentedControl<D>, so the last line should be in my case:

(top_matches_segmented as SegmentedControl<Long>).addSegments( list )

By the way, great control!

RobertApikyan commented 5 years ago

Great, if any question fill free to ask