RobertApikyan / SegmentedControl

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

Flip segment text #8

Closed amrboxit4me closed 6 years ago

amrboxit4me commented 6 years ago

I can't find a method to change segment text so I made a custom adapter and I override this method

@Override
    public void onSegmentSelected(boolean isSelected, boolean isReselected) {
        super.onSegmentSelected(isSelected, isReselected);
        if (isSelected) {
            setSectionDecorationSelected(true);
            itemTV.setText(R.string.on);
        } else {
            setSectionDecorationSelected(false);
            itemTV.setText(R.string.off);
        }
    }

then I used this adapter in my activity by this code segmentedShipSwitch.setAdapter(new ShipSegmentAdapter()); but segment text not fliped

amrboxit4me commented 6 years ago

I tried to made breakpoints but it doesn't stop in the adapter I think it doesn't use it

screen shot 2018-09-06 at 10 06 22 am

amrboxit4me commented 6 years ago

It is worked I have to remove app:segments="@array/switch_array" and I add it programmatically

            ArrayList<CharSequence> segmentDataList = new ArrayList<>();
            segmentDataList.add("On");
            segmentDataList.add("Off");
            segmentedShipSwitch.addSegments(segmentDataList);
amrboxit4me commented 6 years ago

so app:segments="@array/switch_array" force use default adapter

amrboxit4me commented 6 years ago

Use the following to flip text on the adapter

segmentedShipSwitch.findSegmentByAbsolutePosition(this.isEasyShip ? 0 : 1).setSelected(true);
segmentedShipSwitch.findSegmentByAbsolutePosition(this.isEasyShip ? 1 : 0).setSelected(false);

instead ofsegmentedShipSwitch.setSelectedSegment(this.isEasyShip ? 0 : 1);