Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom Adapter #4

Closed
alaa7731 opened this issue Apr 8, 2018 · 7 comments
Closed

Custom Adapter #4

alaa7731 opened this issue Apr 8, 2018 · 7 comments

Comments

@alaa7731
Copy link

alaa7731 commented Apr 8, 2018

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
Copy link
Owner

RobertApikyan commented Apr 8, 2018

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

@RobertApikyan
Copy link
Owner

@RobertApikyan
Copy link
Owner

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
Copy link
Author

alaa7731 commented Apr 9, 2018

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

@alaa7731
Copy link
Author

alaa7731 commented Apr 9, 2018

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
Copy link
Author

alaa7731 commented Apr 9, 2018

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
Copy link
Author

alaa7731 commented Apr 9, 2018

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants