alexandrius / accordion-swipe-layout

Accordion Swipe Layout for Android
136 stars 27 forks source link

Programatically change the icons #4

Closed ionutboanta closed 7 years ago

ionutboanta commented 7 years ago

Hey man, really cool library, one of the very few that actually work. I have one question tho. How can I change the icons/colors in the runtime? Changing them in the recycler adapter did nothing.

Thank you in advance!

alexandrius commented 7 years ago

You could create different ItemViewTypes inside RecyclerAdapter. But anyway I will be implementing this feature soon

ionutboanta commented 7 years ago

Can you find the time to give me a very little example on how could I achieve this? As you know there are little to none examples on this theme, and I am pretty much a beginner. ((SwipeLayout) holder.itemView).setLeftIcons(some array) in the onBindViewHolder doesn't seem to override the main style.

alexandrius commented 7 years ago

Do you need different buttons on every list item? Or say 3 types of item? If you have certain amount of buttons required to each list item you can do so.

@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    if (viewType == ITEM_TYPE_ONE) {
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.recycler_view_item, parent, false);
        return new ViewHolder(v);
    } else if (viewType == ITEM_TYPE_TWO) {
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.recycler_view_item_two, parent, false);
        return new ViewHolder(v);
    }
    View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.recycler_view_item_three, parent, false);
    return new ViewHolder(v);
}

@Override
public void onBindViewHolder(ViewHolder holder, int position) {
    if (getItemViewType(position) == ITEM_TYPE_ONE) {
        holder.textView.setText("Item # " + strings.get(position));
        ((SwipeLayout) holder.itemView).setItemState(SwipeLayout.ITEM_STATE_COLLAPSED, false);
    } else if (getItemViewType(position) == ITEM_TYPE_TWO) {

    } else {

    }
}

private static final int ITEM_TYPE_ONE = 1;
private static final int ITEM_TYPE_TWO = 2;
private static final int ITEM_TYPE_THREE = 3;

@Override
public int getItemViewType(int position) {
    if (position == 0) {
        return ITEM_TYPE_ONE;
    } else if (position == 1) {
        return ITEM_TYPE_TWO;
    }
    return ITEM_TYPE_THREE;
}

This means you inflate different SwipeLayout layout xml-s for each type of item.

If you need even more dynamic views I'll be implementing this feature on the weekend. So stay tuned

ionutboanta commented 7 years ago

I think that will do it, I need to get some information from the MainActivity since I have to make 2 types of swipe layouts based on some information I get from the server. Thank you for your time!