mikepenz / FastAdapter

The bullet proof, fast and easy to use adapter library, which minimizes developing time to a fraction...
https://mikepenz.dev
Apache License 2.0
3.85k stars 494 forks source link

Radio button with Expandable functionality is not working #277

Closed sumitsahu2207 closed 7 years ago

sumitsahu2207 commented 7 years ago

Hi Mike,

Hope you're doing well.

Radio button kind of functionality with expandable in same item is not working. I've item which has one shopping cart icon and text views. I need to highlight only one shopping cart at a time when pressed. This functionality is working fine. When item is clicked(except shopping cart icon), item expands and shows details about that item.

Issue occurs when I click item which actually expands the item and then shopping cart icon. Multiple shopping cart icon starts getting highlighted instead of one at a time.

I've implemented this expandable item functionality with radio button(i.e. shopping cart icon) in ViewPager Fragment.

Please suggest, where's the issue

Attached are the files which I've used for this.

Have a great day ahead, many thanks.

Best Regards, Sumit

item.xml.txt viewpagerFragment Layout.xml.txt viewpagerFragment_itemview_clicklistener.java.txt item.java.txt

mikepenz commented 7 years ago

@sumitsahu2207 you have to check in your code that there are not more than one selection. The radioButton itself won't deselect other radiobuttons as they do not know about each other.

Thanks for the sample code, but I would need a whole sample application to test this.

sumitsahu2207 commented 7 years ago

Thanks Mike. Let me check this.

mikepenz commented 7 years ago

Ok great.

sumitsahu2207 commented 7 years ago

Hi Mike,

I couldn't fix this issue. Shopping cart icon(kind of radio button) is already working fine. But as expandable item functionality is also linked with the same item, it's interfering in item selections. Would you require whole app code or just whole activity and its fragment, layouts and corresponding code will do.

Best Regards Sumit

sumitsahu2207 commented 7 years ago

Hi Mike,

1st Image is when activity starts. 2nd Image is when one shopping cart icon clicked. It changes to Blue from grey 3rd Image I've expanded one of item and tried selecting other item. Other item shopping cart icon also changes to blue from grey.

Basically i'm expecting only one shopping item to be blue at a time.

1 2 3

sumitsahu2207 commented 7 years ago

Hi Mike,

You must be busy, but did you get a chance to look into this issue.

Thanks.

mikepenz commented 7 years ago

how do you handle the selected state of an icon? do you have it set via a ColorStateList or do you define it in the `onClick? how would you expect to remove the selected icon again when a new icon is clicked?

In general the FastAdapter would not call bind if an item is no longer selected, it will just set the state of the item to normal.

If you need the bindView call to be done change the mode of the FastAdapter to call the bind method upon a change: https://github.com/mikepenz/FastAdapter/blob/develop/library-core/src/main/java/com/mikepenz/fastadapter/FastAdapter.java#L196

sumitsahu2207 commented 7 years ago

Thank you for your reply Mike :)

I'm not using ColorStateList, instead i was trying with below logic in item bindView using isSelected() method.

    if (isSelected()){
       viewHolder.mItemBasketSelectedYes.setVisibility(View.VISIBLE);
        viewHolder.mItemBasketSelectedNo.setVisibility(View.GONE);
    }else {
          viewHolder.mItemBasketSelectedYes.setVisibility(View.GONE);
        viewHolder.mItemBasketSelectedNo.setVisibility(View.VISIBLE);
    }

And Below Shopping Basket View Click Listener is in Fragment:

    //a custom OnCreateViewHolder listener class which is used to create the viewHolders
    //we define the listener for the imageLovedContainer here for better performance
    //you can also define the listener within the items bindView method but performance is better if you do it like this
    fastAdapterService.withOnCreateViewHolderListener(new FastAdapter.OnCreateViewHolderListener() {
        @Override
        public RecyclerView.ViewHolder onPreCreateViewHolder(ViewGroup parent, int viewType) {
            return fastAdapterService.getTypeInstance(viewType).getViewHolder(parent);
        }

        @Override
        public RecyclerView.ViewHolder onPostCreateViewHolder(final RecyclerView.ViewHolder viewHolder) {
            //we do this for our ServicePackage.ViewHolder
            if (viewHolder instanceof ServicePackage.ViewHolder) {
                //if we click on the rl_basket_container (mItemBasketContainer)
                mClickListenerHelper.listen(viewHolder, ((ServicePackage.ViewHolder) viewHolder).mItemBasketContainer, new ClickListenerHelper.OnClickListener<ServicePackage>() {
                    @Override
                    public void onClick(View v, int position, ServicePackage item) {

                            Set<Integer> selections = fastAdapterService.getSelections();
                            if (!selections.isEmpty()) {
                                int selectedPosition = selections.iterator().next();
                                fastAdapterService.deselect();

// ServicePackage servicePackage = (ServicePackage) fastAdapterService.getItem(selectedPosition); // servicePackage.withPressed(false); fastAdapterService.notifyItemChanged(selectedPosition); } fastAdapterService.select(position); // item.withPressed(true); // fastAdapterService.notifyItemChanged(position);

                        mListener.onServiceFragmentClick(item,serviceSubCategoryIndex); //Send back clicked Service Package

                    }
                    });
                }

            return viewHolder;
        }
    });

I've below click listener in item (which is for animating when expands/collapses)

//we define a clickListener in here so we can directly animate
final private FastAdapter.OnClickListener<ServicePackage> onClickListener = new FastAdapter.OnClickListener<ServicePackage>() {
    @Override
    public boolean onClick(View v, IAdapter adapter, ServicePackage item, int position) {

// if(item.mPressed){ // item.setItemSelected(true); // }else { // item.setItemSelected(false); // }

// return true;

        if (item.getSubItems() != null) {
            if (!item.isExpanded()) {

// ViewCompat.animate(v.findViewById(R.id.material_drawer_icon)).rotation(180).start(); } else { // ViewCompat.animate(v.findViewById(R.id.material_drawer_icon)).rotation(0).start(); } return mOnClickListener != null ? mOnClickListener.onClick(v, adapter, item, position) : true; }

        return mOnClickListener != null ? mOnClickListener.onClick(v, adapter, item, position) : false;

    }
};

I've tried with many combination (using sepearate mPressed fields also for button press tracking). but it's not working.

I've noticed that when item expands/collapse view basket view 'rl_basket_container' listener get called once for 'item' and 'subitem' so when I tried to use 'mPressed' field it throws 'Cannot Cast Subitem to Item' error. As they're of different type.

In general, clicking basket icon works until item is not expanded. Since I guess, it selects item which changes whole basket icon color selection logic.

Thanks, Sumit

mikepenz commented 7 years ago

Perhaps it is easier if you send me some running code project via email which I can test and adjust. Just remove project specific code

sumitsahu2207 notifications@github.com schrieb am Fr., 2. Dez. 2016, 23:26:

Thank you for your reply Mike :)

I'm not using ColorStateList, instead i was trying with below logic in item bindView using isSelected() method.

if (isSelected()){
   viewHolder.mItemBasketSelectedYes.setVisibility(View.VISIBLE);
    viewHolder.mItemBasketSelectedNo.setVisibility(View.GONE);
}else {
      viewHolder.mItemBasketSelectedYes.setVisibility(View.GONE);
    viewHolder.mItemBasketSelectedNo.setVisibility(View.VISIBLE);
}

And Below Shopping Basket View Click Listener is in Fragment:

//a custom OnCreateViewHolder listener class which is used to create the viewHolders
//we define the listener for the imageLovedContainer here for better performance
//you can also define the listener within the items bindView method but performance is better if you do it like this
fastAdapterService.withOnCreateViewHolderListener(new FastAdapter.OnCreateViewHolderListener() {
    @Override
    public RecyclerView.ViewHolder onPreCreateViewHolder(ViewGroup parent, int viewType) {
        return fastAdapterService.getTypeInstance(viewType).getViewHolder(parent);
    }

    @Override
    public RecyclerView.ViewHolder onPostCreateViewHolder(final RecyclerView.ViewHolder viewHolder) {
        //we do this for our ServicePackage.ViewHolder
        if (viewHolder instanceof ServicePackage.ViewHolder) {
            //if we click on the rl_basket_container (mItemBasketContainer)
            mClickListenerHelper.listen(viewHolder, ((ServicePackage.ViewHolder) viewHolder).mItemBasketContainer, new ClickListenerHelper.OnClickListener<ServicePackage>() {
                @Override
                public void onClick(View v, int position, ServicePackage item) {

                        Set<Integer> selections = fastAdapterService.getSelections();
                        if (!selections.isEmpty()) {
                            int selectedPosition = selections.iterator().next();
                            fastAdapterService.deselect();

// ServicePackage servicePackage = (ServicePackage) fastAdapterService.getItem(selectedPosition); // servicePackage.withPressed(false); fastAdapterService.notifyItemChanged(selectedPosition); } fastAdapterService.select(position); // item.withPressed(true); // fastAdapterService.notifyItemChanged(position);

                    mListener.onServiceFragmentClick(item,serviceSubCategoryIndex); //Send back clicked Service Package

                }
                });
            }

        return viewHolder;
    }
});

I've below click listener in item (which is for animating when expands/collapses)

//we define a clickListener in here so we can directly animate final private FastAdapter.OnClickListener onClickListener = new FastAdapter.OnClickListener() { @Override public boolean onClick(View v, IAdapter adapter, ServicePackage item, int position) {

// if(item.mPressed){ // item.setItemSelected(true); // }else { // item.setItemSelected(false); // }

// return true;

    if (item.getSubItems() != null) {
        if (!item.isExpanded()) {

// ViewCompat.animate(v.findViewById(R.id.material_drawer_icon)).rotation(180).start(); } else { // ViewCompat.animate(v.findViewById(R.id.material_drawer_icon)).rotation(0).start(); } return mOnClickListener != null ? mOnClickListener.onClick(v, adapter, item, position) : true; }

    return mOnClickListener != null ? mOnClickListener.onClick(v, adapter, item, position) : false;

}

};

I've tried with many combination (using sepearate mPressed fields also for button press tracking). but it's not working.

I've noticed that when item expands/collapse view basket view 'rl_basket_container' listener get called once for 'item' and 'subitem' so when I tried to use 'mPressed' field it throws 'Cannot Cast Subitem to Item' error. As they're of different type.

In general, clicking basket icon works until item is not expanded. Since I guess, it selects item which changes whole basket icon color selection logic.

Thanks, Sumit

— You are receiving this because you modified the open/close state. Reply to this email directly, view it on GitHub https://github.com/mikepenz/FastAdapter/issues/277#issuecomment-264579316, or mute the thread https://github.com/notifications/unsubscribe-auth/ABaGiHwpiOjJMLD24ERoPI9fahOpM1pmks5rEJsNgaJpZM4K8nuS .

-- Mike Penz


Mike Penz Android | Backend | Open Source | Web Developer mikepenz.com lanora.io

sumitsahu2207 commented 7 years ago

Sure Mike, I'll send one working sample to your email id - mikepenz@gmail.com

thank you so much.

sumitsahu2207 commented 7 years ago

Hi Mike, I've sent the sample app to you, please check. Thanks.

mikepenz commented 7 years ago

Sent you the fixed source.

It was no library issue, just some bad configuration and the requirement that an update is done when selecting / deselecting.

beside removing a tiny bit of code and letting the fastadapter do it's magic, correct configuration and enabling: https://github.com/mikepenz/FastAdapter/blob/develop/library-core/src/main/java/com/mikepenz/fastadapter/FastAdapter.java#L196 I did nothing :)

sumitsahu2207 commented 7 years ago

Thank you so much Mike, it works now :)

sumitsahu2207 commented 7 years ago

sorry mike, I see different logic, actually I wanted to change color of shopping basket only when basket selected not item. when item selects it should just expand. I'll look into that

sumitsahu2207 commented 7 years ago

I got it working finally, thanks. Your kind help is much appreciated :)