RobertApikyan / SegmentedControl

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

Custom Adapter #4

Closed alaa7731 closed 6 years ago

alaa7731 commented 6 years ago

again, thank you for your work so far.

I have a question regarding implementing Custom adapter, I assume that the Type of adapter can be anything, so in case i made the type as a custom model, which have icon and text, and need to implement the adapter to have icon and text, in the onSegmentBind how can I know if this is the selected or not?

is it even possible to implement it like that, below is my implementation for the custom ViewHolder

public class AppSegmentedViewHolder extends SegmentViewHolder<SegmentedTypeModel> {
    TextView tvTitle;
    ImageView ivIcon;

    public AppSegmentedViewHolder(@NonNull View sectionView) {
        super(sectionView);
        tvTitle = sectionView.findViewById(R.id.tvTitle);
        ivIcon = sectionView.findViewById(R.id.ivIcon);

    }

    @Override
    protected void onSegmentBind(SegmentedTypeModel segmentedTypeModel) {
        tvTitle.setText(segmentedTypeModel.getTitle());
        ivIcon.setImageResource(segmentedTypeModel.getIcon());
    }
}
RobertApikyan commented 6 years ago

Hi !!! try to override onSegmentSelected(boolean isSelected, boolean isReselected) method inside your AppSegmentedViewHolder.

RobertApikyan commented 6 years ago

Also take a look at this utility helper class, for background and radius handling

https://github.com/RobertApikyan/SegmentedControl/blob/master/segmentedcontrolmodule/src/main/java/segmented_control/widget/custom/android/com/segmentedcontrol/utils/Utils.java

RobertApikyan commented 6 years ago

As an example of implementation look at SegmentAdapterImpl.java and SegmentViewHolderImpl.java classes

https://github.com/RobertApikyan/SegmentedControl/tree/master/segmentedcontrolmodule/src/main/java/segmented_control/widget/custom/android/com/segmentedcontrol/custom_segment

alaa7731 commented 6 years ago

great, will check it and give you the result after that

alaa7731 commented 6 years ago

I'm receiving the below exception when I use addSegments

Caused by: java.lang.ClassCastException: java.lang.String cannot be cast to ptasheel.tacme.com.butler.models.SegmentedTypeModel
        at ptasheel.tacme.com.butler.viewholders.AppSegmentedViewHolder.onSegmentBind(AppSegmentedViewHolder.java:15)
        at segmented_control.widget.custom.android.com.segmentedcontrol.item_row_column.SegmentViewHolder.onBind(SegmentViewHolder.java:32)
        at segmented_control.widget.custom.android.com.segmentedcontrol.item_row_column.SegmentViewHolder.onBind(SegmentViewHolder.java:13)
        at section_layout.widget.custom.android.com.sectionlayout.SectionLayout$ViewHolder.bind(SectionLayout.java:183)
        at section_layout.widget.custom.android.com.sectionlayout.SectionLayout$Adapter.onBindViewHolder(SectionLayout.java:249)
        at section_layout.widget.custom.android.com.sectionlayout.SectionLayoutViewControllerComponent$1.onResult(SectionLayoutViewControllerComponent.java:65)
        at section_layout.widget.custom.android.com.sectionlayout.SectionLayoutViewControllerComponent$1.onResult(SectionLayoutViewControllerComponent.java:53)
        at view_component.lib_android.com.view_component.base_view.ControllerComponent.requestHolderComponent(ControllerComponent.java:43)
        at section_layout.widget.custom.android.com.sectionlayout.SectionLayoutViewControllerComponent.insertSectionAtPosition(SectionLayoutViewControllerComponent.java:53)
        at section_layout.widget.custom.android.com.sectionlayout.SectionLayoutViewControllerComponent.addSection(SectionLayoutViewControllerComponent.java:40)
        at section_layout.widget.custom.android.com.sectionlayout.SectionLayout.addSection(SectionLayout.java:62)
        at segmented_control.widget.custom.android.com.segmentedcontrol.SegmentedControlControllerComponent.addSegmentToLastRow(SegmentedControlControllerComponent.java:80)
        at segmented_control.widget.custom.android.com.segmentedcontrol.SegmentedControlControllerComponent.addSegment(SegmentedControlControllerComponent.java:61)
        at segmented_control.widget.custom.android.com.segmentedcontrol.SegmentedControlControllerComponent.addSegments(SegmentedControlControllerComponent.java:248)
        at segmented_control.widget.custom.android.com.segmentedcontrol.SegmentedControl.addSegments(SegmentedControl.java:354)
        at ptasheel.tacme.com.butler.MessageListActivity.initBottomSegmented(MessageListActivity.java:226)
        at ptasheel.tacme.com.butler.MessageListActivity.onCreate(MessageListActivity.java:109)

below are my SegmentViewHolder& SegmentAdapter

SegmentAdapter

public class AppSegmentedAdapter extends SegmentAdapter<SegmentedTypeModel, AppSegmentedViewHolder> {
    private Context context;

    public AppSegmentedAdapter(Context context) {
        this.context = context;
    }

    @NonNull
    @Override
    protected AppSegmentedViewHolder onCreateViewHolder(@NonNull LayoutInflater layoutInflater, ViewGroup viewGroup, int i) {
        return new AppSegmentedViewHolder(context, layoutInflater.inflate(R.layout.segmented_item, null));
    }
}

SegmentViewHolder

public class AppSegmentedViewHolder extends SegmentViewHolder<SegmentedTypeModel> {
    TextView tvTitle;
    ImageView ivIcon;
    Context context;

    public AppSegmentedViewHolder(Context context, @NonNull View sectionView) {
        super(sectionView);
        this.context = context;
        tvTitle = sectionView.findViewById(R.id.tvTitle);
        ivIcon = sectionView.findViewById(R.id.ivIcon);

    }

    @Override
    protected void onSegmentBind(SegmentedTypeModel segmentedTypeModel) {
        tvTitle.setText(segmentedTypeModel.getTitle());
        ivIcon.setImageResource(segmentedTypeModel.getUnselectedIcon());
    }

    @Override
    public void onSegmentSelected(boolean isSelected, boolean isReselected) {
        super.onSegmentSelected(isSelected, isReselected);

        if (isSelected) {
            tvTitle.setTextColor(ContextCompat.getColor(context,R.color.colorPrimaryDark));
            ivIcon.setColorFilter(ContextCompat.getColor(context,R.color.colorPrimaryDark));
        }else{
            tvTitle.setTextColor(ContextCompat.getColor(context,R.color.colorPrimary));
            ivIcon.setColorFilter(ContextCompat.getColor(context,R.color.white));
        }
    }
}

I have a class called SegmentedTypeModel which is my custom model , contains Text and Icon

alaa7731 commented 6 years ago

and my implementation of setAdapter

segmentedTypesFilter.setAdapter(new AppSegmentedAdapter(MessageListActivity.this));

        ArrayList<SegmentedTypeModel> bottomFilter = new ArrayList<SegmentedTypeModel>();

        SegmentedTypeModel segmentedTypeModel = new SegmentedTypeModel();
        segmentedTypeModel.setTitle("1");
        segmentedTypeModel.setSelectedIcon(R.drawable.platinum_icon_act);
        segmentedTypeModel.setUnselectedIcon(R.drawable.platinum_icon);
        bottomFilter.add(segmentedTypeModel);

        segmentedTypeModel = new SegmentedTypeModel();
        segmentedTypeModel.setTitle("2");
        segmentedTypeModel.setSelectedIcon(R.drawable.elder_act);
        segmentedTypeModel.setUnselectedIcon(R.drawable.elder);
        bottomFilter.add(segmentedTypeModel);

        segmentedTypeModel = new SegmentedTypeModel();
        segmentedTypeModel.setTitle("3");
        segmentedTypeModel.setSelectedIcon(R.drawable.disabled_act);
        segmentedTypeModel.setUnselectedIcon(R.drawable.disabled);
        bottomFilter.add(segmentedTypeModel);

        segmentedTypesFilter.addSegments(bottomFilter);
alaa7731 commented 6 years ago

My Bad, i found the problem, I was assigning an array in the XML, which cause the exception, when i removed it it works