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.
Related
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
}
I want to implement this
.
I want to check if the clicked item is fully visible and if it's not I would like to smoothly scroll upwards/downwards. I have a GridLayoutManager with 3 columns. The items are all of the same size, and are just ImageViews.
I am able to get the RecyclerView to scroll with:
#Override
public void onClick(View view) {
int adapterPosition = RecHolder.this.getAdapterPosition();
mRecyclerView.scrollToPosition(adapterPosition);
...
}
But it's not a "scroll", it's very laggish and way too quick.
If I try to use mRecyclerView.smoothScrollToPosition(adapterPosition); the result is the same. Exactly the same movement, there is no visible difference.
Don't bother testing to see if it's not completely visible. Just insert a command to scroll to that position. Since you didn't post any of your code I can't specifically say how you would do it. I personally have my adapter create an intent and my activity handles the intent. If that's what you're doing then you can include the getAdapterPosition() as an extra like this (vh is my ViewHolder).
vh.mImageView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
SelectItemGridAdapter.ContentViewHolder vh = (SelectItemGridAdapter.ContentViewHolder) (((View)v.getParent()).getTag());
Intent intent = new Intent(ACTION_SELECT_Item);
intent.putExtra(Constants.MSG_TYPE, SELECT_ITEM_TAPPED);
intent.putExtra(SELECT_ITEM_TAPPED_ID, vh.viewModel.mItemId);
intent.putExtra(SELECT_ITEM_TAPPED_POS, vh.getAdapterPosition());
LocalBroadcastManager.getInstance(mContext).sendBroadcast(intent);
}
});
Then the reveiver in the activity can get the SELECT_ITEM_TAPPED_POS value...
int pos = intent.getIntExtra(SelectItemGridAdapter.SELECT_ITEM_TAPPED_POS, -1);
if (pos > -1)
rv.scrollToPosition(pos);
HTH, Mike
It's a common question, I know.
I have a ListView of 100 items having 0 to 99 positions. Only first 10 items are visible when the list is rendered for the first time, right? I can use setSelection(int) in ListView.post(Runnable) for positions 0 to 9, that's working correctly. What if I want to select an item having position 45? setSelection(int) doesn't seem to work because when the list is rendered we do not have any item having position 45 rather it will be rendered when we scroll down. So my question is:
How can I select an item (and make it visible) even the item is not rendered?
try the following code:
listview.post(new Runnable()
{
#Override
public void run()
{
listview.setSelection(45); // your position
View view = listview.getChildAt(45);
if (view != null)
{
view.requestFocus(); //request focus on row
}
}
});
}
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.
private void setListviewSelection(final ListView list, final int pos) {
list.post(new Runnable() {
#Override
public void run() {
list.setSelection(pos);
for (int i = 0; i < list.getChildCount(); i++) {
View v = list.getChildAt(i);
if (i == pos && v != null)
v.setBackgroundColor(Color.argb(200, 51, 181, 229));
else if (v != null)
v.setBackgroundColor(Color.BLACK);
}
}
});
}
Here is a code I'm using to imitate a selection in my music player. The idea is that when user press Next or Previous button an element is highlighted in the ListView, but this is not working the way I want, because setSelection doesn't scroll smoothly and basically some elements are not highlighted correctly. For better explanation, what I'm actually trying to implement is a Winamp app which has that way of scrolling when you press next/previous button (when viewing your playlist).
Using setSelectionFromTop(), smoothScrollToPosition() didn't work correctly either.
You can only update Views from the main thread. Move your code to the Main thread.
http://bestsiteinthemultiverse.com/2009/12/android-selected-state-listview-example/
Here is the solution which works perfectly.