RobertApikyan / SegmentedControl

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

None of xml attributes are visible on control #22

Closed draskosaric closed 5 years ago

draskosaric commented 5 years ago

Hi,

I have a segmented control which has imageViews instead of TextViews. None of xml attributes (radius, selectedBackgroundColor, unSelectedBackgroundColor, selectedStrokeColor, unSelectedStrokeColor, focusedBackgroundColor) are visible on control. How can I set them programmatically? I managed to set background color.

This is item_segment.xml:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
              xmlns:tools="http://schemas.android.com/tools"
              android:id="@+id/layout_root"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:orientation="vertical">
    <ImageView
        android:id="@+id/segmented_image"
        android:layout_width="20dp"
        android:layout_height="20dp"
        android:layout_gravity="center"
        tools:src="@drawable/sport_football"
    />
</FrameLayout>

These are adapter and ViewHolder:

class AppSegmentAdapter : SegmentAdapter<Long, AppSegmentViewHolder>() {
    override fun onCreateViewHolder(
        layoutInflater: LayoutInflater,
        viewGroup: ViewGroup,
        i: Int
    ): AppSegmentViewHolder {
        return AppSegmentViewHolder(layoutInflater.inflate(R.layout.item_segment, null))
    }
}

class AppSegmentViewHolder(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)))
    }

    override fun onSegmentSelected(isSelected: Boolean, isReselected: Boolean) {
        super.onSegmentSelected(isSelected, isReselected)
        layout_root.setBackgroundColor(ContextCompat.getColor(layout_root.context, if (isSelected) R.color.colorPrimary else R.color.white))

    }
}

Next is xml code in home_fragment.xml:

<LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
>
    <segmented_control.widget.custom.android.com.segmentedcontrol.SegmentedControl
            android:id="@+id/segmented_control"
            android:layout_width="match_parent"
            android:layout_height="50dp"
            app:columnCount="3"
            app:distributeEvenly="true"
            app:radius="4dp"
            android:layout_margin="8dp"
            app:selectedBackgroundColor="@color/grey"
            app:unSelectedBackgroundColor="@color/white"
            app:selectedStrokeColor="@color/green_dark"
            app:unSelectedStrokeColor="@color/grey_dark_2"
            app:selectionAnimationDuration="200"
            app:focusedBackgroundColor="@color/grey_light_05"
    />
</LinearLayout>

And finally, there is code of setting the view in fragment:

    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)

        val list: List<Long> = listOf(SportHandler.AMERICAN_FOOTBALL, SportHandler.FOOTBALL)
        with (segmented_control as SegmentedControl<Long>) {
            setAdapter(AppSegmentAdapter())
            addSegments( list )
        }

    }

This is how it looks like:

Screen Shot 2019-05-19 at 13 12 27

Please, help.

Thanks in advance!

RobertApikyan commented 5 years ago

Hi, If you create your own AppSegmentViewHolder, you need to handle segment design inside your view holder. All attributes data from xml are available in segmentData filed inside SegmentViewHolder.class. Use Utils.class from library, it is pubic helper class for segment decoration, defining segment's current position radius, background and etc. will be much easier. Also check out, how Utils.class is used in default SegmentViewHolderImpl.class