Activate or de activate recyclerview items - android

I have a fragment which contains a recyclerview and a floating action button. In recyclerview, each row contains a radio button set as disabled by default. my requirement is I need to enable these radio buttons in every row upon floating action button click event. Is there any solution?
Thanks in advance..
OK... I got a solution.
here is my code.
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
for (int i = 0; i <= adapter.getItemCount(); i++) {
View childVIew = recyclerView.getChildAt(i);
if (childVIew != null) {
RadioButton radioYes = childVIew.findViewById(R.id.radio_yes_my_health_info);
radioYes.setEnabled(true);
RadioButton radioNo = childVIew.findViewById(R.id.radio_no_my_health_info);
radioNo.setEnabled(true);
}
}
fab.setImageResource(R.drawable.ic_save_white_24dp);
recyclerView.getRecycledViewPool().setMaxRecycledViews(0, 0);
}
});
Now on FAB button click, all the radio buttons inside my recyclerview will be enabled and I can click to change the state of every radio button. But here I have got a problem. When I click FAB button, all the radio buttons inside the recyclerview will be activated but when I scroll the recyclerview all those radio buttons will be disabled or deactivated again. Please give me a solution for this.

You can have a flag in your adapter like
boolean isActivated = false;
public void setActivated(boolean activated) {
isActivated = activated;
}
and in your onBindViewHolder() method you can check for the flag and set the state of your flag accordingly.
In the OnClickListener of your FAB, call
adapter.setActivated(true);//true or false as per your requirement
adapter.notifyDataSetChanged();
This should work.

You can try looping through every view when you click the button, like so:
for (int i = 0; i < adapter.getItemCount(); i++) {
View view = recycler.getLayoutManager().findViewByPosition(i);
view.setSelected(true); //Select item
notifyItemChanged(i); //Notify item changed
}

Related

buttons in listView not always triggered

I have a listview and in each cell it has a RelativeLayout with 7 buttons.
before the list is scrolled all the buttons work fine (all trigger when clicked) for all visible listView items, but after listView was scrolled some items turn to not clickable (no matter which button in the item I click), and it's random, after another scroll the same item can turn clickable, and other which was before turns to not clickable.
I have noticed that it usually happens (item turns not clickable) after scrolling all the way up.
Another thing that i have noticed that seldom (after 4-5 unsuccessful clicks in a row) the button triggers a few times in a row (like it was delayed). But usually it's not happening after a number of unsuccessful clicks.
In my original code I created an arrayList of RelativeLayouts (each for listView Item), and put the arrayList into adapter. For every 7 buttons (for each cell) I set 7 ids corresponding to their's place in arraylist.
In that way I implemented the OnClick event in the main class.
Here is 3 buttons (out of 7):
for (int i = 0; i < EXPEND_BUTTONS.length; i++) {
if (view.getId() == EXPEND_BUTTONS[i]) {
handleEmojiPanel(i);
break;
}
if (view.getId() == BUTTONS[i] || view.getId() == IMAGES[i]) {
ShowTopItem item = new ShowTopItem(getActivity(), i);
item.show();
break;
}
}
Because of the problem I change the code.
I handled the OnClick event for the buttons in the adapter itself in the getView method (for 2 buttons only):
public View getView(final int position, View convertView, ViewGroup parent) {
pos = position;
Button btn = (Button) listOfObjects.get(position).getChildAt(0);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
ShowTopItem item = new ShowTopItem(getActivity(), position + listChosen);
item.show();
}
});
Button imageBtn = (Button) listOfObjects.get(position).getChildAt(2);
imageBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
ShowTopItem item = new ShowTopItem(getActivity(), position + listChosen);
item.show();
}
});
return listOfObjects.get(position);
}
I have the same result. Nothing changed.
I have looked all over the internet, and it seems that I'm the only one who encountered such issue.
Id anybody knows what can be the issue here?
If some other code is needed, please feel free to ask.
I did not find the reason, but I changed ListView to ScrollView, and all works fine now.
Maybe there is some kind of bug in ListView, but in this case, I wonder why I did not find any complains regarding it.
Anyway, works perfect with ScrollView.

Is this the right way to deselect radio buttons in a listview?

I have a DialogFragment which I am populating with a ListView and a Cursor Adaptor. I was having issue with selecting a radio button and deselecting others. So I did this thing which works. But I wanted to know if I really need a loop there or there is another better way to do it. Here is my code:
viewHolder.radioButton.setOnClickListener(
new View.OnClickListener()
{ #Override
public void onClick(View v) {
for (int i = 0; i < selectionArrayAr.size(); i++){
selectionArrayAr.put(i, false);
}
selectionArrayAr.put(position, true);
notifyDataSetChanged();
}
}
);
I am setting all values to false in the Sparse Array and then finally setting the clicked radio button to true.

Can't solve the selected radio button puzzle in recycler view to add the value of all items?

I have added the radio buttons dynamically in the RecyclerView. Now I want to get the total price with the selected radio buttons of RecyclerView. Somehow I can't understand the puzzle.
just checked first when u clicked on button then it is already clicked or not with this position if not then add your price and display total amount.
Like this
clicked.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(clicked.isChecked()){
amountwithsecdepo = String.valueOf((model.no_of_days*model.charge_per_day)+(model.deposit_amount)+100);
setAnimation(getResources().getString(R.string.rs_sysmbol)+" "+String.valueOf(amountwithsecdepo));
}else {
int temp = (orderModel.o_duration_days * model.charge_per_day) + 100;
setAnimation(getResources().getString(R.string.rs_sysmbol)+" "+String.valueOf(temp));
}
}
});
I think that this might solve your problem.
In your recycler adapter take a float/double variable which will store total.
Initially it will be 0.
in your adapter setOnCheckedChangeListener on your radio button (Inside the ViewHolder Class which is inside your adapter (usually)).
Now whenever you will click on the radio button, the control will go to the checkChangeListener.
all you have to do is add total there.
if (isSelected) {
total = total + yourModel.get(getAdapterPosition()).getPrice();
} else {
total = total - yourModel.get(getAdapterPosition()).getPrice();
}
Let me know if its not clear to you.

ListView with toggle button doesn't scroll smoothly

I have a custom ListView. It's populated by cursor adapter. It's every row includes one ImageView, two TextViews and one ToggleButton. These all loads fine. When I change state of group of ToggleButtons to true, then when I scroll fast listView not scrolling smoothly it hangs for a sec and scrolls. It happens when after group of unchecked buttons appears group of checked buttons and vice versa. Why list view hangs while scrolling?
Any help would be appreciated. My code looks like this:
public void bindView(View view, Context context, final Cursor cursor) {
final ViewHolder viewHolder = new ViewHolder(view);
(...)
if (viewHolder.toggle_artist != null) {
viewHolder.toggle_artist.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
int favourite = isChecked ? 1 : 0;
ContentValues value = new ContentValues();
String where = AmdmContract.ArtistEntry.TABLE_NAME + "." +
AmdmContract.ArtistEntry._ID + " = " + tableId;
value.put(AmdmContract.ArtistEntry.COLUMN_FAVORITE, favourite);
mContext.getContentResolver()
.update(AmdmContract.ArtistEntry.CONTENT_URI,
value, where, null);
}
});
int favFromDB = cursor.getInt(COL_FAVOURITE);
viewHolder.toggle_artist.setChecked(favFromDB == 1);
}
}
Can you please try below code for Toggle Button CheckChangeListener.
Reason: In BaseAdapter getView() method calls on every scroll of ListView and it will calls onCheckChangeListener() automatically.
So replace your code with below code:
tgl.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
ToggleButton selectedToggle = (ToggleButton) v;
if(selectedToggle.isChecked()){
// Code if toggle is checked
}else{
// Code if toggle is not checked
}
}
});
Hope it will help you.
It is a common problem when you show checkboxex or toggle button in listview items. Once you checked few checkboxes and then scroll down the listview, after scrolling up again it all gets mixed up.
It is advised to use maintain a boolean array which can track the checkbox status.
Check this already answered post - Custom listview with checkbox problem

100 Buttons and only 1 Active

I have one activity and here i have 100 buttons, i want that when i press Button 1 then press another Button the Button 1 should get unpressed.
i know i can make this with
if(Button1.isPressed()) {
Button2.setPressed(false);
Button3.setPressed(false);
Button4.setPressed(false);
Button5.setPressed(false);
Button6.setPressed(false);
Button7.setPressed(false);
Button8.setPressed(false);
......................... }
else { do nothing }
.... BUT!
it's too much code
Coders will kill me or will just laugh on me.
any ideas?
maybe there is a way to unpress the all buttons from the activity?
Not the prettiest solution ever, but you could make an OnClickListener like this:
View.OnClickListener listener = new View.OnClickListener() {
public void onClick(View v) {
ViewGroup parent = (ViewGroup) v.getParent();
for (int i = 0; i < parent.getChildCount(); i++) {
View current = parent.getChildAt(i);
if (current != v && current instanceof Button) {
((Button) current).setPressed(false);
}
}
((Button) v).setPressed(true);
}
}
and attach it to all of your buttons.
Then, whenever a button is clicked, it will iterate over all views that are in the same layout (or actually, view group) as the clicked button, and, for any of those views that are buttons except for the clicked button, it will call setPressed(false).
Note that this only works out of the box if all the buttons are in the same layout. If they are in nested layouts, you will have to adapt it a little.
Off topic: What do you need 100 buttons for? That's a lot of buttons. You may want to redesign your user interface
Ok so instead of looping through all the buttons on over and over again when one button is pressed, you can just store a variable which stores the button number of the button that was last pressed. Now, when the second button is pressed, disable the button that was pressed earlier, you get its index from the saved variable, enable the button that was pressed and store its index in the variable.
Heres an example pseudo code to give you and idea:
int buttonLastPressed = 0;
void onButtonClick(Button buttonPressed){
if(buttonLastPressed != 0){
disableButton(buttonLastPressed);
enableButton(buttonPressed);
buttonLastPressed = buttonPressed.getIndex()
}
}
Saves you from looping through each and every button to disable it.
Define id of button 1 to 100
When press button occurs save it's id in some member variable like previous_pressed
Before updating a previous_pressed value find and unpress previous pressed button like this
Button previous_pressed_button = (Button) findViewById(previous_pressed);
Now you have the previous pressed button, So upress it or whatever.

Categories

Resources